r/ProgrammerHumor • • 11d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

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?

20

u/wallstop-dev 11d ago edited 11d ago

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

Uber point: It's a giant foot gun edge case.

2

u/amshegarh 11d ago

why the hell would you want an array of bools? like intention wise, curious

10

u/wallstop-dev 11d ago edited 10d ago

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.

-1

u/amshegarh 11d ago

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>

3

u/wallstop-dev 11d ago edited 11d ago

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.

2

u/Mountain-Ox 10d ago

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.

2

u/autumnal-lights 10d ago edited 10d ago

? i mean isn't this perfect for bits tho? like

~ A & ~ B and then just like

a ~ custom u number_large_enough;

the scans are trivial tho :shrug:

and like workday perfectly fits into u64, and 3 u64's perfectly align with the 24 hours? like? that's perf?

1

u/amshegarh 10d ago

That is interesting, but you can directly use pg for that (assuming you had one?)

Otherwise its just an array of ints (instead of one) + bitwise logic

1

u/Mountain-Ox 10d ago

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.

1

u/autumnal-lights 9d ago

not sure why you're being downvoted, array is on the stack, it's the same as a class which packs like 3 ints?

1

u/LeDYoM 9d ago

My impression has always been that they wanted to have an example of class template specialization in the standard.

But probably I am wrong.

17

u/Own_Lake_276 11d ago

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

0

u/Mountain-Ox 11d ago

What kind of bugs? Nothing would be inspecting the memory values of the vector directly.

The problem with a bit mask is it can only work up to 64 bits afaik. What if you have hundreds of bools that you need to manage?

7

u/telionn 11d ago

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.

4

u/narrill 11d ago

What if you have hundreds of bools that you need to manage?

Then you have a separate type that works the way vector<bool> does now, allowing vector<bool> to, you know, be a vector.

1

u/Bitter_Ad3906 10d ago

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.

1

u/narrill 10d ago

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.

1

u/Bitter_Ad3906 8d ago

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.

2

u/yota-code 11d ago

Adressing bits directly is nice but bool is a well known type by itself and has a size of 1 octet everywhere else... Seems inconsistent 

3

u/haitei 11d ago edited 10d ago

What is wrong with being able to address the bits directly for any length?

Nothing, that's an excellent feature for a dynamic bitset. But when I ask for a dynamic array of bools, I want a dynamic array of bools.

1

u/Carbon_Cotton 10d ago

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