3

Can someone explain why the move assignment operator is (usually) declared as

Foo& operator=(Foo&&); 

Why return a reference and not e.g. Foo or Foo&&? I understand why we want this for the regular assignment operator, due to associativeness rules as (a=b)=c being logically broken (although still compilable) if not returned by reference, but why is this the case when the RHS is a rvalue (xvalue/prvalue)?

8
  • 1
    It's not at all clear what you're asking. You can't return "the object itself" except by reference. If you mean "why not return a copy of the object", then that only works if the type is copyable, and even then copying might be more expensive than you'd like. (a=b)=c requires the first expression (a=b) to be an lvalue whatever the argument type of the assignment operator, so you have to return a reference if you want to support that. Commented Mar 27, 2015 at 1:37
  • @MikeSeymour I changed the question, why not just return Foo? or even Foo&&? What are the "chain" rules that will be broken? Commented Mar 27, 2015 at 1:39
  • Returning Foo is potentially expensive, and impossible if the type isn't copyable, as I said. Returning Foo&& is just weird; you don't want things mysteriously turning into rvalues so that e.g. f(a=b) unexpectedly moves from a without you telling it to. Commented Mar 27, 2015 at 1:42
  • @MikeSeymour thanks, good point with this, you should post (if you want) an answer, as I don't think this issue is super obvious, although now it makes a lot of sense. Commented Mar 27, 2015 at 1:44
  • Another way of looking at it. If all you wanted was an rvalue and you were ok doing a copy, you could always do Foo&& x = Foo(y); This would be the same in my mind as a move operator on y that does a copy so in that case the move operator would be redundant on top of the copy constructor. Commented Mar 27, 2015 at 1:47

1 Answer 1

5

Returning Foo is potentially expensive, and impossible if the type isn't copyable. It's also surprising: (a=b)=c would create a temporary and assign c to that, when you'd expect both assignments to be to a.

Returning Foo&& is just weird; you don't want things mysteriously turning into rvalues so that e.g. f(a=b) unexpectedly moves from a without you telling it to.

Returning Foo& is the conventional way to make the operator behave in an unsurprising way if you chain it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.