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.