Template specialization for enum

梦想的初衷 提交于 2019-11-30 08:32:20

问题


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?


回答1:


You can use std::enable_if with std::is_enum from <type_traits> 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 with the Boost equivalents.




回答2:


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);
}



回答3:


Presumably, the only interesting thing you could do with a type that they only thing you know about it is that it's an enum, is cast it to its underlying type and operate on that. Here's how that might look like, using James' suggested approach (AKA SFINAE):

void Bar(int b); // and/or other underlying types

template<typename T>
typename std::enable_if<std::is_enum<T>::value, void>::type
Foo(T enm)
{
    Bar(static_cast<typename std::underlying_type<T>::type>(enm));
}

As a related bonus, here's a similar method that would only get resolved for a specific type of your choosing (replace bool in is_same to the type of your choosing):

template<typename T>
typename std::enable_if<std::is_same<T,bool>::value, void>::type
Baz(T bl)
{
    if (bl)
    {
        //...
    }
    else
    {
        //...
    }
}


来源:https://stackoverflow.com/questions/1619993/template-specialization-for-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!