Is there a type trait, or is it possible to write a type trait is_scoped_enum<T>
such that:
T
is a scoped enumeration, is_scoped_enum<T>::value
is true
andT
is any other type, is_scoped_enum<T>::value
is falseScoped enums (enum class/struct) are strongly typed enumerations introduced in C++11. They address several shortcomings of the old C-style (C++98) enums, mainly associated with type-safety and name collisions.
In C#, we can check the specific type is enum or not by using the IsEnum property of the Type class. It will return true if the type is enum. Otherwise, this property will return false. It is a read-only property.
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> {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With