Voko
Voko

Reputation: 778

Why is forcing int to bool a performance issue?

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?

Upvotes: 1

Views: 527

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38499

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

Upvotes: 3

Related Questions