c++ - Operator overload which permits capturing with rvalue but not assigning to -
is possible design , how should make overloaded operator+
class c
have possible:
c&& c = c1 + c2;
but not possible:
c1 + c2 = something;
edit: changed objects small letters. c1
, c2
, c
objects of class c
. &&
not logical operator&&
, rather rvalue reference.
for example writing:
double&& d = 1.0 + 2.0;
is 100% proper (new) c++ code, while
1.0 + 2.0 = 4.0;
is compiler error. want exactly same, instead double, class c
.
second edit: if operator returns c or c&, can have assignment rvalue reference, assignment c1 + c2, senseless. giving const here disables it, disables assignment rvalue too. @ least on vc++ 2k10. how double this?
have assignment operator callable on lvalues only:
class c { // ... public: c& operator=(const c&) & = default; };
note single ampersand after closing parenthesis. prevents assigning rvalues.
Comments
Post a Comment