Namespaces
Variants

std:: is_polymorphic

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
is_polymorphic
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定義先ヘッダ <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]
true もし T ポリモーフィック クラス型の場合、 false それ以外の場合
(公開静的メンバ定数)

メンバ関数

operator bool
オブジェクトを bool に変換し、 value を返す
(公開メンバ関数)
operator()
(C++14)
value を返す
(公開メンバ関数)

メンバ型

定義
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>);
// 継承を使用しているが、virtualキーワードは使用していない:
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)
型が非共用体クラス型かどうかをチェックする
(クラステンプレート)
型が抽象クラス型かどうかをチェックする
(クラステンプレート)
型が仮想デストラクタを持つかどうかをチェックする
(クラステンプレート)