std::is_base_of

From cppreference.com
< cpp‎ | types
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++14)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++14)
(C++11)
Type trait constants
Metafunctions
(C++17)
(C++17)
(C++17)
Supported operations
Relationships and property queries
(C++11)
is_base_of
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Type transformations
(C++11)
(C++11)
(C++11)
(C++11)
(C++17)
(C++11)
(C++11)
(C++11)
(C++11)
 
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, 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.

Contents

[edit] Helper variable template

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

Inherited from std::integral_constant

Member constants

value
[static]
true if Derived is derived from Base , false otherwise
(public static member constant)

Member functions

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

Member types

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

[edit] Notes

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] 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';
}

Output:

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

[edit] See also

(library fundamentals TS)
variable template alias of std::is_base_of::value
(variable template)