is_base_of
Applicability
Product |
Supported |
|---|---|
√ |
|
√ |
|
x |
|
x |
|
x |
|
x |
Function
Checks whether a type is a base class of another type during program building as a type characteristics tool defined in the <type_traits> header file. This API can be used in template metaprogramming, type checking, and conditional building to capture potential type errors during building, improving code robustness.
Prototype
1 2 | template <typename Base, typename Derived> struct is_base_of; |
Parameters
Parameter |
Description |
|---|---|
Base |
Base class type to be checked, that is, whether the Base type is the base class of the Derived type. |
Derived |
Derived class type to be checked, that is, whether the Base type is the base class of the Derived type. |
Restrictions
None
Returns
The static constant member value of is_base_of is used to obtain the returned Boolean value. The values of is_base_of<Base, Derived>::value are as follows:
- true: The Base type is a base class of Derived (including the case where Base and Derived are the same type).
- false: The Base type is not a base class of Derived.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class Base {}; class Derived : public Base {}; class Unrelated {}; // Derived class of virtual inheritance class Derived2 : virtual public Base {}; // Define the derived class of virtual inheritance. class VirtualDerived : virtual public Base {}; // Define the derived class of multiple inheritance. class MultiDerived : public Base, public VirtualDerived {}; //Base class of the template template <typename T> class BaseTemplate { public: T value; }; // Derived class of the template template <typename T> class DerivedTemplate : public BaseTemplate<T> {}; // Check whether Base is the base class of Derived. AscendC::PRINTF("Is Base a base of Derived? %d\n" , AscendC::Std::is_base_of<Base, Derived>::value); // Check whether Derived is the base class of Base (the value should be false). AscendC::PRINTF("Is Derived a base of Base? %d\n" , AscendC::Std::is_base_of<Derived, Base>::value); // Check whether Base is the base class of Unrelated (the value should be false). AscendC::PRINTF("Is Base a base of Unrelated? %d\n" , AscendC::Std::is_base_of<Base, Unrelated>::value); AscendC::PRINTF("Is Base a base of Derived (virtual inheritance)? %d\n", AscendC::Std::is_base_of<Base, Derived2>::value); AscendC::PRINTF("Is BaseTemplate<int> a base of DerivedTemplate<int>? %d\n", AscendC::Std::is_base_of<BaseTemplate<int>, DerivedTemplate<int>>::value); // Check whether Base is the base class of VirtualDerived (virtual inheritance). AscendC::PRINTF("Is Base a base of VirtualDerived? %d\n" , AscendC::Std::is_base_of<Base, VirtualDerived>::value); // Check whether Base is the base class of MultiDerived (multiple inheritance). AscendC::PRINTF("Is Base a base of MultiDerived? %d\n" , AscendC::Std::is_base_of<Base, MultiDerived>::value); |
1 2 3 4 5 6 7 8 | // Execution result: Is Base a base of Derived? 1 Is Derived a base of Base? 0 Is Base a base of Unrelated? 0 Is Base a base of Derived (virtual inheritance)? 1 Is BaseTemplate<int> a base of DerivedTemplate<int>? 1 Is Base a base of VirtualDerived? 1 Is Base a base of MultiDerived? 1 |