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

履歴 編集

class template
<type_traits>

std::is_nothrow_copy_assignable(C++11)

namespace std {
  template <class T>
  struct is_nothrow_copy_assignable;

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

概要

Tがコピー代入可能で、かつ代入操作が例外を投げないかを調べる。

要件

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

効果

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

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

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

#include <type_traits>

// nothrow copy assignable
struct A {};

// nothrow copy assignable
struct B {
  B& operator=(const B&) noexcept
  { return *this; }
};

// not copy assignable
struct C {
  C& operator=(const C&)
  { return *this; }
};

// not nothrow copy assignable
struct D {
  D& operator=(const D&) noexcept(false)
  { return *this; }
};

static_assert(
  std::is_nothrow_copy_assignable<int>::value == true,
  "int is nothrow copy assignable");

static_assert(
  std::is_nothrow_copy_assignable<A>::value == true,
  "A is nothrow copy assignable");

static_assert(
  std::is_nothrow_copy_assignable<B>::value == true,
  "B is nothrow copy assignable");

static_assert(
  std::is_nothrow_copy_assignable<C>::value == false,
  "C is not nothrow copy assignable");

static_assert(
  std::is_nothrow_copy_assignable<D>::value == false,
  "D is not nothrow copy assignable");

int main() {}

出力

バージョン

言語

  • C++11

処理系

  • Clang: 3.0
  • GCC: 4.7.3
  • Visual C++: 2012, 2013, 2015
    • 2012~2013には、提案時の名前であるhas_nothrow_assign, has_nothrow_copy_assignも存在する。

参照