std::is_base_of
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class Base, class Derived > struct is_base_of; |
(C++11以降) | |
std::is_base_of は BinaryTypeTrait です。
Derived が Base から 派生している場合、または両方が同じ非共用体クラスである場合(どちらの場合も cv 修飾を無視)、メンバ定数 value は true に等しくなります。それ以外の場合、value は false です。
Base と Derived の両方が非共用体クラス型であり、それらが異なる型である場合(cv 修飾を無視)、Derived は 完全型 である必要があります。それ以外の場合、動作は未定義です。
プログラムが std::is_base_of または std::is_base_of_v(C++17以降) の特殊化を追加する場合、動作は未定義です。
目次 |
[編集] ヘルパー変数テンプレート
| template< class Base, class Derived > constexpr bool is_base_of_v = is_base_of<Base, Derived>::value; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
true は、Derived が Base から派生している場合、または両方が同じ非共用体クラスである場合(どちらの場合も cv 修飾を無視)、それ以外の場合は false(公開静的メンバ定数) |
メンバ関数
| operator bool |
オブジェクトを bool に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[編集] 注釈
std::is_base_of<A, B>::value は、A が B のプライベート、プロテクテッド、または曖昧な基底クラスである場合でも true になります。多くの場合、std::is_convertible<B*, A*> がより適切なテストです。
クラスはそれ自身の基底ではありませんが、std::is_base_of<T, T>::value は true です。なぜなら、このトレイトの意図は「is-a」関係をモデル化することであり、T は T だからです。それにもかかわらず、std::is_base_of<int, int>::value は false です。なぜなら、このトレイトがモデル化する関係にはクラスのみが関与するからです。
[編集] 実装例
namespace details { template<typename B> std::true_type test_ptr_conv(const volatile B*); template<typename> std::false_type test_ptr_conv(const volatile void*); template<typename B, typename D> auto test_is_base_of(int) -> decltype(test_ptr_conv<B>(static_cast<D*>(nullptr))); template<typename, typename> auto test_is_base_of(...) -> std::true_type; // private or ambiguous base } template<typename Base, typename Derived> struct is_base_of : std::integral_constant< bool, std::is_class<Base>::value && std::is_class<Derived>::value && decltype(details::test_is_base_of<Base, Derived>(0))::value > {}; |
[編集] 例
このコードを実行
#include <type_traits> class A {}; class B : A {}; class C : B {}; class D {}; union E {}; using I = int; static_assert ( std::is_base_of_v<A, A> == true && std::is_base_of_v<A, B> == true && std::is_base_of_v<A, C> == true && std::is_base_of_v<A, D> != true && std::is_base_of_v<B, A> != true && std::is_base_of_v<E, E> != true && std::is_base_of_v<I, I> != true ); int main() {}
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2015 | C++11 | 以下の場合、動作は未定義になる可能性があります。Derived が不完全な共用体型である |
この場合、基底特性は std::false_type |
[編集] 関連項目
| (C++26) |
ある型が他の型の仮想基底であるかをチェックする (クラステンプレート) |
| (C++11)(C++20) |
ある型が他の型に変換可能であるかをチェックする (クラステンプレート) |
| (C++20) |
型が別の型から派生していることを規定する (コンセプト) |