How can I partially specialize a class template for ALL enums?

蓝咒 提交于 2019-12-07 05:23:37

问题


Say I have some class template:

template<typename T>
class {
// ....
}

I can partially specialize this template for ALL pointers by:

template<typename T>
class<T *> {
// ....
}

Can I somehow specialize the template for ALL enums? i.e., do something like: (this doesn't work, though)

template<typename T>
class<enum T> {
// ....
}

回答1:


use C++11 and SFINAE.

#include <type_traits>

template<typename T, typename = void>
struct Specialize
{
};

template<typename T>
struct Specialize<T, typename std::enable_if<std::is_enum<T>::value>::type>
{
   void convert() { }
};

enum E
{
};

int main()
{
   Specialize<E> spec;
   spec.convert();
}

Without C++11 use boost::enable_if and boost::is_enum



来源:https://stackoverflow.com/questions/12726954/how-can-i-partially-specialize-a-class-template-for-all-enums

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