Title
Ref-qualifiers for assignment operators
Status
nad
Section
[library]
Submitter
Niels Dekker

Created on 2008-12-18.00:00:00 last changed 171 months ago

Messages

Date: 2010-10-21.18:28:33

Proposed resolution:

A proposed resolution is provided by the paper on this subject, N2819, Ref-qualifiers for assignment operators of the Standard Library

Date: 2010-10-21.18:28:33

[ Frankfurt 2009-07: ]

The LWG declined to move forward with N2819.

Moved to NAD.

Date: 2010-10-21.18:28:33

[ Batavia (2009-05): ]

Move to Open. We recommend this be deferred until after the next Committee Draft.

Date: 2008-12-18.00:00:00

The assignment and equality operators = and == are easily confused, just because of their visual similarity, and in this case a simple typo can cause a serious bug. When the left side of an operator= is an rvalue, it's highly unlikely that the assignment was intended by the programmer:

if ( func() = value )  // Typical typo: == intended!

Built-in types don't support assignment to an rvalue, but unfortunately, a lot of types provided by the Standard Library do.

Fortunately the language now offers a syntax to prevent a certain member function from having an rvalue as *this: by adding a ref-qualifier (&) to the member function declaration. Assignment operators are explicitly mentioned as a use case of ref-qualifiers, in "Extending Move Semantics To *this (Revision 1)", N1821 by Daveed Vandevoorde and Bronek Kozicki

Hereby I would like to propose adding ref-qualifiers to all appropriate assignment operators in the library.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg4472
2010-10-21 18:28:33adminsetmessages: + msg4471
2010-10-21 18:28:33adminsetmessages: + msg4470
2008-12-18 00:00:00admincreate