I would like to specialized the behaviour of a template function member according to the enum member of the class it is operating on. I am pretty sure this is doable but I can't see how. Here is a failed attempt which doesn't compile (why ?). In fact I already got a working solution for my project (using inheritance) but this isn't nice and I am curious about what could be done.
#include <iostream>
struct A
{
enum
{
Size = 2
};
};
struct B
{
enum
{
Size = 3
};
};
template <int I>
struct EnumToType
{
static const int e = I;
};
template <typename T, typename U>
struct C {};
template <typename T>
struct D
{
typedef C<T, typename EnumToType<T::Size> > Type;
};
template <typename T>
struct C<T, EnumToType<2> >
{
void operator()()
{
std::cout << "hi !" << std::endl;
}
};
template <typename T>
struct C<T, EnumToType<3> >
{
void operator()()
{
std::cout << "hello !" << std::endl;
}
};
int main()
{
D<A>::Type da;
D<B>::Type db;
da();
db();
return 0;
}
A useful link...