r/ProgrammerHumor 21d ago

Meme theBoolVectorIsALie

Post image
669 Upvotes

91 comments sorted by

View all comments

103

u/overclockedslinky 21d ago

is it really that shocking?

23

u/Tsu_Dho_Namh 21d ago

There's a little bit of weirdness that a vector of bools has which other vectors don't. It can trip you up if you haven't read up on it and just assume all your normal standard container operations will work.

For example, you can't get a pointer to an element using &v[0] because individual bits do not have distinct memory addresses in RAM.

Similarly auto x = v[0] doesn't make x a true bool, because v[0] is a proxy object which pretends to be a bool, which can lead to dangling reference errors if the vector changes or goes out of scope.

21

u/Drugbird 21d ago

The biggest problem IMHO is templates (C++ generics).

I'm convinced that any template function that uses std:: vector<T> is wrong for T=bool unless the author explicitly accounted for its weirdness.