名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | types
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
型サポート
型の性質
(C++11)
(C++11)
(C++14)
(C++11)
(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++11)
(C++17)
(C++11)(C++20未満)(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) std::is_constructible<T>::valuetrue であれば、 true に等しいメンバ定数 value が提供されます。 そうでなければ、 valuefalse です。
2) std::is_trivially_constructible<T>::valuetrue であれば、 true に等しいメンバ定数 value が提供されます。 そうでなければ、 valuefalse です。
3) std::is_nothrow_constructible<T>::valuetrue であれば、 true に等しいメンバ定数 value が提供されます。 そうでなければ、 valuefalse です。

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
[静的]
T がデフォルト構築可能ならば true、そうでなければ 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> {};

[編集] ノート

多くの処理系では、 is_nothrow_default_constructible は、実質的に noexcept(T()) であるため、デストラクタが例外を投げるかどうかも確認します。 同じことが is_trivially_default_constructible にも適用され、これらの処理系では、デストラクタがトリビアルであることも要求されます。 GCC bug 51452LWG 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 <iostream>
#include <type_traits>
 
struct Ex1 {
    std::string str; // member has a non-trivial default ctor
};
struct Ex2 {
    int n;
    Ex2() = default; // trivial and non-throwing
};
 
int main() {
    std::cout << std::boolalpha << "Ex1 is default-constructible? "
              << std::is_default_constructible<Ex1>::value << '\n'
              << "Ex1 is trivially default-constructible? "
              << std::is_trivially_default_constructible<Ex1>::value << '\n'
              << "Ex2 is trivially default-constructible? "
              << std::is_trivially_default_constructible<Ex2>::value << '\n'
              << "Ex2 is nothrow default-constructible? "
              << std::is_nothrow_default_constructible<Ex2>::value << '\n';
}

出力:

Ex1 is default-constructible? true
Ex1 is trivially default-constructible? false
Ex2 is trivially default-constructible? true
Ex2 is nothrow default-constructible? true

[編集] 関連項目

型が特定の引数に対するコンストラクタを持っているかどうか調べます
(クラステンプレート) [edit]
型がコピーコンストラクタを持っているかどうか調べます
(クラステンプレート) [edit]
型が右辺値参照から構築できるかどうか調べます
(クラステンプレート) [edit]