std:: is_base_of
|
定義先ヘッダ
<type_traits>
|
||
|
template
<
class
Base,
class
Derived
>
struct is_base_of ; |
(C++11以降) | |
std::is_base_of
は
BinaryTypeTrait
です。
Derived
が
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
それ以外の場合
(public static member constant) |
メンバ関数
|
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
のprivate、protected、または曖昧な基底クラスである場合でも
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; // プライベートまたは曖昧な基底クラス } 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)
|
ある型が別の型から派生していることを指定する
(コンセプト) |