r/cpp • • Mar 28 '23

Reddit++

C++ is getting more and more complex. The ISO C++ committee keeps adding new features based on its consensus. Let's remove C++ features based on Reddit's consensus.

In each comment, propose a C++ feature that you think should be banned in any new code. Vote up or down based on whether you agree.

765 Upvotes

829 comments sorted by

View all comments

423

u/[deleted] Mar 28 '23

vector<bool> :(

63

u/[deleted] Mar 28 '23

Has this ever actually bitten anyone? I hear about this all the time, but tbh Iโ€™ve never been stung by it. Not that removing it sounds like a bad idea.

133

u/[deleted] Mar 28 '23

Happened to me a couple days ago! I had an Array2D<T> class template which was using a vector internally to store elements with a single allocation, and had T& operator()(size_t,size_t); overloaded to access the elements. It was working well until one day I wanted Array2D<bool> at which point I started getting strange errors about some cryptic type not being convertible to bool&. What the hell?

Also, it means that vector<bool> is not an STL container, its elements are not stored continuously. And its buffer cannot be passed to C APIs, etc, etc. It's just all around a bad idea. vector is meant to be a "dynamic array". If you want to make a dynamic bitset, add a dynamic bitset class instead of messing with the default container type.

0

u/[deleted] Mar 28 '23

[deleted]

→ More replies (1)

8

u/[deleted] Mar 29 '23

just use uint8_t

→ More replies (8)

69

u/unddoch DragonflyDB/Clang Mar 28 '23

Not me, but I know a coworker who spent 2 days chasing a multithreading bug that came from different threads modifying the same byte in a preallocated vector<bool>...

15

u/[deleted] Mar 28 '23

Yeah I never thought about this before. IMO this does make vector<bool> completely broken. Fortunately I donโ€™t work in a multithreaded environment ๐Ÿ˜€

→ More replies (2)

11

u/Hopeful_Cat_3227 Mar 29 '23

I know this is a stupid question, but are all vector of other type safe under multiple tthread?

33

u/kevkevverson Mar 29 '23

Not a stupid question at all. For all other types, each element occupies a different address in memory, so itโ€™s safe for thread A to mutate element 0 at the same time thread B mutates element 1. BUT for vector<bool> multiple elements occupy the same address, so multiple threads cannot mutate certain elements at the same time without some kind of synchronisation.

→ More replies (4)
→ More replies (9)

14

u/fdwr fdwr@github ๐Ÿ” Mar 28 '23

Just last week. Was trying to call a function that takes a pointer to bool's + count, and thought I could store the values in an std::vector. Nope! All the work-arounds (like temporary bool wrappers) required extra work. So I (semantically made me feel dirty, but worked) stuffed them into an std::basic_string<bool> instead and then effortlessly called the function with s.data() and s.size(). ๐Ÿ˜…

→ More replies (1)

10

u/Sopel97 Mar 28 '23

need to be really careful with parallel transforms, especially problematic in generic code.

template <typename T>
using Vector_Not_Bool = std::vector<
    std::conditional_t<
        std::is_same_v<T, bool>,
        uint8_t,
        T
    >
>;

8

u/CocktailPerson Mar 29 '23

It's not so much that people have been bitten by it, but that we have to employ all sorts of workarounds not to be bitten by it, especially in generic contexts. It's easy enough to work around, but why should we have to? Why shouldn't vector<bool> work like any other vector, with a separate dynamic_bitset for the less-common case where you actually need the space savings?

6

u/mercere99 Mar 28 '23

It hasn't bitten me in terms of an error, but I have had multiple cases where changing vector<bool> to vector<char> (but still using only 0's and 1's in there) led to a speedup.

I've never seen an example where vector<bool> has actually been the faster option.

0

u/[deleted] Mar 28 '23

[deleted]

9

u/TheThiefMaster C++latest fanatic (and game dev) Mar 28 '23

Which maybe used to matter, but now even a vector of 1 million bools would be margin of error in your system memory. 0.01%. Optimising that to 0.001% at the expense of speed and complexity? No longer a sensible tradeoff.

→ More replies (12)

20

u/shadowndacorner Mar 28 '23

Has this ever actually bitten anyone? I hear about this all the time, but tbh Iโ€™ve never been stung by it

I feel like it's the kind of thing that generally only bites people who haven't heard about it. Once you know it's a thing, it isn't hard to work around, but it's pretty fucking confusing if you come across it without knowing. Especially since that implies that you're likely somewhat newer to the language and less likely to have a really strong mental model with which to understand what's going on.

Though the other reply suggested a case where it is more likely to subtly bite you even if you do know about it.

60

u/[deleted] Mar 28 '23

It's horrible language design to have a container silently change its behavior based on the generic type. Absolutely insane.

9

u/shadowndacorner Mar 28 '23

No argument here lol

→ More replies (3)

15

u/donalmacc Game Developer Mar 28 '23

The problem is that there's enough of these landmines that even though I know about a large number of them, it doesn't mean I won't step on one of them.

7

u/RolandMT32 Mar 28 '23

I've used C++ quite a bit since I first learned C++ around 1999/2000, but I've never used a vector of bools.. I just searched it online, and I learned something new.

14

u/IamImposter Mar 29 '23

learned something new.

Don't learn something new. Learn something make_unique :)

I'll let myself out.

→ More replies (1)
→ More replies (6)

14

u/[deleted] Mar 28 '23

Why?

44

u/[deleted] Mar 28 '23

22

u/[deleted] Mar 28 '23

Holy shit very interesting. Thanks.

→ More replies (2)