Last active
August 29, 2015 14:19
-
-
Save ilya-biryukov/887b7e543b72b49376ed to your computer and use it in GitHub Desktop.
C++ trait to check if class has a member 'type' or not
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
template <class T> | |
class has_member_type { | |
struct One { char a[1]; }; | |
struct Two { char a[2]; }; | |
template <class U> | |
static One foo(typename U::type*); | |
template <class U> | |
static Two foo(...); | |
public: | |
static const bool value = sizeof(foo<T>(nullptr)) == sizeof(One); | |
}; | |
struct without_type { | |
}; | |
struct with_type { | |
typedef int type; | |
}; | |
static_assert(!has_member_type<without_type>::value, ""); | |
static_assert(has_member_type<with_type>::value, ""); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment