Namespaces
Variants

std:: is_constructible, std:: is_trivially_constructible, std:: is_nothrow_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
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, 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) T がオブジェクト型または参照型であり、変数定義 T obj ( std:: declval < Args > ( ) ... ) ; が適正な形式である場合、メンバー定数 value true に設定する。それ以外の場合、 value false となる。
このチェックにおいて、変数定義は関数宣言として解釈されず、 std::declval の使用は ODR-use とは見なされない。 アクセスチェック は、 T および Args 内のいずれの型とも無関係なコンテキストから実行されるものとして扱われる。変数定義の直接的なコンテキストの有効性のみが考慮される。
2) (1) と同様ですが、変数定義がtrivialでない操作を呼び出さない点が異なります。このチェックの目的において、 std::declval の呼び出しはtrivialであると見なされます。
3) (1) と同じですが、変数定義が noexcept です。

T またはパラメータパック内のいずれかの型 Args が不完全型、(場合によってはcv修飾された) void 、または未知の境界を持つ配列である場合、動作は未定義です。

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

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

目次

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

template < class T, class ... Args >

inline constexpr bool is_constructible_v =

is_constructible < T, Args... > :: value ;
(C++17以降)
template < class T, class ... Args >

inline constexpr bool is_trivially_constructible_v =

is_trivially_constructible < T, Args... > :: value ;
(C++17以降)
template < class T, class ... Args >

inline constexpr bool is_nothrow_constructible_v =

is_nothrow_constructible < T, Args... > :: value ;
(C++17以降)

std:: integral_constant から継承

メンバ定数

value
[static]
true T Args... から構築可能な場合)、 false (それ以外の場合)
(public static member constant)

メンバ関数

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

関連項目

型がデフォルトコンストラクタを持つかどうかをチェックする
(クラステンプレート)
型がコピーコンストラクタを持つかどうかをチェックする
(クラステンプレート)
型が右辺値参照から構築可能かどうかをチェックする
(クラステンプレート)
その型の変数が引数型のセットから構築可能、またはバインド可能であることを指定する
(コンセプト)