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

履歴 編集

class template
<type_traits>

std::is_nothrow_copy_constructible(C++11)

namespace std {
  template <class T>
  struct is_nothrow_copy_constructible;

  template <class T>
  inline constexpr bool is_nothrow_copy_constructible_v
    = is_nothrow_copy_constructible<T>::value;          // C++17
}

概要

Tがコピー構築でき、かつそのコピーコンストラクタが例外を投げないか調べる

要件

Tは完全型であるか、const/volatile修飾された(あるいはされていない)voidか、要素数不明の配列型でなければならない。

効果

is_nothrow_copy_constructibleは、型Tが例外を投げない保証のもとにコピー構築可能であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

以下の条件がtrueである場合に、例外を投げないコピー構築が可能であると見なされる:

  • C++11 : is_nothrow_constructible<T, const T&>::value == true
  • C++14 : 参照可能な型Tに対しては、is_nothrow_constructible<T, const T&>::value == trueと同じ結果となり、それ以外はfalseと見なされる。
    • 参照可能な型とは、以下のいずれかの条件に合致する型である:
      • オブジェクト型
      • CV修飾されていない、もしくは参照修飾されていない関数型
      • 参照修飾されている型

is_nothrow_constructible<T, T&>::valueでは判断できないので注意。

#include <type_traits>

struct s {
  s(const s&) {
    throw 0;
  }
};

struct t {
  t(const t&) = delete;
  t(t&&) = default;
};

struct u {
  u(const u&) = default;
  u(u&&) = delete;
};

static_assert(std::is_nothrow_copy_constructible<int>::value == true, "value == true, int is nothrow copy constructible");
static_assert(std::is_same<std::is_nothrow_copy_constructible<int>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_nothrow_copy_constructible<int>::type, std::true_type>::value, "type == true_type");
static_assert(std::is_nothrow_copy_constructible<int>() == true, "is_nothrow_copy_constructible<int>() == true");

static_assert(std::is_nothrow_copy_constructible<s>::value == false, "value == false, s is not nothrow copy constructible");
static_assert(std::is_same<std::is_nothrow_copy_constructible<s>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_nothrow_copy_constructible<s>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_nothrow_copy_constructible<s>() == false, "is_nothrow_copy_constructible<s>() == false");

static_assert(std::is_nothrow_copy_constructible<unsigned>::value == true, "unsigned is nothrow copy constructible");
static_assert(std::is_nothrow_copy_constructible<const int>::value == true, "const int is nothrow copy constructible");
static_assert(std::is_nothrow_copy_constructible<int&>::value == true, "int& is nothrow copy constructible");
static_assert(std::is_nothrow_copy_constructible<void*>::value == true, "void* is nothrow copy constructible");
static_assert(std::is_nothrow_copy_constructible<u>::value == true, "u is nothrow copy constructible");

static_assert(std::is_nothrow_copy_constructible<t>::value == false, "t is not nothrow copy constructible");
static_assert(std::is_nothrow_copy_constructible<int[]>::value == false, "int[] is not nothrow copy constructible");
static_assert(std::is_nothrow_copy_constructible<int[1]>::value == false, "int[1] is not nothrow copy constructible");
static_assert(std::is_nothrow_copy_constructible<void>::value == false, "void is not nothrow copy constructible");

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • GCC: 4.7.0, 4.8.0
  • Visual C++: 2012, 2013, 2015
    • 2012~2013には、提案時の名前であるhas_nothrow_copy, has_nothrow_copy_constructorも存在する。
    • 2012は、すべてのクラス型においてtrue_typeになるかのような挙動を示した。上記例のうちs, t, utrue_typeとなった。ただし、2013で非対応の構文については適宜変更の上で試した。
    • 2013は、C++11に準拠する実装となっている。

参照