0

In VS 10 I get a warning:

warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

trying to compile

int x ;
static_cast<bool>(x);

How is it possible to write a code which doesn`t cause this warning?

YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • 6
    It is not an error, it is a warning. – Steve-o Jan 09 '13 at 16:51
  • On a different note, the code, as shown, will lead to *undefined behavior*. The code shows `x` being defined without an initializer, which means `x` will have an *indeterminate* value. Using indeterminate values in any way leads to the above mentioned UB. – Some programmer dude Jul 10 '23 at 14:57

3 Answers3

9

How about something this:

x != 0
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3
int x ;
bool b1 = !!x;  // common idiom; get used to it.  "Newcomers" just need to learn the idiom.
bool b2 = x!=0; // another way, less idiomatic
phonetagger
  • 7,701
  • 3
  • 31
  • 55
1

It is a stupid warning and can be ignored/disabled. There is no performance issue as far as I know.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91