r/ProgrammerHumor • • 11d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

1

u/TerryHarris408 11d ago

Why would you want to bloat up your memory and waste 87% of your space? C++ fixed a mistake that the bool type introduced in C for simplicity of implementation. If you don't like vector bool for whatever reason then feel free to use vector uint8_t for the C style feeling

3

u/No-Information-2571 10d ago

You bloat up the memory in other ways, since bits can only be accessed through bit shifting and boolean operations, and then need to be copied into an int8 anyway. Even if you just want to branch, the CPU will do bit arithmetic. There's specific reasons for when you want to store bools as bits, and not as bytes. VERY SPECIFIC reasons. Like accessing data that is already stored as packed bits, like a register. Or when you are absolutely sure you need to pack bits for dense computations (think: chess engine).

That's why everyone here agrees that it should have been a std::bit_vector class, while std::vector<bool> should have been one byte per bool. It's a landmine in various ways. For example, it breaks all basic assumptions you have about such a vector, like sizeof(T) * n being the total size of the vector in memory.

1

u/Cheap-Chapter-5920 10d ago

This kind of stuff is popular in the embedded world though. Working with processors with 8K of program memory and 128 bytes of RAM and some of those processors have single bit instructions. It's one of the reasons I have to delve into assembly.

2

u/No-Information-2571 10d ago

Then you typically still lay a struct on top, and not an array.

But I know all about smart cards and densely packed formats, that's why I am mentioning "specific reasons". Chess engine is also a good example, 8x8 bits fit neatly into an uint64. Piece movement turns into bit shifts, checking legal moves is mostly AND and OR.

Otherwise, you're trading that 87% "dumb" memory for easily 5x the number of instructions, from a simple array lookup to bit arithmetic. "Small" isn't always good, especially on 64-bit architectures.

5

u/the_horse_gamer 11d ago

the problem is being implicit

vector<bool> breaks any remotely complex generic code

this optimisation should have been a separate type (tr2 had std::dynamic_bitset)

2

u/TerryHarris408 11d ago

what exactly does it break?

5

u/the_horse_gamer 11d ago edited 11d ago

a[0] is no longer a bool&, instead a custom type pretending to be a bool& via operator overloading

can't a.data()

can't iterate via auto&& you can, but it suffers from the same issues as the aforementioned a[0] thing

access to disjoint indices is not necessarily thread-safe

iterator may not be a LegacyForwardIterator, preventing use of many standard iterator-taking functions (with an error at compile time, or at runtime, or UB if you try)

and it's not even guaranteed to use this optimisation

3

u/Supadoplex 11d ago

can't iterate via auto&& 

I'm pretty sure you can. Did you mean auto&?

1

u/the_horse_gamer 11d ago

seems I remembered incorrectly. will fix.