Skip to content

Instantly share code, notes, and snippets.

@den-mentiei
Created April 25, 2013 20:34
Show Gist options
  • Save den-mentiei/5462920 to your computer and use it in GitHub Desktop.
Save den-mentiei/5462920 to your computer and use it in GitHub Desktop.
C++ template-fu allowing to check classes relationship in compile-time.
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];
@den-mentiei
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment