Last active
August 29, 2015 14:06
-
-
Save jefftrull/6131ffeb35ef72ef348f to your computer and use it in GitHub Desktop.
void_t usage test case
This file contains hidden or 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 <type_traits> | |
#include <iostream> | |
using namespace std; | |
template<class... > using void_t = void; | |
template< class, class = void > | |
struct has_type_member : false_type { }; | |
template< class T > | |
struct has_type_member<T, void_t<typename T::type>> : true_type { }; | |
template< class, class = void > | |
struct has_type_data_member : false_type { }; | |
template< class T > | |
struct has_type_data_member<T, void_t<decltype(T::type)>> : true_type { }; | |
struct foo { | |
int type; | |
}; | |
struct bar {}; | |
struct baz { | |
typedef int type; | |
}; | |
int main() { | |
if (has_type_member<foo>::value) { | |
std::cout << "foo has a type member\n"; | |
} else { | |
std::cout << "foo does not have a type member\n"; | |
} | |
if (has_type_member<bar>::value) { | |
std::cout << "bar has a type member\n"; | |
} else { | |
std::cout << "bar does not have a type member\n"; | |
} | |
if (has_type_member<baz>::value) { | |
std::cout << "baz has a type member\n"; | |
} else { | |
std::cout << "baz does not have a type member\n"; | |
} | |
if (has_type_data_member<foo>::value) { | |
std::cout << "foo has a type data member\n"; | |
} else { | |
std::cout << "foo does not have a type data member\n"; | |
} | |
if (has_type_data_member<bar>::value) { | |
std::cout << "bar has a type data member\n"; | |
} else { | |
std::cout << "bar does not have a type data member\n"; | |
} | |
if (has_type_data_member<baz>::value) { | |
std::cout << "baz has a type data member\n"; | |
} else { | |
std::cout << "baz does not have a type data member\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment