Created
April 25, 2013 20:34
-
-
Save den-mentiei/5462920 to your computer and use it in GitHub Desktop.
C++ template-fu allowing to check classes relationship in compile-time.
This file contains 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
typedef char (&yes)[1]; | |
typedef char (&no)[2]; | |
template <typename B, typename D> | |
struct Host { | |
operator B*() const; | |
operator D*(); | |
}; | |
template <typename B, typename D> | |
struct is_base_of { | |
template <typename T> | |
static yes check(D*, T); | |
static no check(B*, int); | |
static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes); | |
}; | |
// Testing | |
class B {}; | |
class D : private B {}; | |
int Test[is_base_of<B,D>::value && !is_base_of<D,B>::value]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.rsdn.ru/forum/cpp/3290559.flat#3290559