When moving semantics added in C ++ to C ++ 11, decisions are made about where to automatically perform the move.
The following general rule was that implicit movement should occur when copying was legal.
Copying elision is legal if you have an anonymous object that you copy to another instance. The compiler can legally exclude a copy and consider two objects as a whole with a lifetime equal to the union of two objects of life.
While copying elision is legal, you are returning a local variable from the function. This was known as NRVO (called return value optimization). This optimization in C ++ 03 allows you to declare a return value and is created directly in the place where the return value is returned. When do you return retval; The copy does not occur.
This cannot be done without encrusting when you return several different objects in different places of your function.
However, when moving semantics when adding, this place was another place where implicit movement can occur. A return local_var; in a function that returns a variable of the same type as local_var can be deleted, and if you cannot do this, you can implicitly go from local_var to the return value.
source share