Namespaces
Variants

std:: derived_from

From cppreference.net
定義先ヘッダ <concepts>
template < class Derived, class Base >

concept derived_from =
std:: is_base_of_v < Base, Derived > &&

std:: is_convertible_v < const volatile Derived * , const volatile Base * > ;
(C++20以降)

コンセプト derived_from < Derived, Base > は、 Base Derived と同じクラス型であるか、または Derived の公開かつ明確な基底クラスである場合にのみ満たされます(cv修飾子は無視します)。

この動作は、 Base Derived のprivateまたはprotected基底クラスである場合の std::is_base_of とは異なることに注意してください。

#include <concepts>
class A {};
class B : public A {};
class C : private A {};
// std::derived_from は公開継承または同一クラスの場合のみ true
static_assert(std::derived_from<B, B> == true);      // 同一クラス: true
static_assert(std::derived_from<int, int> == false); // 同一基本型: false
static_assert(std::derived_from<B, A> == true);      // 公開継承: true
static_assert(std::derived_from<C, A> == false);     // 非公開継承: false
// std::is_base_of は非公開継承の場合も true
static_assert(std::is_base_of_v<B, B> == true);      // 同一クラス: true
static_assert(std::is_base_of_v<int, int> == false); // 同一基本型: false
static_assert(std::is_base_of_v<A, B> == true);      // 公開継承: true
static_assert(std::is_base_of_v<A, C> == true);      // 非公開継承: true
int main() {}

参考文献

  • C++23標準 (ISO/IEC 14882:2024):
  • 18.4.3 コンセプト derived_from [concept.derived]
  • C++20標準 (ISO/IEC 14882:2020):
  • 18.4.3 コンセプト derived_from [concept.derived]

関連項目

(C++11)
ある型が別の型の基底クラスであるかどうかをチェックする
(クラステンプレート)
ある型が別の型に変換可能かどうかをチェックする
(クラステンプレート)