名前空間
変種
操作

std::is_constructible, std::is_trivially_constructible, std::is_nothrow_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)
サポートされている操作
関係とプロパティクエリ
型の変更
(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, 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>()...); がwell-formedである場合、メンバ定数valuetrue とします。それ以外の場合、valuefalse となります。
このチェックの目的上、変数定義は関数宣言として解釈されることはなく、std::declval の使用は odr-use とみなされません。アクセスチェックは、T および Args の型とは無関係なコンテキストから行われるものとみなされます。変数定義の直接的なコンテキストの有効性のみが考慮されます。
2) (1) と同じですが、変数定義では自明ではない操作は呼び出されません。このチェックの目的上、std::declval への呼び出しは自明とみなされます。
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]
TArgs... から構築可能であれば 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>

[編集] 注釈

多くの実装では、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

[編集] 関連項目

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