Why would you want to bloat up your memory and waste 87% of your space? C++ fixed a mistake that the bool type introduced in C for simplicity of implementation. If you don't like vector bool for whatever reason then feel free to use vector uint8_t for the C style feeling
You bloat up the memory in other ways, since bits can only be accessed through bit shifting and boolean operations, and then need to be copied into an int8 anyway. Even if you just want to branch, the CPU will do bit arithmetic. There's specific reasons for when you want to store bools as bits, and not as bytes. VERY SPECIFIC reasons. Like accessing data that is already stored as packed bits, like a register. Or when you are absolutely sure you need to pack bits for dense computations (think: chess engine).
That's why everyone here agrees that it should have been a std::bit_vector class, while std::vector<bool> should have been one byte per bool. It's a landmine in various ways. For example, it breaks all basic assumptions you have about such a vector, like sizeof(T) * n being the total size of the vector in memory.
This kind of stuff is popular in the embedded world though. Working with processors with 8K of program memory and 128 bytes of RAM and some of those processors have single bit instructions. It's one of the reasons I have to delve into assembly.
Then you typically still lay a struct on top, and not an array.
But I know all about smart cards and densely packed formats, that's why I am mentioning "specific reasons". Chess engine is also a good example, 8x8 bits fit neatly into an uint64. Piece movement turns into bit shifts, checking legal moves is mostly AND and OR.
Otherwise, you're trading that 87% "dumb" memory for easily 5x the number of instructions, from a simple array lookup to bit arithmetic. "Small" isn't always good, especially on 64-bit architectures.
a[0] is no longer a bool&, instead a custom type pretending to be a bool& via operator overloading
can't a.data()
can't iterate via auto&& you can, but it suffers from the same issues as the aforementioned a[0] thing
access to disjoint indices is not necessarily thread-safe
iterator may not be a LegacyForwardIterator, preventing use of many standard iterator-taking functions (with an error at compile time, or at runtime, or UB if you try)
and it's not even guaranteed to use this optimisation
View all comments
1
u/TerryHarris408 11d ago
Why would you want to bloat up your memory and waste 87% of your space? C++ fixed a mistake that the bool type introduced in C for simplicity of implementation. If you don't like vector bool for whatever reason then feel free to use vector uint8_t for the C style feeling