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.
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.
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.
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
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.
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.
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".
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.
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.
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.