r/ProgrammerHumor • • 15d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

175

u/ImpulsiveBloop 15d ago edited 15d ago

typedef uint8_t bool8;

std::vector<bool8> booleansAsBytes;

Or just use it directly

std::vector<uint8_t> fuckBools;

61

u/dedservice 15d ago

Yes until you have std::vector<T> in a templated piece of library code and you try to do anything remotely complicated with it.

15

u/ImpulsiveBloop 15d ago

It's all use-case, I suppose. Larger programs can afford more refined solutions.

To say using uint8_t as a bool is bad because it can break when making templates is to say wood beams are bad because they will buckle under a skyscraper.

2

u/PastaPuttanesca42 13d ago

It's not that uint8_t breaks vectors, it's that users of the template that don't know it uses a vector internally will use bool instead.

1

u/4xe1 10d ago

Wouldn't they get a compiler error doing so?

1

u/PastaPuttanesca42 10d ago

I'm not completely sure that in some cases there wouldn't be undefined behaviour, but even if it fails to compile the error will likely be horrible and not easily understandable.

1

u/dedservice 9d ago

Using uint_8 isn't bad, but rather it's not an option as a replacement if you're the one writing the templated code. Partually because you don't know that bool was actually the intent when you get given a uint_8, but more importantly because in every single function that templates on T and uses vector<T>, you have to write a distinct version of the function that special-cases vector<bool> in case your users instantiate the template with a bool, which should be very reasonable and users should want to do by default, because bools are a useful datatype.

1

u/ImpulsiveBloop 9d ago

Hence why I said, "larger programs can afford mor refined solutions"... :/

I only mean that uint8_t is not bad for smaller programs because it does what it needs and is unlikely to cause any complications.

6

u/That-Cpp-Girl 15d ago

template <typename T>

class CoolerVector : public std::vector<T> { using std::vector<T>::vector; };

template <>

class CoolerVector<bool> : public std::vector<uint8_t> { using std::vector<uint8_t>::vector; };

1

u/dedservice 9d ago

Yeah I know there are workarounds but there just shouldn't have to be.

6

u/TheDreadedAndy 15d ago

I mean, as long as you know its a problem you can just use std::enable_if_t to block putting bools in your template. Or you can specialize your template to redirect bools to a more sensible type.

It's a fun landmine for those who don't know, though. But C++ is full of those.

14

u/dedservice 14d ago

Yes but it's silly. We could update the language past the footguns from 1989. 

3

u/StaticCoder 15d ago

Surely you mean requires

1

u/4xe1 10d ago

Isn't that the other way around? I'd expect std::vector<uint8_t> to play a lot nicer with templates than std::vector<bool>

1

u/dedservice 10d ago

Yes exactly. But if you're writing template code and don't know who's going to call it, you have to create specific carve-outs or overloads for vector<bool>, where if the designers had just let that be normal, you wouldn't need that special-case handling.