std:: is_constructible, std:: is_trivially_constructible, std:: is_nothrow_constructible
|
ヘッダーで定義
<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 以降) |
T
がオブジェクト型または参照型であり、変数定義
T obj
(
std::
declval
<
Args
>
(
)
...
)
;
が適正な形式である場合、メンバー定数
value
を
true
に設定する。それ以外の場合、
value
は
false
となる。
このチェックにおいて、変数定義は関数宣言として解釈されず、 std::declval の使用は ODR-use とは見なされない。 アクセスチェック は、
T
および
Args
内のいずれの型とも無関係なコンテキストから実行されるものとして扱われる。変数定義の直接的なコンテキストの有効性のみが考慮される。
noexcept
です。
T
またはパラメータパック内のいずれかの型
Args
が不完全型、(場合によってはcv修飾された)
void
、または未知の境界を持つ配列である場合、動作は未定義です。
上記のテンプレートのインスタンス化が、直接的または間接的に不完全型に依存しており、その型が仮に完全化された場合に異なる結果をもたらす可能性がある場合、その動作は未定義です。
プログラムがこのページで説明されているテンプレートのいずれかに対する特殊化を追加する場合、動作は未定義です。
目次 |
ヘルパー変数テンプレート
|
template
<
class
T,
class
...
Args
>
inline
constexpr
bool
is_constructible_v
=
|
(C++17以降) | |
|
template
<
class
T,
class
...
Args
>
inline
constexpr
bool
is_trivially_constructible_v
=
|
(C++17以降) | |
|
template
<
class
T,
class
...
Args
>
inline
constexpr
bool
is_nothrow_constructible_v
=
|
(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
関連項目
|
(C++11)
(C++11)
(C++11)
|
型がデフォルトコンストラクタを持つかどうかをチェックする
(クラステンプレート) |
|
(C++11)
(C++11)
(C++11)
|
型がコピーコンストラクタを持つかどうかをチェックする
(クラステンプレート) |
|
(C++11)
(C++11)
(C++11)
|
型が右辺値参照から構築可能かどうかをチェックする
(クラステンプレート) |
|
(C++20)
|
その型の変数が引数型のセットから構築可能、またはバインド可能であることを指定する
(コンセプト) |