What is the preferred way in C ++ for converting an inline type (int) to bool?

When programming in Visual C ++, I think that every developer is used to view a warning

warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' 

occasionally. The reason, obviously, is that BOOL is defined as int and directly assigning any of the built-in numeric types to bool is considered a bad idea.

So, now my question is asked by any built-in numerical type (int, short, ...) that should be interpreted as a boolean, what is your preferred way to actually store this value in a variable of type bool ?

Note: when mixing BOOL and bool, it is probably a bad idea, I think the problem will inevitably appear on Windows or somewhere else, so I think this question is neither Visual C ++ nor Windows. Sub>

Given int nBoolean; I prefer this style:

  • bool b = nBoolean?true:false;

The alternatives are listed below:

  • bool b = !!nBoolean;

  • bool b = (nBoolean != 0);

Is there any preferred way? Justification?

I have to add: since I only work with Visual-C ++, I can’t say whether this is a question related to VC ++, or if there is a problem with other compilers. Therefore, it would be interesting to learn from g ++ or users how they handle the int-> bool case.

Regarding the C ++ standard: as David Thornley notes in a comment, the C ++ standard does not require this behavior. In fact, this explicitly allows this, so it can be considered a weirdness of VC ++. To quote the N3029 project (this is what I have around atm.):

4.12 Logical transformations [conv.bool]

The value of arithmetic, a non-taxable enumeration, a pointer or a pointer to a member type can be converted to a prvalue of type bool. A null value, a null pointer value, or a null term pointer value is converted to false; any other value is converted to true. (...)

+4
source share
7 answers

In C ++, it is not preferable , since C ++ Std just allows integral conversion from int to bool . (Thus, the preferred method for Std would be bool b = i; )

However, judging by other answers, it doesn't even seem like an acceptable way to do this in Visual C ++ (MS), although the MSDN page says

... If you cannot rewrite the expression to use the bool type, you can add "! = 0" to the expression, which gives the type of the bool expression ....

So, we can conclude that MS recommends using the comparison !=0 , although I personally consider this the worst of all the warning changes presented in the question and answers here: “Type” in the source code is bool , and even if it is really just int , by at least you should compare " bool " with !=FALSE , as suggested in some other answers.

+1
source

In the context of using the win32 SDK and MFC, I always try to write this way. He is explicit.

 bool b = (myBOOL != FALSE); 

EDIT: I edited :-) Because I'm not sure that myBOOL == TRUE works for the whole BOOL implementation and I can assume that FALSE can be set to 0 most of the time.

+8
source

The correct option, which I would assume:

 bool b = static_cast<bool>(val); 
+4
source

I voted for

 BOOL nBoolean; bool b = (nBoolean != 0); 

Cause? Since BOOL is allowed for int, you should compare it with int when converting to bool. The other two methods: !!nBoolean and nBoolean?true:false treat nBoolean as a boolean and therefore perform an implicit cast conversion.

+2
source

Three good ways:

 static_cast<bool>( whatever ) bool( whatever ) !!whatever 

Personally, I prefer the latter, but nix people can react negatively (they are not needed for these compilers, therefore they are not familiar with this idiom).

One reason that the latter is good is because it closes Visual C ++ (stupidity suppression).

Wrong methods include comparing with true or false , especially with the first.

Cheers and hth.,

+2
source

I try to write like that always ..

 bool b = !!myBOOL; 

Clearer (like an English speaker, I'm used to double negatives ....)

It is also safer and avoids errors:

 bool b = (myBOOL = FALSE); //oops! 

In addition, I believe that a boolean should never be compared using == or != , And && should be used. As soon as == or != used, the logical variable is no longer considered as a logical variable, but as an integral value that defeats the goal of the logical one.

+1
source

If this bothered me, I would write a helper function like

 inline bool to_bool(BOOL b) {...} 

which will hide one of the proposed implementations inside it. And never think about it again :)

-1
source

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


All Articles