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>.
Yes. You can't have a normal bool pointer or reference into the vector, because there aren't any "regular" byte sized bools in there.
So any time you read a bool, it needs to create a new byte bool for the result. And when you write a bool, it needs to convert that bool to the correct bit flipping operation in the vector.
Particularly when you read a bool it's problematic because you can't e.g. take the address of it and expect to get an address inside the vector (which you can do with any other type of vector).
Tbf c++ does have a specific bool type (unlike c where it’s an int in disguise) - and if you want byte-aligned behaviour why not just use a vector<uint8_t> and bitshift/mask…also it’s the dynamically allocated version of a std::bitset so I think this actually makes sense as long as you know the quirks before making assumption…
View all comments
Show parent comment
154
u/Drugbird 10d ago
Yes it is.
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.