I'm a fairly new to C++ and the whole template programming, im sorry this question might be a bit dumb or does not make much sense, but I have the following code
enum SomeEnum : int {FIRST, SECOND, THIRD, FOURTH};
template<SomeEnum Val>
void Foo(){
... some code ...
int x = 0;
if(Val == FIRST){
x = DoCalculationA();
}
else if(Val == THIRD){
x = DoCalculationB();
}
... some more code that uses 'x', and perhaps some more if checks on Val ...
}
Can i assume that the compiler will optimize away the if statements - that when i call this function for example as Foo<FIRST>()
, it wont do any if checks on the Val? Is this even an approriate way to use templates, or should i just be passing the enum as an argument? The code i have needs to run very fast, so any extra if branches are highly problematic.
Edit:
Just to make it a bit more clear. Foo is called like this
Bar(SomeOtherEnum someOtherEnum){
switch(someOtherEnum){
case VALUE_1:
Foo<FIRST>();
case VALUE_2:
Foo<SECOND>();
case VALUE_3:
Foo<THIRD>();
default:
Foo<FOURTH>();
}
}