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
??