Skip to content

Instantly share code, notes, and snippets.

@PIlin
Created August 23, 2015 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PIlin/efc971bf6e3cabd7264e to your computer and use it in GitHub Desktop.
Save PIlin/efc971bf6e3cabd7264e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <type_traits>
template<class T> class HasTypeTag {
// This version doesn't work as intended in GCC
template<class C> static std::true_type test(char[sizeof(C::TypeTag)]);
// This version works in GCC, Clang and VS2013
//template<class C> static auto test(void*) -> decltype(C::TypeTag, std::true_type());
template<class C> static std::false_type test(...);
public:
enum { Value = std::is_same<decltype(test<T>(nullptr)), std::true_type>::value };
};
struct Foo { enum { TypeTag = 42 }; };
struct Bar {};
int main() {
static_assert(HasTypeTag<Foo>::Value == true, "Foo should have TypeTag");
static_assert(HasTypeTag<Bar>::Value == false, "Bar shouldn't have TypeTag");
std::cout << "Foo: " << HasTypeTag<Foo>::Value << std::endl;
std::cout << "Bar: " << HasTypeTag<Bar>::Value << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment