In C ++ 03, there are two copies and two movements in C ++ 11.
In both C ++ 03 and C ++ 11, copying / moving is subject to permission, and as such (for example, in the example) copy / moving will not occur.
vector<int> myVector() { vector<int> res; res.push_back(4); res.push_back(5); return res;
The output from res to the return value of myVector somewhat fragile. In C ++ 11, move , which can occur if elision crashes is almost as free (as doing nothing) - 3 pointer instances and 3 pointers are cleared. In C ++ 03, a copy that can occur if a failure in elision can be expensive (memory allocation and O (n) copies).
Elision is the name of operational compilers that can run in certain circumstances when two values โโturn into one, even if this conversion causes a change in behavior (i.e. the as-if test fails).
When this happens, the lifetime is the unification of the two lifetimes.
In the above case, the local variable res and the return value myVector() can be overridden by the NRVO rule (using return value optimization). The return value of myVector can be discarded in v , since it is an expression of the form A a = b , where b is an expression that leads to an anonymous value (the name of such anonymous values โโchanged from C + +03 to C ++ 11, so I wonโt use it), in particular, the return value of myVector() .
This will cause the res v lifetime and the return value of myVector() be merged into one lifetime. In practice, what happens is that res is placed right where the return value of myVector() supposed to be (in accordance with the calling convention), as well as v , and the destruction of this combined value is skipped until v goes out of scope, as in the return and on v = myVector() .
Or, in other words, v is built directly in myVector .