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

履歴 編集

class template
<type_traits>

std::is_nothrow_default_constructible(C++11)

namespace std {
  template <class T>
  struct is_nothrow_default_constructible;

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

概要

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

要件

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

効果

is_nothrow_default_constructibleは、型Tが例外を投げない保証のもとにデフォルト構築可能であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

is_nothrow_constructible<T>::value == trueの場合に、例外を投げないデフォルト構築が可能であると判断される。

#include <type_traits>

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

struct t {
  t() = delete;
};

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

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

static_assert(std::is_nothrow_default_constructible<unsigned>::value == true, "unsigned is nothrow default constructible");
static_assert(std::is_nothrow_default_constructible<const int>::value == true, "const int is nothrow default constructible");
static_assert(std::is_nothrow_default_constructible<int[1]>::value == true, "int[1] is not nothrow default constructible");
static_assert(std::is_nothrow_default_constructible<void*>::value == true, "void* is nothrow default constructible");

static_assert(std::is_nothrow_default_constructible<t>::value == false, "t is not nothrow default constructible");
static_assert(std::is_nothrow_default_constructible<int&>::value == false, "int& is not nothrow default constructible");
static_assert(std::is_nothrow_default_constructible<int[]>::value == false, "int[] is not nothrow default constructible");
static_assert(std::is_nothrow_default_constructible<void>::value == false, "void is not nothrow default constructible");

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • GCC: 4.8.0
  • Visual C++: 2012, 2013, 2015
    • 2012~2013には、提案時の名前であるhas_nothrow_constructor, has_nothrow_default_constructorも存在する。
    • 2012はvoidにおいて、誤ってfalse_typeになっている。
    • 2013まではstd::is_default_constructible<int[]>のような要素数の指定がない配列型において、誤ってfalse_typeになっている。

参照