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

履歴 編集

class template
<type_traits>

std::is_destructible(C++11)

namespace std {
  template <class T>
  struct is_destructible;

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

概要

Tが破棄可能か調べる

要件

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

効果

is_destructibleは、型Tが破棄可能であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

  • C++11 : 型Tが完全型で template <class U> struct test { U u; }; があるときに test<T>::~test()delete宣言されていなければ、型Tは破棄可能であると判断される。
  • C++14 : 実行時に評価されない文脈で、オブジェクト型Tに対する式std::declval<T&>().~T()が有効であれば破棄可能、そうでなければ破棄できないと判断される。
    • オブジェクト型に含まれない型の場合の判断は以下:
      • Tvoidの場合は破棄できない
      • Tが参照型の場合は破棄可能
      • Tが関数型の場合は破棄できない
    • また、配列型は~T()という式が有効でないため、破棄できない

#include <type_traits>
#include <locale>

struct s
{
  ~s() = delete;

  // デストラクタは = delete されている。
  // そのためデストラクトできない。
};

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

static_assert(std::is_destructible<s>::value == false, "value == false, s is not destructible");
static_assert(std::is_same<std::is_destructible<s>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_destructible<s>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_destructible<s>() == false, "is_destructible<int>() == false");

static_assert(std::is_destructible<const int>::value == true, "const int is destructible");
static_assert(std::is_destructible<int *>::value == true, "int * is destructible");
static_assert(std::is_destructible<long>::value == true, "long is destructible");
static_assert(std::is_destructible<int[1]>::value == true, "int[1] is destructible");

static_assert(std::is_destructible<int[]>::value == false, "int[] is not destructible");
static_assert(std::is_destructible<void>::value == false, "void is not destructible");
static_assert(std::is_destructible<std::locale::facet>::value == false, "std::locale::facet is not destructible");
static_assert(std::is_destructible<std::ctype<char>>::value == false, "std::ctype<char> is not destructible");

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • Clang 3.1, 3.2, 3.3, 3.4(revision 188080)
  • GCC: 4.7.3, 4.8.0, 4.8.1
  • Visual C++: 2012, 2013, 2015
    • 2012~2013は、delete定義されたデストラクタを持つクラスにおいて、誤ってstd::true_typeになっている。

備考

Clang 3.1 - 3.3 では以下のようなエラーが出るが、これはClang付属のlibc++のバグである。

prog.cc:27:1: error: static_assert failed "int[] is not destructible"
static_assert(std::is_destructible<int[]>::value == false, "int[] is not destructible");
^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

revision 188080以降のClang 3.4ならばエラーが出ない。

参照