Operator overload that allows grabbing with rvalue but not assigning

Is it possible to design and how can I overload operator+ for my C class so that this is possible:

 C&& c = c1 + c2; 

but this is unreal:

 c1 + c2 = something; 

Edit: I changed the objects to small letters. c1 , c2 and C are objects of class C && not a logical operator&& , but rather a rvalue reference.

For example, the entry:

 double&& d = 1.0 + 2.0; 

is 100% correct (new) C ++ code, and

 1.0 + 2.0 = 4.0; 

obviously is a compiler error. I want exactly the same, but instead for double, for my class C

Second edit: If my statement returns C or C &, I can assign the rvalue reference, but also assign c1 + c2, which is pointless. Providing a const here disables it, however it also disables the assignment of the rvalue value. At least in VC ++ 2k10. So how to double that?

+7
source share
1 answer

Point the assignment operator only to lvalues:

 class C { // ... public: C& operator=(const C&) & = default; }; 

Note one ampersand after closing the parenthesis. This prevents the assignment of r values.

+10
source

All Articles