std::is_copy_constructible, std::is_trivially_copy_constructible, std::is_nothrow_copy_constructible
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct is_copy_constructible; |
(1) | (C++11以降) |
| template< class T > struct is_trivially_copy_constructible; |
(2) | (C++11以降) |
| template< class T > struct is_nothrow_copy_constructible; |
(3) | (C++11以降) |
| 型特性 | メンバ定数valueの値 | |
|---|---|---|
Tは参照可能な型 |
Tは参照可能な型ではない | |
| (1) | std::is_constructible<T, const T&>::value | false |
| (2) | std::is_trivially_constructible<T, const T&>::value | |
| (3) | std::is_nothrow_constructible<T, const T&>::value | |
T が不完全型、(cv修飾されている可能性のある)void、または不明な境界を持つ配列の場合、動作は未定義です。
上記のテンプレートのインスタンス化が、直接的または間接的に不完全な型に依存し、その型が仮に完全になった場合にそのインスタンス化が異なる結果を生み出す可能性がある場合、動作は未定義です。
プログラムがこのページで説明されているテンプレートのいずれかに特殊化を追加する場合、動作は未定義です。
目次 |
[編集] ヘルパー変数テンプレート
| template< class T > inline constexpr bool is_copy_constructible_v = |
(C++17以降) | |
| template< class T > inline constexpr bool is_trivially_copy_constructible_v = |
(C++17以降) | |
| template< class T > inline constexpr bool is_nothrow_copy_constructible_v = |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
true if T is copy-constructible, false otherwise(公開静的メンバ定数) |
メンバ関数
| operator bool |
オブジェクトを bool に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[編集] 実装例
template<class T> struct is_copy_constructible : std::is_constructible<T, typename std::add_lvalue_reference< typename std::add_const<T>::type>::type> {}; template<class T> struct is_trivially_copy_constructible : std::is_trivially_constructible<T, typename std::add_lvalue_reference< typename std::add_const<T>::type>::type> {}; template<class T> struct is_nothrow_copy_constructible : std::is_nothrow_constructible<T, typename std::add_lvalue_reference< typename std::add_const<T>::type>::type> {}; |
[編集] 注記
In many implementations, is_nothrow_copy_constructible also checks if the destructor throws because it is effectively noexcept(T(arg)). Same applies to is_trivially_copy_constructible, which, in these implementations, also requires that the destructor is trivial: GCC bug 51452, LWG issue 2116.
[編集] 例
このコードを実行
#include <string> #include <type_traits> struct S1 { std::string str; // member has a non-trivial copy constructor }; static_assert(std::is_copy_constructible_v<S1>); static_assert(!std::is_trivially_copy_constructible_v<S1>); struct S2 { int n; S2(const S2&) = default; // trivial and non-throwing }; static_assert(std::is_trivially_copy_constructible_v<S2>); static_assert(std::is_nothrow_copy_constructible_v<S2>); struct S3 { S3(const S3&) = delete; // explicitly deleted }; static_assert(!std::is_copy_constructible_v<S3>); struct S4 { S4(S4&) {}; // cannot bind const, hence not a copy-constructible }; static_assert(!std::is_copy_constructible_v<S4>); int main() {}
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2196 | C++11 | the behavior was unclear if const T& cannot be formed | この場合、生成される値はfalseである |
[編集] 関連項目
| (C++11)(C++11)(C++11) |
型が特定の引数に対するコンストラクタを持つかをチェックする (クラステンプレート) |
| 型がデフォルトコンストラクタを持つかをチェックする (クラステンプレート) | |
| (C++11)(C++11)(C++11) |
型が右辺値参照から構築可能であるかをチェックする (クラステンプレート) |
| (C++20) |
その型のオブジェクトがコピー構築およびムーブ構築可能であることを規定する (コンセプト) |