141

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

7
  • 3
    The casts in your question are redundant, should they be reversed?
    – GManNickG
    Apr 27, 2010 at 21:06
  • 14
    He doesn't mean them to be casts, he means bool t = true; int n = 1; if (t == n) {...} ;
    – egrunin
    Apr 27, 2010 at 21:07
  • 7
    @egrunin: Eh, but true is a bool and 1 is an int anyway. :)
    – GManNickG
    Apr 27, 2010 at 21:19
  • 1
    Right, I meant to state the type of the values.
    – Petruza
    Apr 28, 2010 at 13:15
  • 3
    (int) true is 1 as an integer value, but something as if (pointer) goes through the then part if pointer != 0. The only thing you can assume as true is that false == 0, and true != 0 (and true evaluates to 1 when cast to int) Sep 21, 2016 at 11:38

5 Answers 5

160

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.

23
  • 9
    @Joshua: true is a keyword defined by the language. It can not be redefined by a library. #defines are not allowed to redefine keywords.
    – jalf
    Apr 27, 2010 at 21:04
  • 26
    @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. Apr 27, 2010 at 21:07
  • 2
    @jalf. They're not? See 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
    Apr 27, 2010 at 21:08
  • 3
    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. Apr 27, 2010 at 21:29
  • 3
    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
    Apr 27, 2010 at 22:49
19

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).

11

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.

6

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.

4
  • 4
    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. Apr 28, 2010 at 13:48
  • 1
    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. Apr 28, 2010 at 14:46
  • I too have seen behavior of this kind. Admittedly, the last time I saw it was about 1999. I was using GCC. The language was C. Still, I have indeed seen such behavior.
    – thb
    Nov 15, 2014 at 12:17
  • 1
    This happens because bool is stored in 1 byte, not 1 bit. In a valid program that byte can only ever hold the values 0 or 1, but in the presence of undefined behavior it's quite possible to get a value other than 0 or 1 stored in that byte. The compiler does not compensate for this situation, which can lead to the experience described in this answer.
    – rustyx
    Sep 11, 2021 at 13:11
1

If I write the code:

int a=true;
cout<<a;

The output will be:

1

So yes, you can assume (bool)true==(int)1

Not the answer you're looking for? Browse other questions tagged or ask your own question.