简体   繁体   中英

Why std::bitset::reference::operator~?

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.

由于积分促销, ~true ~1 ,当转换回bool时,这肯定是非零的,因此不是false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM