x
is not an temporary, it is a l-value, an automatic variable which resides on stack.
While, &boolVect[0]
is not an l-value.
l-value is an entity whose address can be taken, in short a variable whose lifetime is long enough to reside in the memory so that it can be addressed by an name.
What is problem with vector<bool>
?? It works fine for vector<int>
The C++ standard library provides a specialization of vector for boolean types.
The goal is optimization, to use less size than a usual implementation of
vector for type bool
. A usual implementation for bool
would reserve at least 1
byte for each element. The vector<bool>
specialization usually uses internally only 1
bit for an element, so it is typically eight times smaller. But this optimization comes with a price:
In C++, the smallest addressable value must have a size of at least 1
byte. Thus, such a specialization of a vector needs special handling for references and iterators.
As a result, a vector<bool>
does not meet all requirements of other vectors. The behavior you see is one such example, elements of the vector<bool>
are not a true l-values.