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:
- bitfield class members can be a single bit;
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
and false
?
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 type bool
, 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.