r/ProgrammerHumor • • 10d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

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.

84

u/Semper_5olus 10d ago

Oh. So a bool is, for whatever reason, a different size if it's in a vector.

And that makes generic functions hard to write without checking for this specific case.

I didn't know that.

That is annoying.

64

u/bwmat 10d ago

The reason is that the people working on that part of the spec felt clever that day

But not clever enough to realize they should have specified it as std::dynamic_bitset (or something like that) instead

53

u/KaMaFour 10d ago

Programmer feeling clever should be a massive alarm bell for everyone

2

u/Interesting-Crab-693 9d ago

I'll remember that in my object oriented programing class next year!

16

u/pyxlmedia 10d ago

It's Not a vector nor does it store bools.

10

u/cinnamonjune 10d ago

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`.

1

u/SubstituteCS 10d ago

std::vector<bool> stores values using bits to shave off space.

In a vacuum this makes a lot of sense as 8 bools only take 1 byte instead of 8 bytes.

In practice, this causes a lot of headaches down the line and can easily lead to problems if someone isn’t aware of this specific nuance.

std::vector<T> is essentially an List<T> if you know C#

8

u/Abadabadon 10d ago

Oh damn really? So you couldn't do a pointer++ traversal across it?

14

u/Drugbird 10d ago

Not in a simple, straightforward way, no.

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>.

6

u/Hot-Category2986 9d ago

This is the explain I needed. Thank you.

1

u/zhaDeth 4d ago

Does that mean it constantly changes from "bit" bools to normal byte bools each time you use it?

1

u/Drugbird 3d ago

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).

0

u/SirPriseTheEigth 10d ago

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…

9

u/Drugbird 10d ago

and if you want byte-aligned behaviour why not just use a vector<uint8_t> and bitshift/mask

Because we have a type system, and I don't want to have to abuse it just to get under some quirky behavior.

also it’s the dynamically allocated version of a std::bitset

Having a std::dynamic_bitset would be great. See e.g. the boost implementation.

Calling it a std::vector is the problem. It's too different from other std:: vectors, which is evident in that it has this huge exception status.

so I think this actually makes sense as long as you know the quirks before making assumption…

This is true for nearly every bad design in programming languages.

1

u/SirPriseTheEigth 10d ago

Well knowing the quirks is what keeps us in a job…

0

u/geekosas 10d ago

That's the way basic type array work... Only objects have references.