I wonder if there is a way to tell an enum type at
compile time. One could define, say
enum A { a1, a2, a3 };
enum B { b1, b2, b3, b4, b5 };
and would like to tell if A or B is enum at compile
time, just like one can use TypeTraits in Loki to
tell that int is one of the fundamental types.
I realize that it's possible to specialize
IsCustomSignedInt like this:
template<> struct IsCustomSignedInt<A> { enum { value
= 1; } };
template<> struct IsCustomSignedInt<B> { enum { value
= 1; } };
so that code can be generated from templated code
using A and B. However, to reduce redundant code, the
version for type int can be used for all enum types,
as long as we can tell that A or B is an enum type.
As a possible solution, if there is no way built-in
the C++ language to tell this (guess I'm still
studying to become a C++ language lawyer, or perhaps
I'd never achieve that), I wonder if using the
Loki::Seq<> is good enough:
typedef Loki::Seq<A, B>::Type CustomEnums;
and then
enum { isCustomEnum = TL::IndexOf<CustomEnums,
UnqualifiedType>::value >= 0 ||
TL::IndexOf<CustomEnums, typename
ReferenceTraits<UnqualifiedType>::ReferredType>::value
>= 0 };
With this aproach, one just adds the name of the enum
type to the typedef.
I also would like to see a tool for determining whether a template argument is an enumeration; in particular, TypeTraits<>::ParameterType for an enum type is somewhat incorrect at present because it will use a const reference instead of a copy, which might be more efficient if pointers are longer than the enum's size: for example, if addresses are 32-bits and the word size is 16 bits, then a const reference will need 32 bits of space, but an enum with values within [0,65,535] could be optimized by the compiler to use only one word of space.
Could something be done with static_cast<> perhaps?