5

The C4800 warning in the microsoft c++ compiler as described here:

https://msdn.microsoft.com/en-us/library/b6801kcy.aspx

makes this code:

// C4800.cpp
// compile with: /W3
int main() {
  int i = 0;

 // try..
 // bool i = 0;

  bool j = i;   // C4800
  j++;
}

throw the C4800 warning: "'type' : forcing value to bool 'true' or 'false' (performance warning)"

Microsoft seems to think it's reasonably important, and has it as a Level3 warning, however Clang does apparently think it's not, as has zero complaints about it at -Weverything, its maximum warning level.

Is there any real world bug someone can come up with that C4800 would point out that would make it worth having it enabled?

Lucas Meijer
  • 4,424
  • 6
  • 36
  • 53
  • 2
    Read the wording: "performance warning". It's just warning you that this may be somewhat slower/more expensive than you might expect. – Jerry Coffin Jan 27 '16 at 21:38
  • 3
    performance degradation is a bug too :) can you think of a real world case where you have a unnecissery performance degradation that this warning would point out? – Lucas Meijer Jan 27 '16 at 21:39
  • 1
    If you haven't found it already, [this question](http://stackoverflow.com/questions/206564/what-is-the-performance-implication-of-converting-to-bool-in-c) may be of some interest to you. The first answer seems to indicate that "performance warning" isn't quite what the warning is intended to point out, anymore. – jaggedSpire Jan 27 '16 at 21:40
  • This conversion should normally not need to result in performance degradation, it could in many cases be a no-op, because the implementation is not required to represent a `bool` as one or zero. The requirement of clamping the value only arises if you cast it back to int or compares `bool`s - which should be quite rare. – skyking Jan 27 '16 at 21:51
  • As the warning message told you, it is not about a potential bug, but rather a *performance* warning. – AnT stands with Russia Jan 27 '16 at 21:59
  • Maybe clang is smart enough that it will translate the code to `bool j = false;` at compile-time, whereas for the MS compiler it is a runtime issue – M.M Jan 27 '16 at 22:02

1 Answers1

3

Basically, this is just warning you that you've converting some other integer type to a bool, and this conversion isn't entirely free.

It's mostly present (at least as I see things) to warn that you're mixing bools with other integer types, which not only leads to a minor reduction in performance, but may also indicate some confusion in the code. Looking at the code in the question:

  int i = 0;

 // try..
 // bool i = 0;

  bool j = i;   // C4800
  j++;

...what we have looks like an incomplete conversion of some code that previously defined j to be of type int. The definition of j has been edited so it's now of type bool, but we're still assigning it a value from an int, and (worse) using a post-increment on it, both of which would make sense if j had type int, but really don't with j having type bool.

So, the question is whether what we really wanted to assign j with the result of a comparison: bool j = (i != 0); or maybe a more complete conversion that would turn i into a bool as well:

bool i = false;

// ...
bool j = i; // no warning

j = true;   // cleaner way of producing the same result as the post-increment.
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111