If I have a normal (weak) enumeration, I can use its enumerated values as non-type template parameters, like so:
enum { Cat, Dog, Horse };
template <int Val, typename T> bool magic(T &t)
{
return magical_traits<Val>::invoke(t);
}
and call it as: magic<Cat>(t)
as far as I can see, if I have a strongly-typed enumeration and don't want to hard-code the enumeration type, I end up with:
enum class Animal { Cat, Dog, Horse };
template <typename EnumClass, EnumClass EnumVal, typename T> bool magic(T &t)
{
return magical_traits<EnumVal>::invoke(t);
}
and now I have to write: magic<Animal, Animal::Cat>(t)
, which seems redundant.
Is there any way to avoid typing out both the enum class and the value, short of
#define MAGIC(E, T) (magic<decltype(E), E>(T));
template <EnumClass EnumVal, typename T> bool magic(T &t)
? Are you going to pass other types of enums to this function? I can't think you are, sincemagical_traits<>
only takes one parameter itself, and it wouldn't make sense. – Typesetterenum class
es, to be a different type each, and to be especially distinct fromint
and the likes. – Bitstocktemplate <Animal a,typename T> bool magic(T &t)
? – Bogey