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

履歴 編集

class template
<type_traits>

std::is_nothrow_constructible(C++11)

namespace std {
  template <class T, class... Args>
  struct is_nothrow_constructible;

  template <class T, class... Args>
  inline constexpr bool is_nothrow_constructible_v
    = is_nothrow_constructible<T, Args...>::value; // C++17
}

概要

Tのコンストラクタ呼出しが適格であり、かつそのコンストラクタが例外を投げないか調べる。 T( Args... )の形式のコンストラクタ呼出しが適格かつ例外を投げないか。

要件

TおよびパラメータパックArgs...のすべての型は完全型であるか、const/volatile修飾された(あるいはされていない)voidか、要素数不明の配列型でなければならない。

効果

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

is_constructible<T, Args...>::value == trueかつ、いかなる例外を投げないならば、例外を投げない構築が可能であると判断される。

#include <type_traits>
#include <exception>

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

static_assert(std::is_nothrow_constructible<int&>::value == false, "value == false, ctor int&() is not nothrow constructible");
static_assert(std::is_same<std::is_nothrow_constructible<int&>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_nothrow_constructible<int&>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_nothrow_constructible<int&>() == false, "is_nothrow_constructible<int&>() == false");

static_assert(std::is_nothrow_constructible<int, int>::value == true, "ctor int(int) is nothrow constructible");
static_assert(std::is_nothrow_constructible<int&, int&>::value == true, "ctor int&(int&) is nothrow constructible");
static_assert(std::is_nothrow_constructible<int[1]>::value == true, "ctor int[1]() is nothrow constructible");
static_assert(std::is_nothrow_constructible<std::exception>::value == true, "ctor exceptiion() is nothrow constructible");

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

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • GCC: 4.7.0, 4.8.0
  • Visual C++: 2012, 2013, 2015
    • 2012は、可変引数テンプレートに対応していないため、不完全な実装である。

参照