What's the problem with this? I've seen a lot of people so bitwise operator stuff as an optimization. What is wrong with being able to address the bits directly for any length?
It is the only vector type that doesn't return a mutable reference (among other things), so if you're using it in some core abstraction and later on someone wants to use that abstraction with bool types, that code either behaves incorrectly (potentially silently) or doesn't compile.
I ran into this exact scenario in my first job on the architecture team for a massive simulation. Working around it was incredibly annoying.
Edit: That is just my personal experience - the problem, in general, is that it breaks the API/contract/expectations of vector.
The full list (to my knowledge):
No pointers or contiguous layout
As part of the above, it is incompatible with std::span
No .data() function (????)
Broken range-based for loops (for(auto& b : vec)) doesn't compile
Not thread safe. If you have a (normal) vector where you assign a thread to each populated index, it is thread safe (single-owner). In vector<bool>, a single index shares its underlying data with other indices, so this breaks
I think you maybe missed my point. It's that building an abstraction with a foundation of vector that leaks some of its core contracts, like mutable reference based indexing and contiguous data type access, will end up breaking and biting you in unexpected ways. It's also that it breaks the (general) API contract of vector.
The question is more "why *wouldn't I* want this core, generic primitive to adhere to its API contract regardless of the type that I put into it?"
If you look at raw vector and ask "why do I want an array of bools", yea sure, whatever, use a literal array of bools or a bit set or this thing or that thing or who cares.
But as you start zooming out and building stuff around the *concepts* of vectors and arbitrary types, eventually you end up with, say, like, a Vehicle class, built on that abstraction. And then you go to build a vehicle that uses bools in a certain way, for business reasons, and suddenly your code doesn't compile. They didn't necessarily want a literal array of books - they just wanted to use your thing that says "you can use me with any generic type" except WOOPS not bools and only not-bools, for some reason.
I am specifically asking because i cannot recall ever using array of bools (in all of my used languages) over the 6+ years, except for my earliest years in my amateur code where i used array of bools + array of string (for example) vs array<struct=bool + str>
I gave examples in both of my comments for the real world use cases I encountered. Abstractions sitting at the basis of primitives that are used in a large (million+ LoC code base)
If you want a contrived explicit example of a literal array of bools, consider an array of coin flips, or any other data that can be thought of as a (ordered) sequence of truth or decision (yes/no) values.
The case I had at a previous job was we need to know if a retailer was available at a given time. We need to be able to check a lot of them very quickly. So we had a string of 1s and 0s, each one indicated a 15 minute segment of the week. That's too many bits to fit in an integer for a simple bitwise operator. I don't recall the implementation details anymore, but we the I/O for loading thousands of these strings for processing was kind of slow. It was PHP which didn't help.
We were on MySQL, this was during the lamp stack days. And I don't know if MySQL ever added features like that. I don't think it had native json support.
The problem is that it’s inconsistent and leads to unexpected bugs. If I want to use a bitmask because I really care about memory then I’ll use a bitmask
Try grabbing a pointer-to-bool from std::vector<bool> and see what happens.
Vector<bool> isn't that bad in a vacuum, honestly, since you can just avoid it. The real problem is when you write `template<typename T> class MyDataStruct { std::vector<T> innerStorage }` and then create a `MyDataStruct<bool>` and it unexpectedly falls apart. Now you have to build a pointless type trait system to remap bool onto uint8_t or something.
Problem of wrong implementation of bool, not for what use case for it. If you need separate implementation for it, then it should be excluded from start. Assume you have sequence of simple numbers and in that list got 12, 12 also number but wrong one. It’s same for std::vector<bool> differ from rest.
I don't know what this comment is trying to say. Dynamic bool collections with and without bit packing are both legitimate use cases, and of the two, vector<bool> should obviously be the latter because that is how vector already works. If you want a tightly packed collection that breaks vector's invariants, it should be its own type and not a specialization of vector.
I'm not sure what "wrong implementation of bool" is meant to mean either. A bool in C++ is not a single bit, definitionally.
Just when implement class need follow rules to all, not make 1 out of rest. Mem usage be good, but for it can be new one like memCompactVector<bool> etc.
Because std::vector<T> has interface all of its implementations must follow - stuff like iteration, dereference
And while storing booleans as bits in std::vector<bool> shrinks the space usage by nearly 90%, it also means it cannot fully implement the std:vector interface.
Good example is reference - in other vectors, you can take reference to one of its items. You can then use this reference to work with item directly.
auto vec = std::vector<int>();
vec.push_back(10);
// this will obtain reference to 0th item
auto& val = vec[0];
val = 15;
// this will print '15'
std::cout << vec[0] <<std::endl;
This is IMPOSSIBLE with std::vector<bool> because there are no bools inside to reference - and referencing bits is impossible as well.
Instead, it will return weird proxy object that is trying its best to pretend it is bool reference. But it cannot ddo everything - and that breaks interface
View all comments
17
u/Mountain-Ox 11d ago
What's the problem with this? I've seen a lot of people so bitwise operator stuff as an optimization. What is wrong with being able to address the bits directly for any length?