Remix.run Logo
▲ bjackman 18 hours ago

'bool' is still at least 1 byte in C. If you want to store one Boolean per bit you have to manually implement a bitmap.

▲tyromaniac 18 hours ago | parent | next [-]

Or pull out the C++ and use std::vector<bool>,... But you should probably never do that

▲randomNumber7 17 hours ago | parent [-]

I heard the C++ guys regret defining vector<bool> using 1 bit per value.

Also for most code it will be premature optimization to worry about that.

▲KerrAvon 16 hours ago | parent [-]

From what I can tell in a cursory web search, that regret is more about issues with C++ being unable to abstract that field vs bit divergence properly, not about the concept of having the specialization be bit-based, which is sound.

▲tialaramex 15 hours ago | parent [-]

I think your description is technically accurate, but maybe this phrases it better:

std::vector<bool> is a perfectly nice growable bit array type, and if the exact same code were in the C++ standard library named std::growable_bit_array nobody would be annoyed about this type, some people would use it, others would ignore it, nobody would write epic rants about it or name it the singe worst thing about C++.

The problem is that C++ popularized generics, and std::vector<T> is a generic growable array type, you ask for a std::vector<Goose> you get a growable array of your custom Goose type, great idea, very popular these days -- yet std::vector<bool> is not a generic growable array of bool, it's this other thing instead that's similar but not quite similar enough to be a drop-in replacement.

In C++ there is no way for the specialization to be bit-based without it being apparent that you are not in fact getting a growable array of the bool type.

▲codys 3 hours ago | parent [-]

No, that's not really an accurate rephrasing. Your comment indicates the problem is that it was exposed as a specialization, KerrAvon is saying the problem was instead that the specialization wasn't effectively abstracted (hidden) behind the API of vector (ie: implication is that ideally we'd have a better API for vector that might require C++ language changes due to the nature of the problems with vector<bool>. not that we shouldn't have the specialization)

▲viega 17 hours ago | parent | prev | next [-]

A bool does automatically cast to an unsized bit slice.

Meaning, if you have a struct and you want to bit-pack your booleans, you can declare each one as, say, uint8_t some_bool : 1;

You may then do `x.some_bool = true;` etc.

It's a small nicety to avoid bitwise operators, anyway.

▲ReDress 18 hours ago | parent | prev [-]

[dead]