Is it bad practice to use the bool type in arithmetic operations, in particular with non bool types? e.g. if I want to set a value to 0 based on a comparison operator without using branches I would do something like this:
1 2 3 4 5 6 7 8 9 10 11 12
//with branch
int var = 10;
if(var == 10)
var = 0;
//without branch
bool tVar = !(var == 10);
var *= tVar;
//or even this
bool tVar = (var == 10);
var *= !tVar;
It compiles and runs fine, I just want to make sure I won't be introducing any tricky bugs due to this.
wow, c++ is odd...
It optimizes this line into machine code that has no jumps in it only for integers. It doesn't do that to floats. Or is it just my optimization settings?
how it's implemented depends on the situation and the compiler.
These kinds of implementation details are not something you need to generally worry about. Just write your code and trust that the compiler knows what it's doing.