Skip to content

Instantly share code, notes, and snippets.

@codyhex
Created November 20, 2017 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyhex/acebfd5da3c7d46d09ba77e7f5dfe7aa to your computer and use it in GitHub Desktop.
Save codyhex/acebfd5da3c7d46d09ba77e7f5dfe7aa to your computer and use it in GitHub Desktop.
checkBaseClass created by Hexe - https://repl.it/@Hexe/checkBaseClass
#include <iostream>
/*
* Class template identification
*/
using namespace std;
template<typename D, typename B>
class IsDerivedFromHelper
{
class No { };
class Yes { No no[3]; };
static Yes Test( B* );
static No Test( ... );
public:
enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };
};
template <class C, class P>
bool IsDerivedFrom() {
return IsDerivedFromHelper<C, P>::Is;
}
class Person {
public:
int a;
};
class Student: public Person {};
int main() {
cout << IsDerivedFrom<Student,Person>() << endl;
// use this directly in C11, is_base_of<_Derived, _Base>()
cout << is_base_of<Person, Student>();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment