機能テスト (C++20)
提供: cppreference.com
< cpp
標準は C++11 またはそれ以降に導入された C++ の言語およびライブラリの機能に対応するプリプロセッサマクロの集合を定義しています。 これらは各機能の存在を検出するためのシンプルかつ移植性のある方法として意図されています。
属性
__has_cpp_attribute( attribute-token )
|
|||||||||
attribute-token (マクロ展開後) の表す属性の存在をチェックします。
標準の属性の場合は、その属性がワーキングドラフトに追加された年と月に展開されます (下の表を参照してください)。 ベンダ固有の属性は非ゼロの値によって判定されます。
__has_cpp_attribute は #if および #elif の式内で展開できます。
これらは #ifdef、 #ifndef および defined によって定義済みのマクロとして扱われますが、それ以外のいかなる場所でも使用することはできません。
| attribute-token | 属性 | 値 | 標準 |
|---|---|---|---|
carries_dependency
|
[[carries_dependency]]
|
200809L
|
(C++11) |
deprecated
|
[[deprecated]]
|
201309L
|
(C++14) |
fallthrough
|
[[fallthrough]]
|
201603L
|
(C++17) |
likely
|
[[likely]]
|
201803L
|
(C++20) |
maybe_unused
|
[[maybe_unused]]
|
201603L
|
(C++17) |
no_unique_address
|
[[no_unique_address]]
|
201803L
|
(C++20) |
nodiscard
|
[[nodiscard]]
|
201603L
|
(C++17) |
noreturn
|
[[noreturn]]
|
200809L
|
(C++11) |
unlikely
|
[[unlikely]]
|
201803L
|
(C++20) |
言語の機能
以下のマクロはすべての翻訳単位内で定義済みです。 各マクロはそれぞれ対応する機能がワーキングドラフトに含まれた年と月に対応する整数リテラルに展開されます。
機能が大幅に変更されたときは、マクロもそれに応じて更新されます。
| マクロの名前 | 機能 | 値 | 標準 |
|---|---|---|---|
__cpp_aggregate_bases |
集成体初期化の拡張 | 201603L |
(C++17) |
__cpp_aggregate_nsdmi |
メンバ初期化子と集成体 | 201304L |
(C++14) |
__cpp_aggregate_paren_init |
直接初期化形式の集成体初期化 | 201902L |
(C++20) |
__cpp_alias_templates |
テンプレートエイリアス | 200704L |
(C++11) |
__cpp_aligned_new |
オーバーアラインされたデータのための動的メモリ確保 | 201606L |
(C++17) |
__cpp_attributes |
属性 | 200809L |
(C++11) |
__cpp_binary_literals |
C++ コア言語における2進リテラル | 201304L |
(C++14) |
__cpp_capture_star_this |
[=,*this] としての値による *this のラムダキャプチャ | 201603L |
(C++17) |
__cpp_char8_t |
char8_t |
201811L |
(C++20) |
__cpp_conditional_explicit |
explicit(bool) |
201806L |
(C++20) |
__cpp_constexpr |
constexpr | 200704L |
(C++11) |
constexpr 関数に対する制約の緩和 / constexpr メンバ関数と暗黙の const |
201304L |
(C++14) | |
| constexpr ラムダ | 201603L |
(C++17) | |
| constexpr 関数におけるトリビアルなデフォルト初期化および asm 宣言 | 201907L |
(C++20) | |
__cpp_coroutines |
コルーチン | 201902L |
(C++20) |
__cpp_decltype |
decltype | 200707L |
(C++11) |
__cpp_decltype_auto |
普通の関数に対する戻り値の型の推定 | 201304L |
(C++14) |
__cpp_deduction_guides |
クラステンプレートに対するテンプレートの実引数推定 | 201703L |
(C++17) |
__cpp_delegating_constructors |
委譲コンストラクタ | 200604L |
(C++11) |
__cpp_enumerator_attributes |
列挙子に対する属性 | 201411L |
(C++17) |
__cpp_fold_expressions |
畳み込み式 | 201603L |
(C++17) |
__cpp_generic_lambdas |
総称 (多相) ラムダ式 | 201304L |
(C++14) |
__cpp_guaranteed_copy_elision |
単純化された値カテゴリによる保証されたコピー省略 | 201606L |
(C++17) |
__cpp_hex_float |
16進浮動小数点リテラル | 201603L |
(C++17) |
__cpp_if_constexpr |
constexpr if |
201606L |
(C++17) |
__cpp_impl_destroying_delete |
destroying operator delete (コンパイラの対応) | 201806L |
(C++20) |
__cpp_impl_three_way_comparison |
三方比較 (コンパイラの対応) | 201711L |
(C++20) |
__cpp_inheriting_constructors |
継承コンストラクタ | 200802L |
(C++11) |
| 継承コンストラクタの文面の修正 | 201511L |
(C++17) | |
__cpp_init_captures |
一般化されたラムダキャプチャ | 201304L |
(C++14) |
__cpp_initializer_lists |
初期化子リスト | 200806L |
(C++11) |
__cpp_inline_variables |
インライン変数 | 201606L |
(C++17) |
__cpp_lambdas |
ラムダ式 | 200907L |
(C++11) |
__cpp_namespace_attributes |
名前空間に対する属性 | 201411L |
(C++17) |
__cpp_noexcept_function_type |
例外指定を型システムの一部とする | 201510L |
(C++17) |
__cpp_nontype_template_args |
すべての非型テンプレート引数に対して定数評価を可能とする | 201411L |
(C++17) |
__cpp_nontype_template_parameter_auto |
非型テンプレート引数を auto で宣言 |
201606L |
(C++17) |
__cpp_nontype_template_parameter_class |
非型テンプレート引数におけるクラス型 | 201806L |
(C++20) |
__cpp_nsdmi |
非静的データメンバの初期化子 | 200809L |
(C++11) |
__cpp_range_based_for |
範囲ベースの for ループ |
200907L |
(C++11) |
一般化された範囲ベースの for ループ |
201603L |
(C++17) | |
__cpp_raw_strings |
生文字列リテラル | 200710L |
(C++11) |
__cpp_ref_qualifiers |
参照修飾子 | 200710L |
(C++11) |
__cpp_return_type_deduction |
普通の関数に対する戻り値の型の推定 | 201304L |
(C++14) |
__cpp_rvalue_references |
右辺値参照 | 200610L |
(C++11) |
__cpp_sized_deallocation |
サイズ付き解放 | 201309L |
(C++14) |
__cpp_static_assert |
static_assert | 200410L |
(C++11) |
拡張された static_assert |
201411L |
(C++17) | |
__cpp_structured_bindings |
構造化束縛 | 201606L |
(C++17) |
__cpp_template_template_args |
テンプレートテンプレート引数のマッチング | 201611L |
(C++17) |
__cpp_threadsafe_static_init |
動的初期化および破棄のマルチスレッド対応 | 200806L |
(C++11) |
__cpp_unicode_characters |
新しい文字型 (char16_t と char32_t) |
200704L |
(C++11) |
__cpp_unicode_literals |
Unicode 文字列リテラル | 200710L |
(C++11) |
__cpp_user_defined_literals |
ユーザ定義リテラル | 200809L |
(C++11) |
__cpp_variable_templates |
変数テンプレート | 201304L |
(C++14) |
__cpp_variadic_templates |
可変長引数テンプレート | 200704L |
(C++11) |
__cpp_variadic_using |
using 宣言におけるパック展開 |
201611L |
(C++17) |
ライブラリの機能
ヘッダ <version> または以下の表内の対応するヘッダがインクルードされると、以下のマクロが定義されます。 各マクロはそれぞれ対応する機能がワーキングドラフトに含まれた年と月に対応する整数リテラルに展開されます。
機能が大幅に変更されたときは、マクロもそれに応じて更新されます。
| マクロの名前 | 機能 | 値 | ヘッダ | 標準 |
|---|---|---|---|---|
__cpp_lib_addressof_constexpr |
constexpr std::addressof | 201603L |
<memory> | (C++17) |
__cpp_lib_allocator_traits_is_always_equal |
std::allocator_traits::is_always_equal | 201411L |
<memory> <scoped_allocator> <string> <deque> <forward_list> <list> <vector> <map> <set> <unordered_map> <unordered_set> | (C++17) |
__cpp_lib_any |
std::any | 201606L |
<any> | (C++17) |
__cpp_lib_apply |
std::apply | 201603L |
<tuple> | (C++17) |
__cpp_lib_array_constexpr |
std::reverse_iterator、 std::move_iterator、 std::array および範囲アクセスに constexpr 修飾子を追加 | 201603L |
<iterator> <array> | (C++17) |
__cpp_lib_as_const |
std::as_const | 201510L |
<utility> | (C++17) |
__cpp_lib_atomic_is_always_lock_free |
constexpr atomic<T>::is_always_lock_free | 201603L |
<atomic> | (C++17) |
__cpp_lib_atomic_ref |
std::atomic_ref | 201806L |
<atomic> | (C++20) |
__cpp_lib_bind_front |
std::bind_front | 201811L |
<functional> | (C++20) |
__cpp_lib_bit_cast |
std::bit_cast | 201806L |
<bit> | (C++20) |
__cpp_lib_bool_constant |
std::bool_constant | 201505L |
<type_traits> | (C++17) |
__cpp_lib_bounded_array_traits |
std::is_bounded_array、 std::is_unbounded_array | 201902L |
<type_traits> | (C++20) |
__cpp_lib_boyer_moore_searcher |
std::boyer_moore_searcher | 201603L |
<functional> | (C++17) |
__cpp_lib_byte |
バイト型の定義 | 201603L |
<cstddef> | (C++17) |
__cpp_lib_char8_t |
char8_t のためのライブラリの対応 |
201811L |
<atomic> <filesystem> <istream> <limits> <locale> <ostream> <string> <string_view> | (C++20) |
__cpp_lib_chrono |
std::chrono::duration および std::chrono::time_point に対する丸め関数 | 201510L |
<chrono> | (C++17) |
| std::chrono::duration および std::chrono::time_point のすべてのメンバ関数を constexpr にする | 201611L |
<chrono> | (C++17) | |
__cpp_lib_chrono_udls |
時間型のためのユーザ定義リテラル | 201304L |
<chrono> | (C++14) |
__cpp_lib_clamp |
一組の境界値の間に値を「クランプ」するためのアルゴリズム (std::clamp) | 201603L |
<algorithm> | (C++17) |
__cpp_lib_complex_udls |
std::complex のためのユーザ定義リテラル | 201309L |
<complex> | (C++14) |
__cpp_lib_concepts |
標準ライブラリコンセプト | 201806L |
<concepts> | (C++20) |
__cpp_lib_constexpr |
雑多な機能に対する constexpr | 201811L |
<*.h> を除くあらゆる C++ 標準ライブラリヘッダ
|
(C++20) |
__cpp_lib_constexpr_swap_algorithms |
std::swap と swap 関連関数に対する constexpr | 201806L |
<algorithm> | (C++20) |
__cpp_lib_destroying_delete |
destroying operator delete (ライブラリの対応) | 201806L |
<new> | (C++20) |
__cpp_lib_enable_shared_from_this |
shared_from_this の再有効化 | 201603L |
<memory> | (C++17) |
__cpp_lib_erase_if |
統一コンテナ削除 | 201811L |
<string> <deque> <forward_list> <list> <vector> <map> <set> <unordered_map> <unordered_set> | (C++20) |
__cpp_lib_exchange_function |
exchange() ユーティリティ関数 | 201304L |
<utility> | (C++14) |
__cpp_lib_execution |
実行ポリシー | 201603L |
<execution> | (C++17) |
| std::execution::unsequenced_policy | 201902L |
<execution> | (C++20) | |
__cpp_lib_filesystem |
ファイルシステムライブラリ | 201703L |
<filesystem> | (C++17) |
__cpp_lib_gcd_lcm |
std::gcd、 std::lcm | 201606L |
<numeric> | (C++17) |
__cpp_lib_generic_associative_lookup |
連想コンテナに異種混合比較検索を追加 | 201304L |
<map> <set> | (C++14) |
__cpp_lib_generic_unordered_lookup |
非順序連想コンテナに異種混合比較検索を追加 | 201811L |
<unordered_map> <unordered_set> | (C++20) |
__cpp_lib_hardware_interference_size |
constexpr std::hardware_{constructive, destructive}_interference_size | 201703L |
<new> | (C++17) |
__cpp_lib_has_unique_object_representations |
std::has_unique_object_representations | 201606L |
<type_traits> | (C++17) |
__cpp_lib_hypot |
std::hypot の3引数オーバーロード | 201603L |
<cmath> | (C++17) |
__cpp_lib_incomplete_container_elements |
標準コンテナに対する最小限の不完全型サポート | 201505L |
<forward_list> <list> <vector> | (C++17) |
__cpp_lib_integer_sequence |
コンパイル時整数列 | 201304L |
<utility> | (C++14) |
__cpp_lib_interpolate |
std::lerp()、 std::midpoint() | 201902L |
<cmath> <numeric> | (C++20) |
__cpp_lib_integral_constant_callable |
std::integral_constant | 201304L |
<type_traits> | (C++14) |
__cpp_lib_invoke |
std::invoke 関数テンプレート | 201411L |
<functional> | (C++17) |
__cpp_lib_is_aggregate |
std::is_aggregate 型特性 | 201703L |
<type_traits> | (C++17) |
__cpp_lib_is_constant_evaluated |
定数評価文脈の検出 (std::is_constant_evaluated()) | 201811L |
<type_traits> | (C++20) |
__cpp_lib_is_final |
std::is_final | 201402L |
<type_traits> | (C++14) |
__cpp_lib_is_invocable |
std::is_invocable、 std::invoke_result | 201703L |
<type_traits> | (C++17) |
__cpp_lib_is_null_pointer |
std::is_null_pointer | 201309L |
<type_traits> | (C++14) |
__cpp_lib_is_swappable |
[nothrow-]swappable 型特性 | 201603L |
<type_traits> | (C++17) |
__cpp_lib_launder |
Core Issue 1776: 参照メンバを含むクラスオブジェクトの置き換え (std::launder) | 201606L |
<new> | (C++17) |
__cpp_lib_list_remove_return_type |
std::forward_list および std::list のメンバ std::list::remove、 std::list::remove_if および std::list::unique の戻り値の型の変更 | 201806L |
<forward_list> <list> | (C++20) |
__cpp_lib_logical_traits |
論理演算子型特性 | 201510L |
<type_traits> | (C++17) |
__cpp_lib_make_from_tuple |
std::make_from_tuple: コンストラクタに対する apply | 201606L |
<tuple> | (C++17) |
__cpp_lib_make_reverse_iterator |
std::make_reverse_iterator | 201402L |
<iterator> | (C++14) |
__cpp_lib_make_unique |
std::make_unique | 201304L |
<memory> | (C++14) |
__cpp_lib_map_try_emplace |
std::map::try_emplace、 std::map::insert_or_assign | 201411L |
<map> | (C++17) |
__cpp_lib_math_special_functions |
C++17 のための数学の特殊関数 | 201603L |
<cmath> | (C++17) |
__cpp_lib_memory_resource |
std::pmr::memory_resource | 201603L |
<memory_resource> | (C++17) |
__cpp_lib_node_extract |
map と set のスプライシング (std::map::extract、 std::map::merge、 std::map::insert(node_type) など) | 201606L |
<map> <set> <unordered_map> <unordered_set> | (C++17) |
__cpp_lib_nonmember_container_access |
非メンバの size() その他 (統一コンテナアクセス) |
201411L |
<iterator> <array> <deque> <forward_list> <list> <map> <regex> <set> <string> <unordered_map> <unordered_set> <vector> | (C++17) |
__cpp_lib_not_fn |
std::not_fn | 201603L |
<functional> | (C++17) |
__cpp_lib_null_iterators |
ヌル前方イテレータ | 201304L |
<iterator> | (C++14) |
__cpp_lib_optional |
std::optional | 201606L |
<optional> | (C++17) |
__cpp_lib_parallel_algorithm |
並列処理 TS を C++17 に採択 | 201603L |
<algorithm> <numeric> | (C++17) |
__cpp_lib_quoted_string_io |
std::quoted | 201304L |
<iomanip> | (C++14) |
__cpp_lib_ranges |
範囲ライブラリと制約付きアルゴリズム | 201811L |
<algorithm> <functional> <iterator> <memory> <ranges> | (C++20) |
__cpp_lib_raw_memory_algorithms |
メモリ管理ツールの拡張 | 201606L |
<memory> | (C++17) |
__cpp_lib_result_of_sfinae |
std::result_of と SFINAE | 201210L |
<functional> <type_traits> | (C++14) |
__cpp_lib_robust_nonmodifying_seq_ops |
非変更シーケンス操作をより頑強にする (std::mismatch、 std::equal および std::is_permutation に対する2範囲オーバーロード) | 201304L |
<algorithm> | (C++14) |
__cpp_lib_sample |
std::sample | 201603L |
<algorithm> | (C++17) |
__cpp_lib_scoped_lock |
可変長引数版の std::lock_guard (std::scoped_lock) | 201703L |
<mutex> | (C++17) |
__cpp_lib_shared_mutex |
std::shared_mutex (タイムアウト付きでない) | 201505L |
<shared_mutex> | (C++17) |
__cpp_lib_shared_ptr_arrays |
std::shared_ptr | 201611L |
<memory> | (C++17) |
__cpp_lib_shared_ptr_weak_type |
shared_ptr::weak_type | 201606L |
<memory> | (C++17) |
__cpp_lib_shared_timed_mutex |
shared_mutex を std::shared_timed_mutex に名前変更 |
201402L |
<shared_mutex> | (C++14) |
__cpp_lib_string_udls |
文字列型のためのユーザ定義リテラル | 201304L |
<string> | (C++14) |
__cpp_lib_string_view |
std::string_view | 201606L |
<string> <string_view> | (C++17) |
__cpp_lib_three_way_comparison |
三方比較 (ライブラリの対応) | 201711L |
<compare> | (C++20) |
__cpp_lib_to_chars |
初等文字列変換 (std::to_chars、 std::from_chars) | 201611L |
<charconv> | (C++17) |
__cpp_lib_transformation_trait_aliases |
戻ってきた TransformationTraits | 201304L |
<type_traits> | (C++14) |
__cpp_lib_transparent_operators |
透過的な演算子関数オブジェクト (std::less<> など) | 201210L |
<functional> | (C++14) |
| std::owner_less をより柔軟にする (std::owner_less<void>) | 201510L |
<memory> <functional> | (C++17) | |
__cpp_lib_tuple_element_t |
std::tuple_element | 201402L |
<tuple> | (C++14) |
__cpp_lib_tuples_by_type |
型によるタプルのアドレッシング | 201304L |
<utility> <tuple> | (C++14) |
__cpp_lib_type_trait_variable_templates |
型特性の変数テンプレート (std::is_void_v など) | 201510L |
<type_traits> | (C++17) |
__cpp_lib_uncaught_exceptions |
std::uncaught_exceptions | 201411L |
<exception> | (C++17) |
__cpp_lib_unordered_map_try_emplace |
std::unordered_map::try_emplace、 std::unordered_map::insert_or_assign | 201411L |
<unordered_map> | (C++17) |
__cpp_lib_variant |
std::variant: C++17 のための型安全な共用体 | 201606L |
<variant> | (C++17) |
__cpp_lib_void_t |
std::void_t | 201411L |
<type_traits> | (C++17) |
例
Run this code
#ifdef __has_include // __has_include が存在するかどうかチェック。
# if __has_include(<optional>) // 標準ライブラリに対するチェック。
# include <optional>
# elif __has_include(<experimental/optional>) // 実験的なバージョンに対するチェック。
# include <experimental/optional>
# elif __has_include(<boost/optional.hpp>) // 外部のライブラリを試す。
# include <boost/optional.hpp>
# else // まったく何も見付からなかった。
# error "Missing <optional>"
# endif
#endif
#ifdef __has_cpp_attribute // __has_cpp_attribute が存在するかどうかチェック。
# define DEPRECATED(msg) [[deprecated(msg)]]
# endif
#endif
#ifndef DEPRECATED
# define DEPRECATED(msg)
#endif
DEPRECATED("foo() has been deprecated") void foo();
#if __cpp_constexpr >= 201304 // 機能の特定のバージョンに対するチェック。
# define CONSTEXPR constexpr
#else
# define CONSTEXPR inline
#endif
CONSTEXPR int bar(unsigned i)
{
#if __cpp_binary_literals // 機能の存在に対するチェック。
unsigned mask1 = 0b11000000;
unsigned mask2 = 0b00000111;
#else
unsigned mask1 = 0xC0;
unsigned mask2 = 0x07;
#endif
if ( i & mask1 )
return 1;
if ( i & mask2 )
return 2;
return 0;
}
int main()
{
}
関連項目
| Standing Document 6 | 機能テスト勧告の公式のドキュメント |