Lets say I have 2 functions which perform the exact same operations on the arguments but use different sets of constants to do so. For an oversimplified example:
int foo1(int x){
return 3+4*x
}
int foo2(int x){
return 6-4*x
}
In the real applications assume there will be multiple arguments and constants/literals, and of course the computation will be much more complex. For the sake of simplicity, as well as maintainability, I want to rewrite these two functions as a template that can produce both of these functions, so that I could call foo<1> or foo<2> and the proper function will be generated. I am aware that I could do something like this:
int foo(int x, int funcType){
const int firstConst = (funcType==1) ? 3 : 6;
const int secondConst = (funcType==1) ? 4 : -4;
return firstConst+secondConst*x;
}
but since I am always aware at compile time of which function I want to use, I would like to use templates to avoid the branching. Is there any way to do this?