1
votes

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?

1
Could you add the compiler information? As well as the compilation flags?cigien
The if only tests for truthful values, it doesn't necessarily cast to bool.tadman
@tadman — the code does not cast anything. You can see that by looking at it. The result of the expression is “contextually converted to bool”, which may or may not involve generating an actual bool value, since the calculation itself might set the appropriate processor flags.Pete Becker
@PeteBecker That's what I was trying to say, though you do have a more specific approach there.tadman

1 Answers

3
votes

This warning is of the obsolete Visual Studio 2015 compiler spelled with incorrect words in some context. Now it sounds more correct

Implicit conversion from int to bool. Possible information loss

Compiler Warning (level 4) C4800