std::is_polymorphic
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct is_polymorphic; |
(C++11以降) | |
std::is_polymorphic は UnaryTypeTrait です。
T が ポリモーフィッククラス(つまり、少なくとも1つの仮想関数を宣言または継承している、非共用体クラス)である場合、メンバー定数 value を true に設定します。それ以外の型の場合、value は false になります。
T が未定義の非共用体クラス型の場合、動作は未定義です。
プログラムが std::is_polymorphic または std::is_polymorphic_v の特殊化を追加した場合、その動作は未定義です。
目次 |
[編集] テンプレートパラメータ
| T | - | チェックする型 |
[編集] ヘルパー変数テンプレート
| template< class T > constexpr bool is_polymorphic_v = is_polymorphic<T>::value; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
T が ポリモーフィックなクラス型である場合は true、それ以外の場合は false(公開静的メンバ定数) |
メンバ関数
| operator bool |
オブジェクトを bool に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[編集] 実装例
namespace detail { template<class T> std::true_type detect_is_polymorphic( decltype(dynamic_cast<const volatile void*>(static_cast<T*>(nullptr))) ); template<class T> std::false_type detect_is_polymorphic(...); } // namespace detail template<class T> struct is_polymorphic : decltype(detail::detect_is_polymorphic<T>(nullptr)) {}; |
[編集] 例
このコードを実行
#include <type_traits> struct A { int m; }; static_assert(!std::is_polymorphic_v<A>); struct B { virtual void foo(); }; static_assert(std::is_polymorphic_v<B>); struct C : B {}; static_assert(std::is_polymorphic_v<C>); struct D { virtual ~D() = default; }; static_assert(std::is_polymorphic_v<D>); // Uses inheritance, but not the virtual keyword: struct E : A {}; static_assert(!std::is_polymorphic_v<E>); struct F : virtual A {}; static_assert(!std::is_polymorphic_v<F>); struct AX : A {}; struct AY : A {}; struct XY : virtual AX, virtual AY {}; static_assert(!std::is_polymorphic_v<XY>); int main() {}
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2015 | C++11 | 次に該当する場合、動作は未定義でした。T が未定義の共用体型 |
この場合、基底特性は std::false_type |
[編集] 関連項目
| (C++11) |
型が共用体でないクラス型であるかをチェックする (クラステンプレート) |
| (C++11) |
型が抽象クラス型であるかをチェックする (クラステンプレート) |
| (C++11) |
型が仮想デストラクタを持つかをチェックする (クラステンプレート) |