10

How can one implement type trait whose value member is true if and only if the passed in type T is a class enum? While I know that for instance

+T{};

will work if T is an enum and fail if it is an enum class, I couldn't find a way so far to use this for SFINAE.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Inkane
  • 1,440
  • 1
  • 15
  • 32
  • Update the C++ tag to the correct C++ version... This is confusing. is_enum type trait should also work with a class enum. But I also have problems with this. – jaques-sam Mar 07 '19 at 13:22

2 Answers2

16

Based on your +T{} test:

Option #1:

Expression SFINAE in trailing 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 #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, "!");
}
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
7

You check whether the enumeration is convertible to int.

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

These static assertions 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.

Columbo
  • 60,038
  • 8
  • 155
  • 203