Namespaces
Variants

std:: is_base_of

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
헤더 파일에 정의됨 <type_traits>
template < class Base, class Derived >
struct is_base_of ;
(C++11부터)

std::is_base_of BinaryTypeTrait 입니다.

만약 Derived derived from Base 이거나, 또는 둘 다 동일한 non-union 클래스인 경우 (두 경우 모두 cv-qualification을 무시), 멤버 상수 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 로부터 파생되었거나, 둘 다 동일한 비-union 클래스인 경우 (두 경우 모두 cv-qualification 무시), 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, 또는 모호한(ambiguous) 기본 클래스인 경우에도 true 입니다. 많은 상황에서 std:: is_convertible < B * , A * > 가 더 적합한 검사 방법입니다.

어떤 클래스도 자기 자신의 기반 클래스가 아니지만, std :: is_base_of < T, T > :: value 는 참입니다. 왜냐하면 이 특성의 의도는 "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; // private 또는 모호한 베이스 클래스
}
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

참고 항목

한 타입이 다른 타입의 가상 베이스인지 검사합니다
(클래스 템플릿)
한 타입이 다른 타입으로 변환 가능한지 검사합니다
(클래스 템플릿)
한 타입이 다른 타입으로부터 파생되었음을 명시합니다
(컨셉트)