When compiling on Windows, the compiler gives this warning:
forcing value to bool 'true' or 'false' (performance warning)
It arises when I do something like:
int a = ...
bool b = (a & (1 << 3);
The solution is either to do:
bool b = (a & (1 << 3)) != 0;
or to use an int instead of a bool.
The question is: why the first case incurs a performance issue but not the second? Also, why isn't there the warning when I do:
if (a & (1 << 3)) {
...
}
Because in this case, the value is converted to bool isn't it?
if
only tests for truthful values, it doesn't necessarily cast tobool
. – tadman