名前空間
変種
操作

std::is_polymorphic

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型のプロパティ
(C++11)
(C++11)
is_polymorphic
(C++11)
(C++14)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
関係とプロパティクエリ
型の変更
(C++11)(C++11)(C++11)
型の変換
(C++11)(C++23で非推奨)
(C++11)(C++23で非推奨)
(C++11)
(C++11)(C++20まで*)(C++17)

(C++11)
(C++17)
コンパイル時有理数演算
コンパイル時整数シーケンス
 
ヘッダ <type_traits> で定義
template< class T >
struct is_polymorphic;
(C++11以降)

std::is_polymorphicUnaryTypeTrait です。

Tポリモーフィッククラス(つまり、少なくとも1つの仮想関数を宣言または継承している、非共用体クラス)である場合、メンバー定数 valuetrue に設定します。それ以外の型の場合、valuefalse になります。

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)
型が共用体でないクラス型であるかをチェックする
(クラステンプレート) [編集]
型が抽象クラス型であるかをチェックする
(クラステンプレート) [編集]
型が仮想デストラクタを持つかをチェックする
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)