std::is_polymorphic
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <type_traits> で定義
|
||
template< class T > struct is_polymorphic; |
(C++11以上) | |
T が多相クラス (つまり、少なくとも1つの仮想関数を宣言するか継承する非 union クラス) であれば、 true に等しいメンバ定数 value が提供されます。 それ以外の型に対しては、 value は false です。
T が非 union クラス型の場合、 T は完全型でなければなりません。 そうでなければ、動作は未定義です。
テンプレート引数
| T | - | 調べる型 |
ヘルパー変数テンプレート
<tbody> </tbody> template< class T > inline constexpr bool is_polymorphic_v = is_polymorphic<T>::value; |
(C++17以上) | |
std::integral_constant から継承
メンバ定数
value [静的] |
T が多相クラス型ならば true、そうでなければ 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)) {};
|
例
Run this code
#include <iostream>
#include <type_traits>
struct A {
int m;
};
struct B {
virtual void foo();
};
struct C : B {};
int main()
{
std::cout << std::boolalpha;
std::cout << std::is_polymorphic<A>::value << '\n';
std::cout << std::is_polymorphic<B>::value << '\n';
std::cout << std::is_polymorphic<C>::value << '\n';
}
出力:
false
true
true
関連項目
(C++11) |
型がクラス型 (共用体を除く) かどうか調べます (クラステンプレート) |
(C++11) |
型が抽象クラス型かどうか調べます (クラステンプレート) |