How to implement a property of type is_enum_class?

How to implement an object attribute whose value element is true if and only if the passed type T is an enumeration of the class? Although I know that, for example,

+T{}; 

will work if T is an enum and crashes if it is an enum class, I have not yet been able to find a way to use this for SFINAE.

+7
c ++ typetraits metaprogramming sfinae
source share
2 answers

Based on the test +T{} :

Option number 1:

SFINAE expression in return type:

 #include <type_traits> template <typename T> auto test(int) -> decltype((void)+T{}, std::false_type{}); template <typename T> auto test(...) -> std::true_type; template <typename T> using is_enum_class = std::integral_constant<bool, decltype(test<T>(0))::value && std::is_enum<T>::value>; 

Demo

Option number 2:

In void_t-fashion:

 template <typename T, typename V = void> struct test : std::false_type {}; template <typename T> struct test<T, decltype((void)+T{})> : std::true_type {}; template <typename T> using is_enum_class = std::integral_constant<bool, !test<T>::value && std::is_enum<T>::value>; 

Demo 2

Tests:

 enum class EC { a, b }; enum E { c, d }; int main() { static_assert(is_enum_class<EC>::value, "!"); static_assert(!is_enum_class<E>::value, "!"); static_assert(!is_enum_class<int>::value, "!"); } 
+10
source share

You check if the enum can be converted to int .

 template <class T> using is_scoped_enum = std::integral_constant<bool, !std::is_convertible<T,int>{} && std::is_enum<T>{}>; 

These static statements will succeed:

 enum e {}; enum class e2 {}; static_assert( is_scoped_enum<e2>::value, "" ); static_assert( !is_scoped_enum<e>::value, "" ); static_assert( !is_scoped_enum<char>::value, "" ); 

Demo

+5
source share

All Articles