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

履歴 編集

class template
<type_traits>

std::is_move_constructible(C++11)

namespace std {
  template <class T>
  struct is_move_constructible;

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

概要

Tがムーブ構築可能か調べる

要件

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

効果

is_move_constructibleは、型Tがムーブ構築可能であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

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

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

#include <type_traits>

struct s {
  s(s&&) = delete;
  // ムーブコンストラクタは = delete されている。
  // そのためムーブコンストラクトできない。
};

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

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

static_assert(std::is_move_constructible<double>::value == true, "double is move constructible");
static_assert(std::is_move_constructible<const int>::value == true, "const int is move constructible");
static_assert(std::is_move_constructible<void*>::value == true, "void* is move constructible");
static_assert(std::is_move_constructible<int&>::value == true, "int& is move constructible");
static_assert(std::is_move_constructible<int&&>::value == true, "int&& is move constructible");

static_assert(std::is_move_constructible<int[1]>::value == false, "int[1] is not move constructible");
static_assert(std::is_move_constructible<int[]>::value == false, "int[] is not move constructible");
static_assert(std::is_move_constructible<void>::value == false, "void is not move constructible");
static_assert(std::is_move_constructible<int ()>::value == false, "int () is not move constructible");

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • GCC: 4.7.0
  • Visual C++: 2012, 2013, 2015
    • 2012~2013には、提案時の名前であるhas_move_constructorも存在する。
    • 2012は、誤ってvoidと参照型においてtrue_typeになっている。上記例のうち、std::is_move_constructible<void>, std::is_move_constructible<int&>, std::is_move_constructible<int&&>が該当する。
    • 2012~2013は、上記例のうちis_move_constructible<s>に関するものにおいて、誤った結果になる。これは、is_constructibleの不具合に由来する。

参照