std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T, class... Args > struct is_constructible; |
(1) | (C++11以降) |
| template< class T, class... Args > struct is_trivially_constructible; |
(2) | (C++11以降) |
| template< class T, class... Args > struct is_nothrow_constructible; |
(3) | (C++11以降) |
1)
このチェックの目的上、変数定義は関数宣言として解釈されることはなく、std::declval の使用は odr-use とみなされません。アクセスチェックは、
T がオブジェクト型または参照型であり、変数定義 T obj(std::declval<Args>()...); がwell-formedである場合、メンバ定数value を true とします。それ以外の場合、value は false となります。このチェックの目的上、変数定義は関数宣言として解釈されることはなく、std::declval の使用は odr-use とみなされません。アクセスチェックは、
T および Args の型とは無関係なコンテキストから行われるものとみなされます。変数定義の直接的なコンテキストの有効性のみが考慮されます。3) (1) と同じですが、変数定義は
noexcept です。T またはパラメータパック Args のいずれかの型が不完全型、( cv修飾されている可能性のある)void、または未知の境界を持つ配列である場合、動作は未定義です。
上記のテンプレートのインスタンス化が、直接的または間接的に不完全な型に依存し、その型が仮に完全になった場合にそのインスタンス化が異なる結果を生み出す可能性がある場合、動作は未定義です。
プログラムがこのページで説明されているテンプレートのいずれかに特殊化を追加する場合、動作は未定義です。
目次 |
[編集] ヘルパー変数テンプレート
| template< class T, class... Args > inline constexpr bool is_constructible_v = |
(C++17以降) | |
| template< class T, class... Args > inline constexpr bool is_trivially_constructible_v = |
(C++17以降) | |
| template< class T, class... Args > inline constexpr bool is_nothrow_constructible_v = |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
T が Args... から構築可能であれば true、そうでなければ false(公開静的メンバ定数) |
メンバ関数
| operator bool |
オブジェクトを bool に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[編集] 注釈
多くの実装では、is_nothrow_constructible はデストラクタが例外を投げないかもチェックします。これは実質的に noexcept(T(arg)) と同等です。is_trivially_constructible も同様で、これらの実装ではデストラクタが自明であることも要求されます。GCC bug 51452 LWG issue 2116。
[編集] 例
このコードを実行
#include <iostream> #include <type_traits> class Foo { int v1; double v2; public: Foo(int n) : v1(n), v2() {} Foo(int n, double f) noexcept : v1(n), v2(f) {} }; int main() { auto is = [](bool o) { return (o ? "\t" "is " : "\t" "isn't "); }; std::cout << "Foo ...\n" << is(std::is_trivially_constructible_v<Foo, const Foo&>) << "Trivially-constructible from const Foo&\n" << is(std::is_trivially_constructible_v<Foo, int>) << "Trivially-constructible from int\n" << is(std::is_constructible_v<Foo, int>) << "Constructible from int\n" << is(std::is_nothrow_constructible_v<Foo, int>) << "Nothrow-constructible from int\n" << is(std::is_nothrow_constructible_v<Foo, int, double>) << "Nothrow-constructible from int and double\n"; }
出力
Foo ...
is Trivially-constructible from const Foo&
isn't Trivially-constructible from int
is Constructible from int
isn't Nothrow-constructible from int
is Nothrow-constructible from int and double[編集] 関連項目
| 型がデフォルトコンストラクタを持つかをチェックする (クラステンプレート) | |
| (C++11)(C++11)(C++11) |
型がコピーコンストラクタを持つかをチェックする (クラステンプレート) |
| (C++11)(C++11)(C++11) |
型が右辺値参照から構築可能であるかをチェックする (クラステンプレート) |
| (C++20) |
その型の変数が、一連の引数型から構築可能、または束縛可能であることを規定する (コンセプト) |