r/ProgrammerHumor • • 15d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

2

u/Scolmann 15d ago

Is there any behavior that breaks from them changing it to be bytes though?

It would take more memory but logically the code should be the same unless someone's doing bitcast BS with it

2

u/Blue_Moon_Lake 15d ago

It breaks multithreading.

6

u/TheThiefMaster 15d ago

No the current behaviour breaks multithreading

1

u/Blue_Moon_Lake 15d ago

Oh I read it incorrectly. I thought you asked what the bitset behavior was breaking.

Theoretically, it would break all code that try to handle it as a bitset by doing unsafe shitty read/write on the memory address.
But also, it would be extremely easy to do a "replace all" of vector<bool> to bitset when upgrading the code.

3

u/TheThiefMaster 15d ago

Can you manually read/write vector<bool>? I don't think it gives you data pointers at all

1

u/Blue_Moon_Lake 14d ago

Probably, but I never tried to write stupid code on purpose.

2

u/TheThiefMaster 14d ago

But my question highlights a problem with vector<bool> - it can't work in generic code that expects vector to be contiguous, addressable with T*, and have a .data() function that gives you that. Because it doesn't.

It doesn't even support &bool_vec[0] or &*begin() - because neither return actual references, but a reference proxy class. You can't take the address of an rvalue class, so it fails to compile entirely.

It's very well defended, but on the flip side it's incompatible with a lot of generic contexts because it doesn't have the same API as vector<T> generally, only a subset of it. Some of which seems to exist solely for there to be a common subset (like the nested reference type alias).

And the worst part is it explicitly has a member function on its nested "reference" type - so currently it can't be left unspecialised by an implementation.

1

u/Blue_Moon_Lake 14d ago

Yes, everybody says it should have been a different type while vector<bool> should be a regular vector.

4

u/TheThiefMaster 14d ago

And everyone knows it - but as long as any code uses vector<bool>::reference::flip() it's hard to fix.

IMO, the best solution is to make the new type, given the warning that change is coming, and then make the change. End the nonsense.