简体   繁体   中英

`std::bitset::reference` assignment operator?

I was reading the documentation of std::bitset::reference , and I am not sure to understand why, there are two assignment operators defined:

reference& operator=( bool x );
reference& operator=( const reference& x );

Are the two declarations for optimization reasons, or because the compiler-generated copy function would not have the expected result?

A typical proxy reference class stores a pointer (or reference) to the bitset and the index of the bit referred to (or something equivalent to these). A memberwise copy assignment, if it compiles, would in all likelihood simply reseat the "reference".

But you want b[0] = b[1] to assign the value of the second bit to the first bit, rather than reseat the proxy reference returned by b[0] to refer to the second bit. Hence the need for the second overload.

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