namespace std {
template <class T>
struct is_copy_constructible;
template <class T>
inline constexpr bool is_copy_constructible_v
= is_copy_constructible<T>::value; // C++17
}
概要
型Tがコピー構築可能か調べる
要件
型Tは完全型であるか、const/volatile修飾された(あるいはされていない)voidか、要素数不明の配列型でなければならない。
効果
is_copy_constructibleは、型Tがコピー構築可能であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。
以下の条件がtrueである場合に、コピー構築可能であると見なされる:
- C++11 :
is_constructible<T, const T&>::value == true - C++14 : 参照可能な型
Tに対しては、is_constructible<T, const T&>::value == trueと同じ結果となり、それ以外はfalseと見なされる。
例
#include <type_traits>
struct s {
s(const s&) = delete;
// コピーコンストラクタは = delete されている。
// そのためコピーコンストラクトできない。
};
static_assert(std::is_copy_constructible<int>::value == true, "value == true, int is copy constructible");
static_assert(std::is_same<std::is_copy_constructible<int>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_copy_constructible<int>::type, std::true_type>::value, "type == true_type");
static_assert(std::is_copy_constructible<int>() == true, "is_copy_constructible<int>() == true");
static_assert(std::is_copy_constructible<s>::value == false, "value == false, s is not copy constructible");
static_assert(std::is_same<std::is_copy_constructible<s>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_copy_constructible<s>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_copy_constructible<s>() == false, "is_copy_constructible<s>() == false");
static_assert(std::is_copy_constructible<double>::value == true, "double is copy constructible");
static_assert(std::is_copy_constructible<const int>::value == true, "const int is copy constructible");
static_assert(std::is_copy_constructible<void*>::value == true, "void* is copy constructible");
static_assert(std::is_copy_constructible<int&>::value == true, "int& is copy constructible");
static_assert(std::is_copy_constructible<int[1]>::value == false, "int[1] is not copy constructible");
static_assert(std::is_copy_constructible<int[]>::value == false, "int[] is not copy constructible");
static_assert(std::is_copy_constructible<void>::value == false, "void is not copy constructible");
static_assert(std::is_copy_constructible<int&&>::value == false, "int&& is not copy constructible");
static_assert(std::is_copy_constructible<int ()>::value == false, "int () is not copy constructible");
int main(){}
出力
バージョン
言語
- C++11
処理系
- GCC: 4.7.0 ✅
- Visual C++: 2012 ✅, 2013 ✅, 2015 ✅
- 2012~2013には、提案時の名前である
has_copy_constructorも存在する。 - 2012は、
is_copy_constructible<void>が誤ってtrue_typeになっている。 - 2012~2013は、C++11の定義に基づく実装となっている。
- 2012~2013は、上記例のうち
is_copy_constructible<s>に関するものにおいて、誤った結果になる。これは、is_constructibleの不具合に由来する。 - 2012は、左辺値参照型において
true_typeになっている。具体的には、上記例のうちis_copy_constructible<int&>が該当する。
- 2012~2013は、上記例のうち
- 2012~2013には、提案時の名前である
参照
- N2983 Allowing Move Constructors to Throw
- LWG Issue 2196. Specification of
is_*[copy/move]_[constructible/assignable]unclear for non-referencable types- C++11では、この型特性が参照型に対してどのような振る舞いになるのか不明確であったため、C++14で明確化された。
- P0006R0 Adopt Type Traits Variable Templates from Library Fundamentals TS for C++17