Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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> {
// ....
}
like image 553
qwer1304 Avatar asked Oct 04 '12 12:10

qwer1304


1 Answers

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

like image 95
ForEveR Avatar answered Nov 03 '22 00:11

ForEveR