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

履歴 編集

class template
<type_traits>

std::is_nothrow_move_constructible(C++11)

namespace std {
  template <class T>
  struct is_nothrow_move_constructible;

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

概要

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

これを使用することにより、特定の条件を満たすことで、例外を投げない強い保証をする関数・クラスを実装できる。

要件

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

効果

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

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

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

#include <type_traits>

// nothrow move constructible
struct A {};

// nothrow move constructible
struct B {
  B(B&&) noexcept {}
};

// not nothrow move constructible
struct C {
  C(C&&) {}
};

// not nothrow move constructible
struct D {
  D(D&&) noexcept(false) {}
};

static_assert(
  std::is_nothrow_move_constructible<int>::value == true,
  "int is nothrow move constructible");

static_assert(
  std::is_nothrow_move_constructible<A>::value == true,
  "A is nothrow move constructible");

static_assert(
  std::is_nothrow_move_constructible<B>::value == true,
  "B is nothrow move constructible");

static_assert(
  std::is_nothrow_move_constructible<C>::value == false,
  "C isn't nothrow move constructible");

static_assert(
  std::is_nothrow_move_constructible<D>::value == false,
  "D isn't nothrow move constructible");

int main() {}

出力

バージョン

言語

  • C++11

処理系

  • Clang: 3.0
  • GCC: 4.7.3
  • Visual C++: 2012, 2013, 2015
    • 2012は、すべてのクラス型においてtrue_typeになるかのような挙動を示した。上記例のクラスCの場合に、true_typeとなった。

関連項目

参照