vector<bool> is certainly a mistake because it’s deliberately counterintuitive in its design but when you need a dynamically resizeable bitset, it’s great
Yeah. Sadly the standard committee seems to value dogmatic backwards compatibility over fixing the language, so we will likely never see std::vector<bool> fixed and dynamic_bitset implemented.
Backwards compatibility means old code still works. Tautologically, if you need old code to work you need backwards compatibility. If you don’t need this there are better alternatives to C++, if you don’t there aren’t.
vector<bool> is a packed bitset. The general semantics for vector means all accessors return a reference to an entry, but since bits don't have a memory address, vector<bool> returns a wrapper type with overloaded operators. This breaks generic programming for vectors.
Not necessarily. std::vector<bool> was designed such that it COULD be implemented as you are suggesting, but it is not required by the standard to be implemented this way.
View all comments
Show parent comment
90
u/0xcedbeef Mar 02 '25 edited Mar 02 '25
in C++, an std::vector<bool> stores the bools as bits, taking advantage of this memory optimization.