views:

507

answers:

7

Can I assume (bool)true == (int)1 for any C++ compiler ?

A: 

No, TRUE can be arbitrary among compilers. Some will use 1, some -1 (all bits set, signed var), other compilers may use a different value. Even if the value is standardized, not all compilers may follow the standard.

FALSE, however, is when all bits are clear, which only happens for the numeric value 0.

Sukasa
He is asking about the `bool` type, not the `BOOL` define.
Franci Penov
Also, there are plenty of APIs that are cross-compiler that use BOOL, TRUE and FALSE, such as WinAPI, that must use consistent values for true.
DeadMG
+30  A: 

Yes. The casts are redundant. In your expression:

true == 1

Integral promotion applies and the bool value will be promoted to an int and this promotion must yield 1.

Reference: 4.7 [conv.integral] / 4: If the source type is bool... true is converted to one.

Charles Bailey
Not sure why it's down-voted, it's the correct one.
GMan
This is true only for primitives. Beware of value of true coming from imports from other libraries.
Joshua
@GMan: Thanks for the vote of confidence. If a compiler promotes `true` to any other integer value then it's not a conforming C++ compiler.
Charles Bailey
@Joshua: `true` is a keyword defined by the language. It can not be redefined by a library. `#define`s are not allowed to redefine keywords.
jalf
@Joshua - it's part of the language, so no library should be defining values for `true`. if you know of one that does it, please let us know so we can avoid it. :-) just like we would avoid any library that defines `while`.
Franci Penov
@jalf: #define's are indeed allowed to define system keywords. The pre-processing phase of C compilation is purely textual, and knows nothing of keywords or C syntax in general. Nevertheless, it is, of course, almost always a bad idea to redefine language keywords.
Dale Hagglund
@jalf. They're not? See http://gcc.gnu.org/onlinedocs/cpp/Macros.html, and at least one of the entries in the International Obfuscated C Code Contest, which once asked "When does a `while` not take a while?" (Answer: when it takes two parameters, because then that entry had `#defined` it to `printf`.)
Ken Bloom
@Ken Bloom: The rules may be different between C++ and C.
David Thornley
I think that that it's quite safe to say that no compiler or library implementing the standard will redefine any keywords with macros because that would make it so that when any user of them used them, they would be in violation of the standard. So, in effect, they're guaranteed by the standard. However, there's nothing to stop you or a third party library from being stupid and redefining them.
Jonathan M Davis
@Joshua: `true` is a C++ keyword, and may not be used for any other purpose. 17.4.3.1.1 in the Standard forbids `#define true ...`, so no conforming program can have `true` mean anything other than the primitive `bool` value.
David Thornley
C99, §6.10.1/1 says: "The expression that controls conditional inclusion shall be an integer constant expression except that: it shall not contain a cast; identifiers (including those lexically identical to keywords) are interpreted as described below;" Though not stated as direct permission, this clearly contemplates the possibility of a macro that's "lexically identical" to a keyword.
Jerry Coffin
@David Thornley: that's almost but not quite true. A translation unit is only prohibited from re-defining key words if it includes a header. A translation unit that was entirely self-contained (didn't depend on any headers) could apparently re-define keywords; whether it should or not is a separate question...
Jerry Coffin
@David Thornley: If I'm reading that right, apparently it's OK to redefine `true` as long as you don't include a header.
dreamlax
If that is true, then headers can obviously not do `#define TRUE 9`. And if a self contained file defines a function that returns a `bool`, and that unit used the preprocessor to change `true` to `9`, then the compiler will simply convert that `9` back to `true` in the return statement. So there is still no way for `true` and `false` to be anything other than what they are unless you personally change them.
Dennis Zickefoose
What I meant was do not assume that a function in a third party library declared in a header file to return bool returns a true that cast as integer returns 1.
Joshua
Oh, and #defines are allowed to redefine keywords. C++1x caused too many problems with its new keywords so that requirement had to be removed.
Joshua
The standard allows the C++ preprocessor to have the ability to redefine keywords, but programs that do so are not necessarily standards compliant.
Ken Bloom
Ken opened a new question for the discussion here: http://stackoverflow.com/questions/2726204/c-preprocessor-define-ing-a-keyword-is-it-standards-conforming
Georg Fritzsche
Well, in case someone wants to use my library and re-defines true as something else than 1, it's their problem, not mine. :D
Petruza
@Jerry: Thanks. I apparently read that too fast. Is there anything actually forbidding redefinition of keywords in general? Why would the ban apply only to translation units without headers?
David Thornley
A: 

You should never check for true, and you should never have to. The value may be completely arbitrary, the only guarantee you have is that false == 0, and that true != false.

Williham Totland
A `bool` value has only two values according to the standard.
dreamlax
While it is true that any non-zero value used where a boolean expression is valid is regarded as `true` conversion between the built-in `bool` and `int` is well defined. However I guess your point is that a test such as `if( isValid == true )` is entirely redundant and I would better be expressed as `if( isValid )`. I agree with that, but that does not make this post correct.
Clifford
+1  A: 

YES. See here.

Roddy
This doesn't look like a useful answer to me. It's an off-site link to a book by Schildt, who is not known as supremely accurate, and I can't read the page anyway (Google says I've either reached my limit or the book isn't readable that way). Please edit this answer to include some actual content.
David Thornley
A: 

I've found different compilers return different results on true. I've also found that one is almost always better off comparing a bool to a bool instead of an int. Those ints tend to change value over time as your program evolves and if you assume true as 1, you can get bitten by an unrelated change elsewhere in your code.

Michael Dorgan
This is an incorrect answer for C++, as `true` is a language keyword with defined behavior. If you refer to a commonly defined macro such as `TRUE`, it is correct.
David Thornley
Could be my experience was with C compilers - I've spent plenty of time with them over the years. My point about directly using mathetical expressions in if statements stands though. We had code that seeing if a bit shift was non zero in an if, then someone else took that same non-zero value and assumed it was 1 and blew stuff up. A simple conversion to true/1 would have prevented that.
Michael Dorgan
+6  A: 

Charles Bailey's answer is correct. The exact wording from the C++ standard is (§4.7/4): "If the source type is bool, the value false is converted to zero and the value true is converted to one."

Edit: I see he's added the reference as well -- I'll delete this shortly, if I don't get distracted and forget...

Edit2: Then again, it is probably worth noting that while the Boolean values themselves always convert to zero or one, a number of functions (especially from the C standard library) return values that are "basically Boolean", but represented as ints that are normally only required to be zero to indicate false or non-zero to indicate true. For example, the is* functions in <ctype.h> only require zero or non-zero, not necessarily zero or one.

If you cast that to bool, zero will convert to false, and non-zero to true (as you'd expect).

Jerry Coffin
+1  A: 

According to the standard, you should be safe with that assumption. The C++ bool type has two values - true and false with corresponding values 1 and 0.

The thing to watch about for is mixing bool expressions and variables with BOOL expression and variables. The latter is defined as FALSE = 0 and TRUE != FALSE, which quite often in practice means that any value different from 0 is considered TRUE.

A lot of modern compilers will actually issue a warning for any code that implicitly tries to cast from BOOL to bool if the BOOL value is different than 0 or 1.

Franci Penov