views:

547

answers:

2

Is it possible to specialize a templatized method for enums?

Something like (the invalid code below):

template <typename T>
void f(T value);

template <>
void f<enum T>(T value);

In the case it's not possible, then supposing I have specializations for a number of types, like int, unsigned int, long long, unsigned long long, etc, then which of the specializations an enum value will use?

+8  A: 

You can use Boost's enable_if with is_enum from Boost.TypeTraits to accomplish this.

In an answer to one of my questions, litb posted a very detailed and well-written explanation of how this can be done.

James McNellis
This looks like it might work. I'm taking a look at it. Thanks!
nilton
+3  A: 

I'm not sure if I understand your question correctly, but you can instantiate the template on specific enums:

template <typename T>
void f(T value);

enum cars { ford, volvo, saab, subaru, toyota };
enum colors { red, black, green, blue };

template <>
void f<cars>(cars) { }

template <>
void f<colors>(colors) { }

int main() {
    f(ford);
    f(red);
}
Thomas Padron-McCarthy
That wouldn't work because I don't have the enum type beforehand.
nilton