Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C4800 have any real world value?

Tags:

c++

clang

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?

like image 503
Lucas Meijer Avatar asked Oct 19 '22 17:10

Lucas Meijer


1 Answers

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.
like image 148
Jerry Coffin Avatar answered Oct 21 '22 08:10

Jerry Coffin