When is explicit move needed for a return statement?
Asked Answered
S

1

20

In a comment to another question Jonathan Wakely responds to my statement:

You never need explicit move for a local variable function return value. It's implicit move there

->

... never say never ... You need an explicit move if the local variable is not the same type as the return type, e.g. std::unique_ptr<base> f() { auto p = std::make_unique<derived>(); p->foo(); return p; }, but if the types are the same it will move if possible ...

So it seems sometimes we may have to move a local variable on return.

The example

std::unique_ptr<base> f() { 
  auto p = std::make_unique<derived>();
  p->foo(); 
  return p; 
}

is nice in that it gives a compilation error

> prog.cpp:10:14: error: cannot convert ‘p’ from type
> ‘std::unique_ptr<derived>’ to type ‘std::unique_ptr<derived>&&’

but I'm wondering whether there is a good chance to detect this in general -- and is this here a limit of the language rules or of unique_ptr??

Sparse answered 5/7, 2013 at 4:21 Comment(0)
L
23

Update:

Explicit move should not be needed in modern compiler versions.

Core DR 1579 changed the rules so that the return value will be treated as an rvalue even when the types are not the same. GCC 5 implements the new rule, for C++11 as well as C++14.

Original answer:

This is not a limitation of unique_ptr, it's a limitation of the language, the same limitation applies to any return statement that calls a converting constructor taking an rvalue reference:

struct U { };

struct T {
  T(U&&) { }
};

T f() {
  U u;
  return u;  // error, cannot bind lvalue to U&&
}

This will not compile because [class.copy]/32 says:

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.

This means that the expression in a return statement can only be treated as an rvalue if it is eligible for copy/move elision (aka NRVO) but that is too restrictive because it means it only applies when the type is exactly the same, even though the variable is always going out of scope so it would be reasonable to always treat is as an rvalue (technically as an xvalue, an expiring value.)

This was recently suggested by Richard Smith (and previously by Xeo) and I think it's a very good idea.

Lindell answered 5/7, 2013 at 7:42 Comment(9)
I originally sent this suggestion to Mike and Richard a long time ago for consideration in Bristol, but it seems they didn't get to it. :(Furious
Let's push to get it in C++17! I'm not sure how to alter the wording though - the current rule has the benefit of simplicity.Lindell
I think we came to the conclusion that the rule should be detached from the copy-elision rules and simply state "if the return-expression is the name of an automatic local variable or function parameter, move it".Furious
Oh, and here is the related Core / Evolution issue. Going through my mails again, it seems that Jeffrey Yasskin actually beat me to it. I added the suggestion to also move from subobjects of local variables, though. :)Furious
@Xeo: automatically moving subobjects of local variables seems dangerous though, as their values might be used in the destructor.Corenda
@Joe: If they're publicly accessible (which they need to be to do return a.x;), then you don't have any guarantees about their values at the time of destruction anyways. Also, all types should have a sensible state after being moved from (likely the default state).Furious
@Furious A sensible state of an individual object isn't necessarily a sensible state in the context of a larger object which it contains. It certainly shouldn't for public members (as anyone can set them), but there are, with 100% certainty, numerous people who abuse public members and have (hopefully documented) requirements on what they may or may not contain. Therefore such a change could break legacy code.Corenda
The member might not be public, you might be returning it from a friend or member function that can access the object. I agree with @Corenda that moving part of an object could break its invariants, so you'd either need a way to explicitly disable the implicit move, or be conservative and not do it for sub-objectsLindell
This should be updated to reflect the resolution of open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1579Cadency

© 2022 - 2024 — McMap. All rights reserved.