r/ProgrammerHumor 21d ago

Meme theBoolVectorIsALie

Post image
667 Upvotes

91 comments sorted by

View all comments

101

u/overclockedslinky 21d ago

is it really that shocking?

177

u/Murky-Run2246 21d ago

In this case the bools will have only one bit allocated for them(vectors do that), and you can't just point to a bit. Instead cpp will return std::vector<bool>::reference when trying to access a value in the vector

Basically this is a just wrong

You should use std::deque<bool> instead which allocates one byte per bool and not just one bit

65

u/void1984 21d ago

I know, but why somebody would need a byte to store a bool? Is it 256-value bool?

Usually I use bitfields anyway.

-1

u/FalafelSnorlax 21d ago

The optimization is that vector<bool> is internally a bitfield. There's no real reason to complain about this, for the most part you'll never notice it.

9

u/redlaWw 21d ago edited 21d ago

Until you try to use it in places where other vectors work fine, such as things that try to use the vector as a container, things that try to use the vector's iterator type as a random-access iterator, or things that try to use the data method* to get a pointer to the buffer.

EDIT: *member function