Martin Ba
Martin Ba

Reputation: 38775

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

When programming with Visual C++, I think every developer is used to see the warning

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

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

So my question is now, given any built-in numerical type (int, short, ...) that is to be interpreted as a boolean value, what is the/your preferred way of actually storing that value into a variable of type bool?

Note: While mixing BOOL and bool is probably a bad idea, I think the problem will inevitably pop up whether on Windows or somewhere else, so I think this question is neither Visual-C++ nor Windows specific.

Given int nBoolean; I prefer this style:

The following might be alternatives:

Is there a generally preferred way? Rationale?

I should add: Since I only work with Visual-C++ I cannot really say if this is a VC++ specific question or if the same problem pops up with other compilers. So it would be interesting to specifically hear from g++ or users how they handle the int->bool case.

Regarding Standard C++: As David Thornley notes in a comment, the C++ Standard does not require this behavior. In fact it seems to explicitly allow this, so one might consider this a VC++ weirdness. To quote the N3029 draft (which is what I have around atm.):

4.12 Boolean conversions [conv.bool]

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. (...)

Upvotes: 4

Views: 658

Answers (7)

Martin Ba
Martin Ba

Reputation: 38775

There is no preferred way in C++, since the C++ Std simply allows the integral conversion from int to bool. (So the preferred way wrt the Std would be bool b = i;.)

That said, judging from the other answers, there does not even seem to be an accepted way to do it in Visual C++ (MS) although the MSDN page states

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

So one might conclude that MS recommends to use the !=0 comparison, although I, personally, think it's the worst of all the warning-supressing alterantives presented in the question and the answers here: The "type" in the source code is BOOL, and even though it's really just an int at least one should compare a "BOOL" with !=FALSE like has been proposed in some other answers.

Upvotes: 1

Stephane Rolland
Stephane Rolland

Reputation: 39906

In the context of using win32 SDK and MFC, I tend to write it always this way. It's explicit.

bool b = (myBOOL != FALSE);

EDIT: I have edited :-) cause i'm not sure myBOOL == TRUE works for all implementation of BOOL and I can assume that FALSE may have the 0 value most of the time.

Upvotes: 8

Roman L
Roman L

Reputation: 3056

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

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

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

Upvotes: -1

ThomasMcLeod
ThomasMcLeod

Reputation: 7769

I cast my vote for

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

Reason? Since BOOL resolves to an int, one should compare it to an int when converting to bool. The other two methods: !!nBoolean and nBoolean?true:false treat nBoolean as as logical value and therefore perform an implicit cast conversion.

Upvotes: 2

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

Three good ways:

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

Personally I prefer the last, but *nix folks may react negatively (not needed for those compilers, so not familiar with that idiom).

One reason that the last one is good is that it shuts up Visual C++ (sillywarning suppression).

Ungood ways include comparing with true or false, especially the former.

Cheers & hth.,

Upvotes: 2

T33C
T33C

Reputation: 4429

I tend to write it always this way. .

bool b = !!myBOOL;

It is clearer (well as an English speaker, I am used to double-negatives....)

It is also safer and avoids mistakes like:

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

Also, I am of the opinion that booleans should never be compared using == or != rather && should be used. As soon as == or != is used the boolean variable is no longer treated as a boolean but as an integral value which defeats the purpose of boolean.

Upvotes: 1

Viktor Sehr
Viktor Sehr

Reputation: 13099

The correct way I'd assume is:

bool b = static_cast<bool>(val);

Upvotes: 4

Related Questions