std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct is_default_constructible; |
(1) | (C++11以降) |
| template< class T > struct is_trivially_default_constructible; |
(2) | (C++11以降) |
| template< class T > struct is_nothrow_default_constructible; |
(3) | (C++11以降) |
T が不完全型、(cv修飾されている可能性のある)void、または不明な境界を持つ配列の場合、動作は未定義です。
上記のテンプレートのインスタンス化が、直接的または間接的に不完全な型に依存し、その型が仮に完全になった場合にそのインスタンス化が異なる結果を生み出す可能性がある場合、動作は未定義です。
プログラムがこのページで説明されているテンプレートのいずれかに特殊化を追加する場合、動作は未定義です。
目次 |
[edit] ヘルパー変数テンプレート
| template< class T > inline constexpr bool is_default_constructible_v = |
(C++17以降) | |
| template< class T > inline constexpr bool is_trivially_default_constructible_v = |
(C++17以降) | |
| template< class T > inline constexpr bool is_nothrow_default_constructible_v = |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
T がデフォルト構築可能であれば 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> |
[edit] 実装例
template<class T> struct is_default_constructible : std::is_constructible<T> {}; template<class T> struct is_trivially_default_constructible : std::is_trivially_constructible<T> {}; template<class T> struct is_nothrow_default_constructible : std::is_nothrow_constructible<T> {}; |
[edit] 注釈
多くの実装では、std::is_nothrow_default_constructible はデストラクタが例外を投げないかどうかもチェックします。これは実質的に noexcept(T()) と同等であるためです。std::is_trivially_default_constructible も同様で、これらの実装ではデストラクタがトリビアルであることも要求されます: GCC bug 51452, LWG issue 2116。
std::is_default_constructible<T> は T x; がコンパイルされることをテストするのではなく、引数リストが空の直接初期化を試みます(std::is_constructible を参照)。したがって、std::is_default_constructible_v<const int> および std::is_default_constructible_v<const int[10]> は true です。
[edit] 例
#include <string> #include <type_traits> struct S1 { std::string str; // member has a non-trivial default constructor }; static_assert(std::is_default_constructible_v<S1> == true); static_assert(std::is_trivially_default_constructible_v<S1> == false); struct S2 { int n; S2() = default; // trivial and non-throwing }; static_assert(std::is_trivially_default_constructible_v<S2> == true); static_assert(std::is_nothrow_default_constructible_v<S2> == true); int main() {}
[edit] 関連項目
| (C++11)(C++11)(C++11) |
型が特定の引数に対するコンストラクタを持つかをチェックする (クラステンプレート) |
| (C++11)(C++11)(C++11) |
型がコピーコンストラクタを持つかをチェックする (クラステンプレート) |
| (C++11)(C++11)(C++11) |
型が右辺値参照から構築可能であるかをチェックする (クラステンプレート) |
| (C++20) |
その型のオブジェクトがデフォルト構築可能であることを規定する (コンセプト) |