< cpp‎ | types
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations

Common vocabulary types

Elementary string conversions
Type support
Basic types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
Type properties

(C++11)(until C++20)
(C++11)(deprecated in C++20)
Type trait constants
Supported operations
Relationships and property queries
Type modifications
Type transformations

(C++11)(until C++20)(C++17)
Defined in header <type_traits>
template< class Base, class Derived >
struct is_base_of;
(since C++11)

If Derived is derived from Base or if both are the same non-union class (in both cases ignoring cv-qualification), provides the member constant value equal to true. Otherwise value is false.

If both Base and Derived are non-union class types, and they are not the same type (ignoring cv-qualification), Derived shall be a complete type; otherwise the behavior is undefined.

The behavior of a program that adds specializations for is_base_of or is_base_of_v (since C++17) is undefined.


[edit] Helper variable template

template< class Base, class Derived >
inline constexpr bool is_base_of_v = is_base_of<Base, Derived>::value;
(since C++17)

Inherited from std::integral_constant

Member constants

true if Derived is derived from Base or if both are the same non-union class (in both cases ignoring cv-qualification), false otherwise
(public static member constant)

Member functions

operator bool
converts the object to bool, returns value
(public member function)
returns value
(public member function)

Member types

Type Definition
value_type bool
type std::integral_constant<bool, value>

[edit] Notes

std::is_base_of<A, B>::value is true even if A is a private, protected, or ambiguous base class of B. In many situations, std::is_convertible<B*, A*> is the more appropriate test.

Although no class is its own base, std::is_base_of<T, T>::value is true because the intent of the trait is to model the "is-a" relationship, and T is a T. Despite that, std::is_base_of<int, int>::value is false because only classes participate in the relationship that this trait models.

[edit] Possible Implementation

namespace details {
    template <typename B>
    std::true_type test_pre_ptr_convertible(const volatile B*);
    template <typename>
    std::false_type test_pre_ptr_convertible(const volatile void*);
    template <typename, typename>
    auto test_pre_is_base_of(...) -> std::true_type;
    template <typename B, typename D>
    auto test_pre_is_base_of(int) ->
template <typename Base, typename Derived>
struct is_base_of :
        std::is_class<Base>::value && std::is_class<Derived>::value &&
        decltype(details::test_pre_is_base_of<Base, Derived>(0))::value
    > { };

[edit] Example

#include <iostream>
#include <type_traits>
class A {};
class B : A {};
class C {};
int main() 
    std::cout << std::boolalpha;
    std::cout << "a2b: " << std::is_base_of<A, B>::value << '\n';
    std::cout << "b2a: " << std::is_base_of<B, A>::value << '\n';
    std::cout << "c2b: " << std::is_base_of<C, B>::value << '\n';
    std::cout << "same type: " << std::is_base_of<C, C>::value << '\n';


a2b: true
b2a: false
c2b: false
same type: true

[edit] See also

checks if a type can be converted to the other type
(class template) [edit]