I have an expression
x += y;
and, based on a boolean, I want to be able to change it to
x -= y;
Of course I could do
if(i){x+=y;} else{x-=y;}
//or
x+=(y*sign); //where sign is either 1 or -1
But if I have to do this iteratively, I want to avoid the extra computation. Is there a more efficient way? Is it possible to modulate the operator?
The most efficient way to do this iteratively is to precompute the data you need.
So, precomputation:
Then in your loop:
EDIT: re question in commentary about how to generate code, like this:
E.g. with Visual C++ 10.0 this generates two versions of
myFunc, one with anaddinstruction and the other with asubinstruction.Cheers & hth.,