Copy elision
Copy elision is when the compiler constructs an object directly in its final location and skips a copy or move that the abstract machine described. Since C++17, some elisions are mandatory. Named return value optimisation (NRVO) is still optional.
Detailed Explanation
In the abstract machine, return User{"ada"}; creates a temporary User and copies or moves it into the caller. That is waste. The compiler is allowed — and in C++17, for a prvalue, required — to construct the User straight into the caller's object. There is no temporary and the copy/move constructor is not called. It may be inaccessible or deleted; the program still compiles.
Mandatory elision (C++17) applies to prvalues: User u = User{"ada"};, return User{"ada"};, a function parameter initialised by a prvalue. The value is materialised once.
NRVO is the optional optimisation when you return a named local: User u{"ada"}; return u;. The compiler may construct u in the return slot. It may also move. If it cannot (the function returns one of two named locals depending on a branch), it must move or copy. return std::move(u); on a local forbids NRVO because the expression is no longer the name. Write return u;.
Why you care: a factory that returns a unique_ptr or a large vector by value is cheap. Do not add an out-parameter "to avoid the copy" unless you have measured a missed NRVO in a hot path. Do not return std::move(local) "to help".
Elision is not a licence to return a reference to a local. Elision applies to values. A reference parameter or return is a different rule.
Debugging: if your copy constructor has a side effect (a log), elision can skip it. Do not rely on copy constructor side effects. Put logging in a named function.
std::move on a return of a member (return std::move(name_);) is sometimes correct because NRVO does not apply to members. A local should stay unmoved.
Guaranteed copy elision made unique_ptr factories and immovable types returnable as prvalues even without a move constructor (immovable types still cannot be named and returned in a way that requires a move). Most user types should still be movable.
Code Examples
#include <iostream>
#include <string>
#include <vector>
std::vector<int> make() {
return std::vector<int>{1, 2, 3}; // C++17: constructed in the caller
}
std::string named() {
std::string s = "ada";
return s; // NRVO or move; do not std::move(s)
}
int main() {
auto v = make();
std::cout << v.size() << '\n'; // prints: 3
std::cout << named() << '\n'; // prints: ada
}#include <memory>
std::unique_ptr<int> factory() {
return std::make_unique<int>(1); // elided / moved into the caller
}
int main() {
auto p = factory();
return *p == 1 ? 0 : 1;
}Common Gotchas
Side effects in copy/move constructors may not run
logging "copied" in the copy constructor is not a reliable trace.
Elision is not `return &local`
that is still a dangling pointer. Elision only applies to returning *objects* by value.
Two named locals
`return cond ? a : b;` usually cannot NRVO both. The compiler will move from the chosen one.