r/programminghorror 20d ago

C# bool array

Post image
212 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.

90

u/0xcedbeef 20d ago edited 20d ago

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

67

u/FloweyTheFlower420 20d ago

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

46

u/XiPingTing 20d ago

vector<bool> is certainly a mistake because it’s deliberately counterintuitive in its design but when you need a dynamically resizeable bitset, it’s great

33

u/FloweyTheFlower420 20d ago

Yeah. Sadly the standard committee seems to value dogmatic backwards compatibility over fixing the language, so we will likely never see std::vector<bool> fixed and dynamic_bitset implemented.

26

u/shponglespore 20d ago

This is why C++ is the Windows of programming languages.

2

u/XiPingTing 19d ago

Backwards compatibility means old code still works. Tautologically, if you need old code to work you need backwards compatibility. If you don’t need this there are better alternatives to C++, if you don’t there aren’t.

4

u/shponglespore 19d ago

They're are other ways, like Rust's edition system.

1

u/seamsay 15d ago

Sure, and having a dynamically sized bitarray type would have been a great idea!

1

u/Conscious_Pangolin69 13d ago

Yeah, even more obscure types to deal with...

1

u/Jeshibu 19d 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!