I was reading the documentation of std::bitset and I was wondering why std::bitset::reference explicitly define operator~ because I don't see any performance or design reasons. Without it, I think it would work equally well:
bool b = ~mybitset[i];
because the reference would be converted to a bool, on which the ~ operator would be applied.
Any explanation for this design decision?
bool b = true;
b = ~b;
The value of b after this operation is true!
This is because ~ promotes the bool to int of value 1, then performs the bitwise-not on the result, which resolves to -2, and then casts that back to bool which is true.
So it has to provide an operator so that the result is how you would expect it.
Thanks to the integral promotions, ~true is ~1, which is most assuredly nonzero, and hence not false, when converted back to bool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With