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은 public 상속 또는 동일 클래스인 경우에만 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);      // public 상속: true
static_assert(std::derived_from<C, A> == false);     // private 상속: false
// std::is_base_of는 private 상속인 경우에도 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);      // public 상속: true
static_assert(std::is_base_of_v<A, C> == true);      // private 상속: 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 Concept derived_from [concept.derived]

참고 항목

(C++11)
한 타입이 다른 타입의 베이스인지 확인
(클래스 템플릿)
한 타입이 다른 타입으로 변환 가능한지 확인
(클래스 템플릿)