Is there any way to ensure that a templated class will fail to compile if a specific template argument is supplied with something other than a strongly-typed enumeration (i.e. enum class)?
Use a trait and static_assert.
I.e.
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!" );
};
(Taken from this answer.)
Demo.
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