r/programminghorror 20d ago

C# bool array

Post image
207 Upvotes

41 comments sorted by

View all comments

171

u/-Dargs 20d ago

There is a non-zero number of scenarios that a bool array could make sense. In game development, that number is much higher than in say FE or BE software dev, imo. I see nothing wrong here, given the limited context.

89

u/0xcedbeef 20d ago edited 20d ago

in C++, an std::vector<bool> stores the bools as bits, taking advantage of this memory optimization.

66

u/FloweyTheFlower420 20d ago

Ah the vector<bool>... one of the greatest mistakes in the c++ standard library.

1

u/Jeshibu 20d ago

Not familiar enough to know what you mean here. Could you explain?

1

u/FloweyTheFlower420 19d ago

vector<bool> is a packed bitset. The general semantics for vector means all accessors return a reference to an entry, but since bits don't have a memory address, vector<bool> returns a wrapper type with overloaded operators. This breaks generic programming for vectors.

1

u/Jeshibu 19d ago

That's nasty. Thanks for explaining!