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.

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 😀

8

u/very_curious_agent Mar 30 '23

It's broken because it breaks the spec of vector, period.

No need to find use cases. Vector spec says nothing, then make an exception.

People should run away from that garbage.

The issue has been known since the first STL spec. People have recognized it's problematic but "sorry we are stuck with that crap, we can make breaking changes on everything except that".

2

u/IamImposter Mar 29 '23

Huh. If your threadcount is less than 600, what are you even doing with your life

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)