Skip to content

Instantly share code, notes, and snippets.

@ilya-biryukov
Last active August 29, 2015 14:19
Show Gist options
  • Save ilya-biryukov/887b7e543b72b49376ed to your computer and use it in GitHub Desktop.
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
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