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

履歴 編集

class template
<type_traits>

std::is_polymorphic(C++11)

namespace std {
  template <class T>
  struct is_polymorphic;

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

概要

Tが多相的なクラスか調べる

要件

Tが非共用体のクラスである場合、その型は完全型でなければならない。

効果

is_polymorphicは、型Tが多相的クラス (cv修飾を許容する) であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

備考

多相的クラスは、少なくとも一つの仮想関数を持つクラスである。

#include <type_traits>

struct polymorphic_class {
  virtual ~polymorphic_class() {}   // 仮想関数を持つ
};
class non_polymorphic_class{};

static_assert(std::is_polymorphic<polymorphic_class>::value == true, "value == true, polymorphic_class is polymorphic");
static_assert(std::is_same<std::is_polymorphic<polymorphic_class>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_polymorphic<polymorphic_class>::type, std::true_type>::value, "type == true_type");
static_assert(std::is_polymorphic<polymorphic_class>() == true, "is_polymorphic<polymorphic_class>() == true");

static_assert(std::is_polymorphic<non_polymorphic_class>::value == false, "value == false, non_polymorphic_class is not polymorphic");
static_assert(std::is_same<std::is_polymorphic<non_polymorphic_class>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_polymorphic<non_polymorphic_class>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_polymorphic<non_polymorphic_class>() == false, "is_polymorphic<non_polymorphic_class>() == false");

static_assert(std::is_polymorphic<const volatile polymorphic_class>::value == true, "const volatile polymorphic_class is polymorphic");
static_assert(std::is_polymorphic<polymorphic_class&>::value == false, "polymorphic_class& is not polymorphic");

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • GCC: 4.3.4, 4.5.3, 4.6.2, 4.7.0
  • Visual C++: 2008 (std::tr1), 2010, 2012, 2013, 2015

備考

上の例でコンパイラによってはエラーになる。GCC 4.3.4, 4.5.3, Visual C++ 2010.0 は integral_constantoperator bool() を持っていないためエラーになる。

参照