Created
August 23, 2015 11:45
-
-
Save PIlin/efc971bf6e3cabd7264e to your computer and use it in GitHub Desktop.
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
#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