Difficulty: Advanced
What is copy elision? Explain RVO and NRVO, what C++17 changed, and why 'return std::move(x)' can hurt performance.
This topic separates people who have read about move semantics from people who understand what the compiler actually does with their code. The surprise for most candidates is that the best copy is the one that never happens.
Copy elision is a compiler optimisation where the copy or move of an object is omitted entirely, so the object is constructed directly in its final destination. Imagine ordering a cake: instead of baking it in the kitchen, boxing it and then carrying it to your table (with a copy at every step), the chef bakes it right on your table. No transport, no duplicates.
There are two flavours worth naming. RVO, return value optimisation, applies when you return a temporary (a prvalue): 'return Widget(1, 2);'. NRVO, named return value optimisation, applies when you return a named local variable: 'Widget w; ...; return w;'. In both cases the compiler constructs the object straight into the caller's storage.
What did C++17 change? For prvalues, elision became mandatory. The language rules were rewritten so that a prvalue does not create a temporary object that then gets copied; it initialises the destination directly. That means 'Widget w = makeWidget();' where makeWidget does 'return Widget();' works even if Widget has deleted copy and move constructors. NRVO, however, is still optional: compilers usually do it, but the type must still be movable or copyable in case they choose not to, and the choice can depend on control flow (for example returning different named locals from different branches).
Now the performance trap. Writing 'return std::move(w);' for a local w seems like a good idea but is harmful: the returned expression is no longer just a name, so NRVO cannot apply, and you force a move construction where zero would have been possible. Compilers such as GCC and Clang warn about this ('-Wpessimizing-move' and '-Wredundant-move'). The correct guidance is simply 'return w;'. Even if elision does not happen, the language treats the returned local as an rvalue first, so a move is selected automatically.
Another interview angle: elision is allowed to change observable behaviour, since constructors with side effects like printing may not run. That is one of the very few places where the standard allows an optimisation to change output, and pre-C++17 compilers could be forced off with '-fno-elide-constructors'. Also, passing by value and then moving is a common sink-parameter pattern, and elision makes it even cheaper when the argument is a prvalue.
So the takeaway for real code: return by value, trust the compiler, do not use std::move on return of locals, and prefer returning objects over out-parameters.
#include <iostream>
struct T {
T() { std::cout << "ctor\n"; }
T(const T&) { std::cout << "copy\n"; }
T(T&&) noexcept { std::cout << "move\n"; }
};
T makePrvalue() { return T(); } // C++17: guaranteed elision
T makeNamed() { T t; return t; } // NRVO: allowed, usually done
T makeBad() { T t; return std::move(t); } // pessimising: forces a move
int main() {
std::cout << "prvalue:\n"; T a = makePrvalue();
std::cout << "named:\n"; T b = makeNamed();
std::cout << "bad:\n"; T c = makeBad();
(void)a; (void)b; (void)c;
}
Typical output with GCC, Clang and MSVC in C++17 mode. The prvalue case is guaranteed; the named case relies on NRVO which is common but optional; the std::move case cannot be elided.
copy elision, RVO, NRVO, C++17, return by value