r/ProgrammerHumor • • 10d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

502

u/Bloodgiant65 10d ago

The problem is, especially with multithreading, etc. you can get into issues if you try to use a vector<bool>. It would have made much more sense to have the vector<bool> work like anything else, and introduce a std::bitset or similar for this kind of behavior.

265

u/Blue_Moon_Lake 10d ago

But because C++ never fixes past mistakes by breaking behavior, it'll never happen and they'll never do the sensible thing.

69

u/ImpulsiveBloop 10d ago

This is why I tend to just stick to C stdint types when I want specific data types while saving space. Consistent, configurable, and can be typedef-ed for clarity.

57

u/bwmat 10d ago

We just use vector<char> when we need it at work lol. 

45

u/Bloodgiant65 10d ago

Yeah I think that is pretty normal, unfortunately. Though to be explicit I would normally use uint8_t instead.

19

u/ImpulsiveBloop 10d ago

Yeah, I recall there being a difference between using char and uint8_t, too. But I can't be bothered to remember what. Overflow or something.

Unsigned char would be better. But that is more stuff to type out, and less clear on what it is imo.

26

u/nfitzen 10d ago edited 10d ago

From a C perspective, since IDK C++: char is defined to be the smallest addressable unit (in other words, a machine byte, which may be more than 8 bits on cursed hardware). Since CHAR_MAX >= 127 per the ISO C standard, it's probably the best thing to use for a hacky boolean type (of course, when bool doesn't work). uint8_t could also be used, but I feel like it communicates that we need 8 bits specifically rather than "the minimal amount the machine can address individually."

Edit: Also, unsigned char might be my first instinct, but it doesn't particularly matter. Any non-zero value is truthy to C. So I'd probably use char since I'm a fan of brevity.

7

u/ImpulsiveBloop 10d ago

Thank you. Just pretend I put in the effort to say this myself lol

6

u/bwmat 10d ago

If you're actually using the elements as booleans, only zero/non-zero matters, so char, signed char, unsigned char (yeah these are actually 3 separate types lol cpp) are all the same in this context

3

u/ImpulsiveBloop 10d ago

Yeah, I just meant in general. 

They are different, was my point, so they shouldn't be used interchangeably without consideration.

2

u/Scolmann 10d ago

Is there any behavior that breaks from them changing it to be bytes though?

It would take more memory but logically the code should be the same unless someone's doing bitcast BS with it

2

u/Blue_Moon_Lake 10d ago

It breaks multithreading.

6

u/TheThiefMaster 10d ago

No the current behaviour breaks multithreading

→ More replies (6)
→ More replies (2)

47

u/Usual_Office_1740 10d ago

I'm not going to try defend the design of std::vector<bool> because it looks annoying to develop around, but this post got me wondering, so I Googled it. The underlying bit packing behavior and implementation is a remnant of C++98 with addons over years.

This design was set down before algoriths, iterators, ranges and during a time when concurrent programming was not the norm, if it existed at all.

I think this was mediocre idea that has aged like warm milk in a hot car. Not the terrible atrocious design people seem to make it out to be.

44

u/ben_g0 10d ago

The bit packing behaviour can be genuinely useful as it saves memory and uses the cache more effectively. I have no problem with the implementation in and of itself.

The problem IMO is just that it is mapped to the vector<bool> type, while it behaves differently than any other vector in some cases, and its elements aren't really bools and don't act exactly the same. Even without concurrent programming.
The "vector of bits" that vector<bool> actually is, is still very useful to have in the standard library, but would be better as a dedicated bitvector type. And vector<bool> should just act like any other vectors and just store a list of bools.

10

u/Usual_Office_1740 10d ago

That is a reasonable complaint about it. I've only been programming with C++ for a few years. This is one of the many things that gets thrown around a lot in the cpp subreddits but rarely seems to get the context it deserves. I had to learn to frame the comments about performance costs in the same way. There are valid points to what's being said with these comments but there is often more context that much more experienced devs don't specify and it changes how newer developers like myself view things.

I legitimately tried to avoid using std::function and lambdas at one point early on because I read that they were "slow".

9

u/ben_g0 10d ago

About performance costs there is indeed a lot of misinformation and misunderstanding about it on platform like Reddit. A lot of people seem to still think that more CPU cycles means slower, while CPU clock speed is rarely the bottleneck.

Also, it's perhaps a bit of a controversial opinion, but unless what you're doing is very performance critical, I think it's totally fine to use approaches that are not the most optimal but easier to use or that keep the code better organised. Development time often has much more strict limits than compute time, so I think that trades some performance to make it faster or easier to write the program is usually a good trade-off.

3

u/Usual_Office_1740 10d ago

I agree about your maybe controversial opinion on development time and ergonomics being more critical than performance time. I am pushing myself to just get something written and working instead of fixating on whether it is the optimal decision or even a good way to do it.

I read once that there have been studies done in grocery stores where often times to much variety for a single item can reduce the overall sales figures for that item. People can't decide what bag of Lays they want and opt for not getting Lays at all rather than making a decision between one of twenty options. Things sometimes sell better when there are only a few of the most popular flavors because the impulse buy response can kick in.

In C++ there are a dozen ways to do most things and there are trade-offs for most of them. I think I've been struggling with that for a while and your opinion is one way I'm leaning into to pushing passed this observation. Picking what looks to be the most ergonomic option or even just the first option that comes to mind.

As far as performance goes. I was definitely affected by what is said on reddit. I'm a newer developer. I don't want to write buggy sluggish software. If people say that's slow I don't want to do it that way. It wasn't really until I followed a book for writing a terminal based text editor called Kilo earlier this year that I stopped bothering to consider that at all. When I saw how snappy and responsive that project stayed even with the number of memcopies and system calls being done to a heap allocated representation of a 1000k line text document with syntax highlighting every key stroke that I began to get a sense of just how powerful modern CPU's are and how little my struggling with the few hundred extra assembly instructions I'm agonizing over will ever matter in the project's I'm capable of producing.

Those concerns are probably valid for some and I like to think those that make those comments are often people working in high performance industries were their experience has colored there thought process. It is slow to them based on the way they view things and the criteria they've had to learn to evaluate against over years of experience in an industry where that matters. That doesn't mean I should assume it's slow based on what I need.

→ More replies (1)

3

u/Scared_Accident9138 9d ago

Concurrency wasn't supported by the C++ standard itself back then, so there also wasn't a need to specify what happens in case of concurrent access

→ More replies (4)

16

u/Eearslya 10d ago

That's the fun part, we already do have std::bitset.

9

u/setibeings 10d ago

alright, then we need to replace vector with a type that doesn't randomly do something different when it holds bools in particular. lol.

2

u/danielcw189 10d ago

and also give it a name that isn't misleading like "vector"

7

u/Ayjayz 10d ago

It's fixed size though

7

u/Beginning-Junket8979 10d ago

There is a std::bitset but it's more like a std::array<bit, N> in terms of storage allocation and has no iterators.