Topic: Ref-qualifiers for implicitly-declared assignment operators?


Author: "Niels Dekker - no return address" <noreply@this.is.invalid>
Date: Mon, 22 Dec 2008 22:10:20 CST
Raw View
C++ doesn't allow assigning to a temporary object of a built-in type:

 int int_value = 42;
 int() = int_value;  // Invalid.

So why does it allow assigning to a temporary of a simple POD struct?

 struct foo { int data; };
 foo foo_value = { 42 };
 foo() = foo_value;  // Okay?

The latest Draft, N2800, 12.8 Copying class objects [class.copy]/10
specifies that an implicitly-declared copy assignment operator has
either one of the following two forms (depending on the definition of
the class X):

 X& X::operator=(const X&)
 X& X::operator=(X&)

Shouldn't such an implicitly-declared assignment operator have a
ref-qualifier (&), to prevent assigning to an rvalue?

The assignment operator was explicitly mentioned as one of the use
cases of ref-qualifiers, in "Extending Move Semantics To *this
(Revision 1)", by Daveed Vandevoorde and Bronek Kozicki:
www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1821.htm


Kind regards,

 Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]