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

履歴 編集

class template
<type_traits>

std::is_copy_constructible(C++11)

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と見なされる。
    • 参照可能な型とは、以下のいずれかの条件に合致する型である:
      • オブジェクト型
      • CV修飾されていない、もしくは参照修飾されていない関数型
      • 参照修飾されている型

#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&>が該当する。

参照