The question is not so obvious as it seems, and I have trouble finding much information about the bool
type in the standard.
According to the C++11 standard, what are the guarantees associated with the bool
type with regards to:
true
and false
?b
be of type bool
, does the assertion (b == true) || (b == false)
hold? Is (false < true)
well-formed, and does it hold?It has two possible values: True and False , which are special versions of 1 and 0 respectively and behave as such in arithmetic contexts.
Boolean values and operations C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0.
bool
types are described in section §3.9.1, Fundamental types. Of relevance here is a sentence from paragraph 6:
Values of type
bool
are eithertrue
orfalse
.47
The reference footnote 47 provides some interesting additional information:
47) Using a
bool
value in ways described by this International Standard as “undefined,” such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neithertrue
norfalse
.
This is just a direct consequence of the standard imposing no requirements on programs with undefined behaviour.
There are no size requirements on bool
, other than the implicit "at least one byte" that applies to all types as a consequence of the C++ memory model.
There are also no requirements on the internal representation of bool
objects, however, due to the requirements regarding integral conversions (true
must convert to 1
and false
to 0
), implementations may be inclined to pick the same representations for true
and 1
, and for false
and 0
, since that makes such conversions unnecessary.
Storage: how much space does it take, ignoring alignment?
Implementation defined, but in practice one byte. It can't usually be smaller, since that's the smallest possible object size. Exceptions are:
std::vector<bool>
packs values so that each takes a single bit; but doesn't really hold objects of type bool
. Other types (like std::bitset
) do similar things, but don't pretend to be storing bool
.Is there any requirement for the value that will be stored to represent
true
andfalse
?
No; just the requirement that, when converted to a numeric type, true
becomes 1 and false
becomes 0. In practice that means that an implementation is likely to use those values; although, on some platforms, other representations might work better.
Values taken: Let
b
be an object of typebool
, does the assertion(b == true) || (b == false)
hold?
The assertion will hold if b
has been initialised or assigned with a valid value. If it's uninitialised, then it may not hold; but you have undefined behaviour anyway, if you use an uninitialised value. In fact, the standard contains a specific footnote (referenced by C++11 3.9.1/6) warning about this:
47) Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neither true nor false.
UPDATE: the question keeps on growing:
Is
(false < true)
well-formed, and does it hold?
Yes, and yes. The operands are promoted to int
, giving 0 < 1
, which is true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With