Enum templated template only

Is there a way to ensure that a templated class will fail to compile if a particular template argument is supplied with something other than a strongly typed enum (i.e., an enum class)?

+3


source to share


1 answer


Use the sign and static_assert

.

those.



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

template <typename T>
struct myTemplate
{
   static_assert( is_scoped_enum<T>{}, "Invalid type argument!" );
};

      

(Derived from this answer .)
Demo .

+7


source







All Articles