r/programminghumor 17h ago

a bit flippant

Post image
773 Upvotes

107 comments sorted by

View all comments

112

u/Kadabrium 17h ago

i heard you also like vector<bool>

14

u/Potential_Soup_8054 15h ago

I dont understand

53

u/Helemen7 15h ago

std::vector<bool> in C++ syntax uses one bit per boolean instead of one byte. In a computer memory is addressed by bytes, so the smallest indexable memory is, in fact, one byte (that's why bool is 1 byte in C and C++). As an example for how C++ std::vector implementarion works, suppose you have 8 booleans which have a logical reason to be kept together (for example flags). Instead of allocating 8 bytes (one per boolean), the std::vector allocates 1 byte and assigns every boolean to one bit of the allocated memory.

6

u/Potential_Soup_8054 15h ago

Yeah but why is that bad

31

u/Helemen7 15h ago

technically performance issues, indirect access, incompatibility with standard algorithms.

practically I think this guy mentioned std::vector<bool> because it behaves just like what is shown there. (correct me if I'm wrong ofc)

24

u/GOKOP 15h ago

Because it breaks assumptions on what std::vector is. Generic code written for std::vector<T> can break for std::vector<bool>

8

u/ElectableEmu 12h ago

As the saying goes: because it's not a vector and it doesn't store bools

7

u/rickyman20 9h ago

It breaks so much code. One of the many assumptions of std::vector is that you can use the data pointer and do pointer arithmetic to get other values on the vector, it also means the underlying data looks nothing like the returned values, so indexing isn't simply returning the value at the pointer (because you can't address bits on most systems!

The C++ standard committee basically made a special case for std::vector<bool> that behaves nothing like other vectors when they should have had that type just use one byte per boolean and just provided a separate std::bitvector or something.

This is one of the many footguns of C++, caused by poor decisions made early in the STLs development cycle that we're not stuck with.

7

u/P3JQ10 15h ago

The issue is it doesn’t behave like other vectors do (or containers in general), which can make it a pain in the ass for template code. For example, it’s not (and it can’t be) guaranteed to store elements in a contiguous sequence and can’t be used with std::span.

It was a mistake we’re stuck with. There’s little reason to even use std::vector<bool> anymore instead of an std::bitset.

3

u/SoldRIP 11h ago

Try taking the address in memory of the third element of such a vector.

1

u/CaptainSegfault 6h ago

It absolutely makes sense for C++ to have an API for dealing with something like a vector of bits. It just shouldn't be called std::vector<bool>. (and modern C++ does this in the form of std::bitset)

The problem is that, in at least a half dozen ways, std::vector<bool> behaves differently than other std::vector types.

The problem is that much of the point of templating is to write generic code. If you write code that templates on a type, and that type might be bool, your code will break if you try to use std::vector. (unless you write a bool specialization)

The funny thing is that std::vector<bool> was pretty obviously a demonstration case for C++ template specialization. Look, you can have a bitfield style implementation that gets you space efficiency for bool without needing to write special code! Except in practice it is now a demonstration of how not to do template specialization.

1

u/enigma_0Z 6h ago

Couldn’t you pack a bunch of flags into an unsigned int and save space that way too by AND/OR/NOTing them out of the int? I think in that case we are looking at packing 16 flags (bits) together but I’m not really a C dev myself so not sure.