Does the C4800 have real value?

C4800 warning in microsoft C ++ compiler as described here:

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

does this code:

// C4800.cpp // compile with: /W3 int main() { int i = 0; // try.. // bool i = 0; bool j = i; // C4800 j++; } 

throw warning C4800: "type": forced bool "true" or "false" (performance warning) "

Microsoft, apparently, considers this to be quite important, and has it as a warning of level 3, however, apparently, Klang, apparently, does not think about it, since it has zero complaints about it in all, the maximum level of warning.

Are there any bugs in the real world, someone might think that the C4800 would indicate that it would be useful to turn it on?

+5
source share
1 answer

Basically, it just warns you that you are converting some other integer type to bool , and this conversion is not completely free.

Basically it is present (at least as I see it) to warn that you mix bool with other integer types, which not only leads to a slight decrease in performance, but may also indicate some confusion in the code. Looking at the code in question:

  int i = 0; // try.. // bool i = 0; bool j = i; // C4800 j++; 

... what we mean by an incomplete conversion of some code that previously defined j as an int type. The definition of j been edited so that now it is of type bool , but we still assign it a value from int and (worse) using post-increment on it, both of which will do if j was of type int , but actually not with j with type bool .

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

 bool i = false; // ... bool j = i; // no warning j = true; // cleaner way of producing the same result as the post-increment. 
+3
source

Source: https://habr.com/ru/post/1241665/