Namespaces
Variants

std:: is_default_constructible, std:: is_trivially_default_constructible, std:: is_nothrow_default_constructible

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
is_default_constructible is_trivially_default_constructible is_nothrow_default_constructible
(C++11) (C++11) (C++11)
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <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以降)
1) メンバー定数 value を提供します。これは std:: is_constructible < T > :: value と等しい値を持ちます。
2) メンバ定数 value を提供します。これは std:: is_trivially_constructible < T > :: value と等しい値を持ちます。
3) メンバー定数 value を提供します。これは std:: is_nothrow_constructible < T > :: value と等しい値です。

T が完全型でない場合、(おそらくcv修飾された) void または不明な境界の配列である場合、動作は未定義です。

上記のテンプレートのインスタンス化が、直接的または間接的に不完全型に依存しており、その型が仮に完全化された場合に異なる結果をもたらす可能性がある場合、その動作は未定義です。

プログラムがこのページで説明されているテンプレートのいずれかに対する特殊化を追加する場合、動作は未定義です。

目次

ヘルパー変数テンプレート

template < class T >

inline constexpr bool is_default_constructible_v =

is_default_constructible < T > :: value ;
(C++17以降)
template < class T >

inline constexpr bool is_trivially_default_constructible_v =

is_trivially_default_constructible < T > :: value ;
(C++17以降)
template < class T >

inline constexpr bool is_nothrow_default_constructible_v =

is_nothrow_default_constructible < T > :: value ;
(C++17以降)

std::integral_constantから継承

メンバ定数

value
[static]
true Tがデフォルト構築可能な場合、 false それ以外の場合
(公開静的メンバ定数)

メンバ関数

operator bool
オブジェクトを bool に変換し、 value を返す
(公開メンバ関数)
operator()
(C++14)
value を返す
(公開メンバ関数)

メンバ型

定義
value_type bool
type std:: integral_constant < bool , value >

実装例

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> {};
(注:指定された条件により、HTMLタグ・属性、 タグ内のテキスト、C++固有の用語は翻訳対象外となっています。上記のコードブロック内には翻訳すべき自然言語テキストが含まれていないため、出力は入力と同一となります)

注記

多くの実装では、 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 となります。

#include <string>
#include <type_traits>
struct S1
{
    std::string str; // メンバーが非トリビアルなデフォルトコンストラクタを持つ
};
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; // トリビアルかつ非スロー
};
static_assert(std::is_trivially_default_constructible_v<S2> == true);
static_assert(std::is_nothrow_default_constructible_v<S2> == true);
int main() {}

関連項目

型が特定の引数に対してコンストラクタを持つかどうかをチェックする
(クラステンプレート)
型がコピーコンストラクタを持つかどうかをチェックする
(クラステンプレート)
型が右辺値参照から構築可能かどうかをチェックする
(クラステンプレート)
型のオブジェクトがデフォルト構築可能であることを指定する
(コンセプト)