enum myenum { val1 = 10, val2 = 30, val3 = 45 }; template<myenum e> struct is_valid_myenum { static const bool value = (e==val1 || e==val2 || e==val3); }; template<myenum t> class myClass { static_assert(is_valid_myenum<t>::value, "t must be a valid enum value"); }; myClass<10> a;
If you really really want to avoid duplication (and are not interested in using any external tool to generate source code), you can resort to macro hacking.
#define LIST \ ITEM(val1,10)\ ITEM(val2,30)\ ITEM(val3,45) #define ITEM(NAME,VALUE) NAME = VALUE, enum myenum { LIST }; #undef ITEM #define ITEM(NAME,VALUE) e==NAME || template<myenum e> struct is_valid_myenum { static const bool value = ( LIST false ); }; template<myenum t> class myClass { static_assert(is_valid_myenum<t>::value, "t must be a valid enum value"); }; myClass<10> a; // fails, OK myClass<val1> b; // compiles OK myClass<myenum(24)> c; // fails, OK
source share