The main problem though is that vector<bool> behaves differently from any other data type. For instance, separate elements of the vector do not have unique addresses (because addresses are byte aligned).
This means that if you're doing anything with pointers on vectors, particularly if you're writing generic code (templates) then you very quickly run into cases where code is perfectly valid and correct for all types except for bool.
For instance, two adjacent elements in a vector are sizeof(T) apart in memory. Except when T=bool.
What's worse is that the spec says that all vector<T> must return a T& if you do an operator[] on them, but of course vector<bool> can't do this because there are no bools to reference, so the solution they came up with was to say that operator[] on vector<bool> returns a vector<bool>::reference which is an internal class that implicitly decays to bool.
This means that you can do `my_vec[0] = true;`, and you can do `bool b = my_vec[0];`, but if you do `auto b = my_vec[0];` then things go horribly wrong because the type of `b` is not `bool` but instead becomes `vector<bool>::reference`.
The main difficulty is that operator[] doesn't return a bool, but rather it returns some reference type that's convertible to a bool.
So if you do something like bool& begin = &vector[0]; bool& next = begin + 1;
That wouldn't work. I'm not even sure if that's a compilation error or if it just does the wrong thing. Vector<bool> is so cursed I do my best to never use it, but as a consequence I'm also not too familiar with it's details.
You can still use iterators though to iterate over the vector<bool>.
The problem is, especially with multithreading, etc. you can get into issues if you try to use a vector<bool>. It would have made much more sense to have the vector<bool> work like anything else, and introduce a std::bitset or similar for this kind of behavior.
This is why I tend to just stick to C stdint types when I want specific data types while saving space. Consistent, configurable, and can be typedef-ed for clarity.
From a C perspective, since IDK C++: char is defined to be the smallest addressable unit (in other words, a machine byte, which may be more than 8 bits on cursed hardware). Since CHAR_MAX >= 127 per the ISO C standard, it's probably the best thing to use for a hacky boolean type (of course, when bool doesn't work). uint8_t could also be used, but I feel like it communicates that we need 8 bits specifically rather than "the minimal amount the machine can address individually."
Edit: Also, unsigned char might be my first instinct, but it doesn't particularly matter. Any non-zero value is truthy to C. So I'd probably use char since I'm a fan of brevity.
I'm not going to try defend the design of std::vector<bool> because it looks annoying to develop around, but this post got me wondering, so I Googled it. The underlying bit packing behavior and implementation is a remnant of C++98 with addons over years.
This design was set down before algoriths, iterators, ranges and during a time when concurrent programming was not the norm, if it existed at all.
I think this was mediocre idea that has aged like warm milk in a hot car. Not the terrible atrocious design people seem to make it out to be.
The bit packing behaviour can be genuinely useful as it saves memory and uses the cache more effectively. I have no problem with the implementation in and of itself.
The problem IMO is just that it is mapped to the vector<bool> type, while it behaves differently than any other vector in some cases, and its elements aren't really bools and don't act exactly the same. Even without concurrent programming. The "vector of bits" that vector<bool> actually is, is still very useful to have in the standard library, but would be better as a dedicated bitvector type. And vector<bool> should just act like any other vectors and just store a list of bools.
That is a reasonable complaint about it. I've only been programming with C++ for a few years. This is one of the many things that gets thrown around a lot in the cpp subreddits but rarely seems to get the context it deserves. I had to learn to frame the comments about performance costs in the same way. There are valid points to what's being said with these comments but there is often more context that much more experienced devs don't specify and it changes how newer developers like myself view things.
I legitimately tried to avoid using std::function and lambdas at one point early on because I read that they were "slow".
About performance costs there is indeed a lot of misinformation and misunderstanding about it on platform like Reddit. A lot of people seem to still think that more CPU cycles means slower, while CPU clock speed is rarely the bottleneck.
Also, it's perhaps a bit of a controversial opinion, but unless what you're doing is very performance critical, I think it's totally fine to use approaches that are not the most optimal but easier to use or that keep the code better organised. Development time often has much more strict limits than compute time, so I think that trades some performance to make it faster or easier to write the program is usually a good trade-off.
View all comments
759
u/Semper_5olus 10d ago
I see how it would be nice to have that option -- perhaps in another type analogous to long vs short ints -- but isn't a bit all you need?