by banachtarski · 9y ago · 0 comments
Returning a stack allocated value does not invoke the move constructor. It doesn't copy either. This is a separate optimization that existed even prior to C++11 called the "named return value optimization" or NRVO for short. And by "sufficient new compiler" you really should just say every compiler. No compiler I know of in existence used by actual people doesn't implement this.
Here's a demo: http://ideone.com/QRqj5P
It's VERY important that this optimization isn't confused with move construction as the latter would actually be extremely less performant than what actually happens.
Consider this example: http://ideone.com/T872Oj
In F1 the compiler can NRVO away the move.
In F2 it cannot.
Both will fail to compile if you delete the move constructor.
Edit: I see that StephanTLavavej clarified that below.
See also: http://en.cppreference.com/w/cpp/language/copy_elision
Unless the 'named return value' you're returning happens to be a named parameter.
Example: https://ideone.com/TddXn7
The move here actually comes from the return, not passing in the newly constructed object. The compiler is eliding Foo's move constructor in main(), but it can't elide both... even if the function is fully inlined.
In your case a move would happen but if you changed the parameter to a reference I think it would copy.
This results in less code and more manageable semantics.
Returning a value from a C++ function is a "move" in the Rust sense. It's not a move in the C++ sense, since C++ moves involve move constructors.
It's probably this confusion that led to it being written that way, probably should be fixed.
return val; // val is `rvalue` here
This is the part that isn't accurate. "val" is a named reference and is not an rvalue. There isn't any "moving" up the stack that happens. He's likely confusing this with an expiring value (a subtype of rvalue) that happens in code like foo(returns_something());
The returned value from returns_something() is un-named and is about to die after this line executes (lives until foo's invocation unwinds) and so it is an rvalue. You can't have a named rvalue unless you std::move explicitly I don't think.This is the one time in the language that lvalues are automatically moved from, and it's because the language/compiler knows that returning from a function will destroy all local variables, so it's safe to move from them.
Giving the compiler the ability to move implicitly surprises me since moves can have side-effects as you've said.
A lot of C++ resources explain moving solely in terms of rvalues so it's easy to associate the two in the wrong places.
I'm not saying that you are wrong, only that your demo poorly demonstrates what you are arguing.
Aren't copy elision guarantees part of the new C++17 standard?
I have removed a bit where I say that rvalues are same as returned values, to avoid confusing other readers.
However, if the name was a very big string, i.e. something like file contents, and it would be necessary to ensure no copies for performance reasons, the unique_ptr or shared_ptr would come to the rescue
This is also not true at all. You can choose to provide a non-templatized function that only accepts an rvalue reference. For example, http://ideone.com/055ViY does not compile.The purpose of the smart pointers unique_ptr and shared_ptr is to annotate ownership for heap allocated objects (unique vs shared ownership). They do help alleviate the thing you're describing, but shouldn't be used so liberally in that way since they heap allocate!
NRVO is a compiler optimization, not a feature of the language itself.
So it's up to the compiler to elide the copy (making it a compiler optimisation as you say), but the standard says that it has to move if it doesn't elide. This means that returning a named loca variable by value is always the best choice. Again, see "Effective Modern C++" for more.
Rust doesn't, so it's just an optimization. In C++ the code will have different semantics (e.g. if your move ctors have side effects) based on this optimization, so the language needs to define if/how it works.
trait Copy: Clone {}
Copy is nearly always done by memcpy/memmove. When Copy is invoked it won't use the `Clone::clone` method: https://is.gd/2hMyNP.
i.e. Clone is always explicit.Added: Essentially Copy is the same as a move, but the original value is still valid.