最終更新日時(UTC):
が更新

履歴 編集

class template
<type_traits>

std::is_base_of(C++11)

namespace std {
  template <class Base, class Derived>
  struct is_base_of;

  template <class Base, class Derived>
  inline constexpr bool is_base_of_v = is_base_of<Base, Derived>::value; // C++17
}

概要

Baseが型Derivedの基底クラスか調べる。

要件

BaseDerivedが非共用体のクラスであり、異なる型である場合(cv修飾は無視される)、Derivedは完全型でなければならない。

効果

is_base_ofは、型Baseが型Derivedの基底クラス (cv修飾は無視される) である、もしくは2つが同じクラス型ならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

備考

派生時のprivateprotected指定は、派生関係の判定に影響しない。

#include <type_traits>

struct B {};
struct B1 : B {};
struct B2 : B {};
struct D : private B1, private B2 {};

static_assert(std::is_base_of<B, D>::value == true, "B is base of D");
static_assert(std::is_base_of<B, B>::value == true, "B is base of B");
static_assert(std::is_base_of<D, B>::value == false, "D is not base of B");

// cv修飾は無視される
static_assert(std::is_base_of<const B, const D>::value == true, "B is base of D");

// クラス型ではない
static_assert(std::is_base_of<int, char>::value == false, "int is not base of char");

int main() {}

出力

バージョン

言語

  • C++11

処理系

参照

  • [LWG Issue 2015. Incorrect pre-conditions for some type traits]
    • C++11では要件が「BaseDerivedがクラスであり、異なる型である場合(cv修飾は無視される)、Derivedは完全型でなければならない。」であったが、共用体を意図せず許容していたため、C++14で「BaseDerived非共用体のクラスであり、異なる型である場合(cv修飾は無視される)、Derivedは完全型でなければならない。」に変更した。
  • P0006R0 Adopt Type Traits Variable Templates from Library Fundamentals TS for C++17