Ehsan Rashid
Ehsan Rashid

Reputation: 9

C++ function template partial specialization with multiple enum parameter in c++?

enum ABC
{
    A,     B,  C
}

enum XYZ
{
    X,         Y,  Z
}


template<XYZ xyz>
void DoSomething     ();

template<ABC abc, XYZ xyz>
void DoSomething     ();

template<> void DoSomething  <X>()
{ ... }
template<> void DoSomething  <Y>()
{ ... }
template<> void DoSomething  <Z>()
{ ... }

By switch i have done this

template<ABC abc, XYZ xyz>
void DoSomething     ()
{
   switch (xyz)
   {
    case X: ... break;
    case Y: ... break;
    case Z: ... break;
    default: break;
   }
}

but i wana do something like this writting 3 different function of each enum value of 2nd parameter and removing the switch

template<> void Pos::DoSomething  <ABC abc, X>()
{
 ...
}
template<> void Pos::DoSomething  <ABC abc, Y>()
{
 ...
}
template<> void Pos::DoSomething  <ABC abc, Z>()
{
 ...
}

how to do this ??? template partial specialization of function? please help me

Upvotes: 0

Views: 335

Answers (1)

ForEveR
ForEveR

Reputation: 55887

There is no partial function specialization. You can use structs with static functions for this case.

template<ABC, XYZ> struct do_helper;
template<ABC abc> struct do_helper<abc, X>
{
   static void apply()
   {
   }
};

// same for Y, Z

template<ABC abc, XYZ xyz>
void Pos::DoSomething()
{
   do_helper<abc, xyz>::apply();
}

Upvotes: 4

Related Questions