Created
November 20, 2017 17:51
-
-
Save codyhex/acebfd5da3c7d46d09ba77e7f5dfe7aa to your computer and use it in GitHub Desktop.
checkBaseClass created by Hexe - https://repl.it/@Hexe/checkBaseClass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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