名前空間
変種
操作

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

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型のプロパティ
(C++11)
(C++11)
(C++14)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
is_default_constructibleis_trivially_default_constructibleis_nothrow_default_constructible
(C++11)(C++11)(C++11)
関係とプロパティクエリ
型の変更
(C++11)(C++11)(C++11)
型の変換
(C++11)(C++23で非推奨)
(C++11)(C++23で非推奨)
(C++11)
(C++11)(C++20まで*)(C++17)

(C++11)
(C++17)
コンパイル時有理数演算
コンパイル時整数シーケンス
 
ヘッダ <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、または不明な境界を持つ配列の場合、動作は未定義です。

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

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

目次

[edit] ヘルパー変数テンプレート

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]
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] 関連項目

型が特定の引数に対するコンストラクタを持つかをチェックする
(クラステンプレート) [編集]
型がコピーコンストラクタを持つかをチェックする
(クラステンプレート) [編集]
型が右辺値参照から構築可能であるかをチェックする
(クラステンプレート) [編集]
その型のオブジェクトがデフォルト構築可能であることを規定する
(コンセプト) [編集]
English 日本語 中文(简体) 中文(繁體)