Trying to do the following:
int A = 1; bool B = (bool)A;
I get a warning when compiling: warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning). Why? How to deal with it!?
Did not drive. The fact is that now all compilers are optimizing. In this context, it turns out that the variable A is not needed at all. And the compiler can easily simply reduce everything to
bool B = true; . At least if the code is exactly like that. If not, then please give a more advanced example.
The warning C4800 will respond best to MSDN .
This warning is generated when this is a bool. This is the case where the variable has been defined as the type bool. If you cant be bool, then you can add "! = 0" to the expression, which gives the expression type bool. Casting the expression for the bool will not be a warning, which is by design.
In short, in a simple way, Microsoft says: "Do not use a cast to a boolean type." Probably the problem is that the compiler still needs to do an extra step. Namely insert test
What do you wonder about this warning? He absolutely hints at you on an implicit type conversion. Want to do that? No problem: use
static_cast for example or a C-style cast (less preferred)