最終更新日時(UTC):
が更新

履歴 編集

class template
<type_traits>

std::is_constructible(C++11)

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 : 型Tvoid(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_constantoperator bool() を持っていないためエラーになる。

参照