Reputation: 81
I have a template which takes as specialization an enum class (note the different initializations of enum), and I would like to be able to compare two different enums
enum choiceA {One, Two, Three};
typedef enum {Four, Five, Six} choiceB;
template<typename S>
void f(S choice, int& x)
{
if (choice == One || choice == Four) { x = SomeWhateverIntegerValue;}
else if(choice = Five) {x = AnOtherWhateverIntegerValue;}
else {x = OtherIntegerValue;}
}
so that
int x = 0;
f<choiceA>(One, x);
works as intended.
Upvotes: 0
Views: 57
Reputation: 75688
The fact that you have two enum types for the same enumeration is odd. I would fix that design first.
Anyway to strictly answer your question you don't need templates here. A simple overload would suffice:
void f(choiceA choice, int& x)
{
if (choice == One)
{
x = SomeWhateverIntegerValue;
}
else
{
x = OtherIntegerValue;
}
}
void f(choiceB choice, int& x)
{
if (choice == Four)
{
x = SomeWhateverIntegerValue;
} else if(choice == Five)
{
x = AnOtherWhateverIntegerValue;
}
else
{
x = OtherIntegerValue;
}
}
As a side note avoid the C style typedef enum/class { ... } Name;
.
Also take care if(choice = Five)
you have a dangerous typo. It should be ==
. Enable compiler warnings, most modern compilers warn about this.
Upvotes: 3