Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a type is an enum class? [duplicate]

Is there a type trait, or is it possible to write a type trait is_scoped_enum<T> such that:

  • if T is a scoped enumeration, is_scoped_enum<T>::value is true and
  • if T is any other type, is_scoped_enum<T>::value is false
like image 492
James McNellis Avatar asked Nov 16 '22 02:11

James McNellis


1 Answers

I think testing if it is an enum and not implicitly convertible to the underlying type should do the trick.

template <typename T, bool B = std::is_enum<T>::value>
struct is_scoped_enum : std::false_type {};

template <typename T>
struct is_scoped_enum<T, true>
: std::integral_constant<bool,
    !std::is_convertible<T, typename std::underlying_type<T>::type>::value> {};
like image 64
R. Martinho Fernandes Avatar answered Dec 10 '22 16:12

R. Martinho Fernandes