名前空間
変種
操作

std::is_base_of

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)
(C++14)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
関係とプロパティクエリ
is_base_of
(C++11)
(C++11)
(C++11)
型の変更
(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 Base, class Derived >
struct is_base_of;
(C++11以降)

std::is_base_ofBinaryTypeTrait です。

DerivedBase から 派生している場合、または両方が同じ非共用体クラスである場合(どちらの場合も cv 修飾を無視)、メンバ定数 valuetrue に等しくなります。それ以外の場合、valuefalse です。

BaseDerived の両方が非共用体クラス型であり、それらが異なる型である場合(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 は、DerivedBase から派生している場合、または両方が同じ非共用体クラスである場合(どちらの場合も 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 は、AB のプライベート、プロテクテッド、または曖昧な基底クラスである場合でも true になります。多くの場合、std::is_convertible<B*, A*> がより適切なテストです。

クラスはそれ自身の基底ではありませんが、std::is_base_of<T, T>::value は true です。なぜなら、このトレイトの意図は「is-a」関係をモデル化することであり、TT だからです。それにもかかわらず、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

[編集] 関連項目

ある型が他の型の仮想基底であるかをチェックする
(クラステンプレート) [編集]
ある型が他の型に変換可能であるかをチェックする
(クラステンプレート) [編集]
型が別の型から派生していることを規定する
(コンセプト) [編集]
English 日本語 中文(简体) 中文(繁體)