Modass
Modass

Reputation: 197

Mapping Enums in template specializations

I have a templated class which uses a global enum with some modes I need as parameters for the member functions of that class. I want to write a specialization of that class which requires that the values of that enum are "translated". Currently I implement a conversion function which basically is a switch statement, which translates the general enum values to the values required by the specialization. E.g. something like this:

enum Modes { ModeA, ModeB, ModeC };
template<typename Foo>
class tClass
{
   void bar ( Modes _mode )
   {
     do_stuff(_mode);
   }
}

template<>
class tClass<specializedFoo>
{
   void bar ( Modes _mode )
   {
     do_Stuff(convertMode(_mode));
   }
}

The memberfunctions are called frequently which introduces an additional function call everytime an instanciation of the specialization is used, which, in my opinion should be avoidable, as I already know the exact type of that template parameter.

Is there any way to avoid a translation function and thus the additional function call?

Upvotes: 1

Views: 224

Answers (1)

Modass
Modass

Reputation: 197

After a bit more research I found a possible solutiuon:

If my member functions get an integer value as a parameter instead of a value from Modes and inside the class specialization I use a separate enum with the translated values in the same order as in the original enum, I can use:

enum specEnum {ModeASpec, ModeBSpec, ModeCSpec};
void bar ( int _mode )
{
  do_Stuff( static_cast<specEnum>(_mode));
}

Edit/Addition:

There is even no need to change the type to int for the parameters, so

void bar ( Modes _mode )
{
  do_Stuff( static_cast<specEnum>(_mode));
}

does the trick.

Upvotes: 1

Related Questions