namespace std {
template <class T, class... Args>
struct is_constructible;
template <class T, class... Args>
inline constexpr bool is_constructible_v
= is_constructible<T, Args...>::value; // C++17
}
概要
型T
のコンストラクタ呼出しが適格か調べる。T( Args... )
の形式のコンストラクタ呼び出しが適格であるか。
要件
型T
および、Args...
の全ての型は、完全型でなければならない。
効果
is_constructible
は、T( Args... )
の形式のコンストラクタ呼出しが適格であるならばtrue_type
から派生し、そうでなければfalse_type
から派生する。- C++17 : 型
T
がvoid(int, char)
のような関数型、もしくは(CV修飾された)void
である場合、false_type
から派生する。
例
#include <type_traits>
struct s {
explicit s(int) {} // intを引数にとるコンストラクタ
};
static_assert(std::is_constructible<s, int>::value == true, "value == true, s(int) is constructible");
static_assert(std::is_same<std::is_constructible<s, int>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_constructible<s, int>::type, std::true_type>::value, "type == true_type");
static_assert(std::is_constructible<s, int>() == true, "is_constructible<s, int>() == true");
static_assert(std::is_constructible<s, int*>::value == false, "value == false, s(int*) is not constructible");
static_assert(std::is_same<std::is_constructible<s, int*>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_constructible<s, int*>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_constructible<s, int*>() == false, "is_constructible<s, int*>() == false");
int main(){}
出力
バージョン
言語
- C++11
処理系
- GCC: 4.3.4 ✅, 4.5.3 ✅, 4.6.2 ✅, 4.7.0 ✅
- Visual C++: 2012 ✅, 2013 ✅, 2015 ✅
- 2012までは、可変引数テンプレートに対応していないため、不完全な実装である。
- 2012~2013は、不具合がある模様。
備考
上の例でコンパイラによってはエラーになる。GCC 4.3.4, 4.5.3, Visual C++ 11.0 は integral_constant
が operator bool()
を持っていないためエラーになる。