r/ProgrammerHumor • • 10d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

2

u/TheThiefMaster 9d 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 9d ago

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

4

u/TheThiefMaster 9d 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.