Consider the following function definition:
X foo() { X x; // perhaps do something to x return x; }Now suppose that as before, X is a class for which we have overloaded the copy constructor and copy assignment operator to implement move semantics. If you take the function definition above at face value, you may be tempted to say, wait a minute, there is a value copy happening here from x to the location of foo 's return value. Let me make sure we're using
move semantics instead:
X foo() { X x; // perhaps do something to x return std::move(x); // making it worse! }
Unfortunately, that would make things worse rather than better. Any modern compiler will apply
return value optimization to the original function definition. In other words, rather
than constructing an |