In this tutorial blog we will learn how to check inheritance at compile time using C++11 type_traits. In c++11 type_traits are compile time meta functions that returns actual type or check whether some type is of a particular type.

Before using type_traits meta functions we need to include below header.

#include <type_traits>

how to check inheritance at compile time using C++11 type_traits std::is_base_of

#include<iostream>
using namespace std;

class Base {};

class Der : public Base {};

class Unrel{};

int main()
{
  cout << is_base_of<Base,Der>::value << endl;
  cout << is_base_of<Base,Unrel>::value << endl;
  return 0;
}

 

Re:

http://en.cppreference.com/w/cpp/types/is_base_of




Related Contents to follow