Why does return std::move(local); in a C++ function returning by value make it slower, and when is std::move on return still right?
answer
- NRVO wants a plain name
- a call expression is not a name
- the warning that names it
- subobjects are not move-eligible
basics
~20 sNRVO needs the return operand to be the plain name of a local. std::move(local) is a call, so the object cannot be built in the caller's storage and a move always runs. Keep std::move for subobjects such as members.
solid answer
~40 s`return local;` gets the best available outcome for free: NRVO when the compiler can apply it, otherwise an implicit move. Writing `return std::move(local);` changes the operand from a name to a function-call expression of type `T&&`, which disqualifies NRVO, so the move constructor is guaranteed to run. With a `Report` that logs its constructors, `return r;` prints nothing and `return std::move(r);` prints one move, at `-O0` and `-O2` alike. On a `const` local it is worse: `std::move` yields `const T&&`, which binds to the copy constructor. Clang's `-Wall` reports this as `-Wpessimizing-move` and the parameter case as `-Wredundant-move`. `std::move` on return is right when the operand is not move-eligible by itself: a member of a local (`return std::move(rep.rows);`), a member of `*this` in a function that gives it away, or `*opt`.
code
cpp · 27 lines#include <cstdio>
#include <utility>
struct Report {
Report() = default;
Report(const Report&) { std::puts(" copy"); }
Report(Report&&) noexcept { std::puts(" move"); }
};
Report make_report() {
Report r;
return r; // NRVO: r is built in the caller's object
}
Report make_report_moved() {
Report r;
return std::move(r); // not a name any more: NRVO is impossible
}
int main() {
std::puts("return r:");
Report a = make_report();
std::puts("return std::move(r):");
Report b = make_report_moved();
(void)a;
(void)b;
}go deeper
Know that return local; is already the fastest form, and that adding std::move to it can make things slower.
Explain that std::move(local) is a call expression, so the operand is no longer a name and NRVO is disqualified, leaving a guaranteed move.
Separate pessimizing from redundant moves, keep std::move where the operand is a member or dereference, and turn the two warnings on in CI.
Make the warnings errors in shared build settings, so the habit is corrected by the compiler rather than in every review.
## What `return local;` already gives you For `Report make_report() { Report r; ...; return r; }` the language provides two layers: 1. **NRVO**: the compiler may construct `r` in the caller's result object, so neither a copy nor a move happens. 2. **Implicit move**: if it does not, the name `r` is treated as an rvalue and the move constructor is chosen. There is no third layer that `std::move` could add. The best case is already zero constructor calls. ## What `std::move` takes away `std::move(r)` is a function call whose result is an xvalue of type `Report&&`. The elision clause applies only when the `return` operand is the **name** of a local, so the call expression disqualifies NRVO. What remains is ordinary copy-initialization of the result object from an rvalue: one move constructor call, every time. | Return statement | Constructors run (Clang 21, -O0 and -O2) | |---|---| | `return r;` | none (NRVO) | | `return std::move(r);` | one move | | `const Report r; return std::move(r);` | one **copy**, since `const Report&&` binds to `const Report&` | For a type that holds a `std::vector`, a move is cheap but not free: it copies pointers and nulls the source, and the moved-from local's destructor still runs. For a type whose move costs as much as a copy, such as one holding a large `std::array`, the pessimization is a full copy. ## What the compilers say Clang prints `warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]` with a note `remove std::move call here`. For a by-value parameter, where NRVO was never possible and implicit move already applies, it prints `warning: redundant move in return statement [-Wredundant-move]`. Both are in Clang's `-Wall`. GCC documents `-Wpessimizing-move` as enabled by `-Wall` and `-Wredundant-move` by `-Wextra`. Treating these as errors in CI stops the habit from spreading. ## Where `std::move` on return is right Implicit move covers only a returned **name** of a local or parameter. Anything else is an lvalue that will be copied unless you move it explicitly: - **A member of a local:** `return std::move(rep.rows);` returns the vector without copying, while `return rep.rows;` copies it. - **A data member of `*this`** in a function meant to hand it over, such as `std::vector<Row> take_rows() && { return std::move(rows_); }`. - **A dereferenced wrapper:** `return std::move(*opt);` or `return std::move(*ptr);`. - **An element of a local container:** `return std::move(batch.back());`. In each case the operand is not a plain name, so neither NRVO nor implicit move applies, and `std::move` is what turns a copy into a move. ## What changes across standards The pessimization is the same in every standard since C++11, but the set of "redundant" cases grew: - **C++11**: a returned local or by-value parameter is implicitly moved, so `std::move` on it is redundant (or pessimizing for a local). - **C++20**: a returned local can be moved into a different return type's rvalue-reference constructor, so `std::move` became redundant there too, for example a `Derived` local returned as `Base`. - **C++23**: a returned rvalue-reference variable is an xvalue, so `T&& f(T&& x) { return x; }` compiles without `std::move`. ## Why the habit persists - Advice written before C++11 often said to avoid returning large objects by value, and `std::move` looks like the modern fix. - In other contexts, such as passing a local to a function for the last time, `std::move` really is the right tool, so it seems consistent to use it on `return` too. - The cost is invisible in tests: the program is correct either way, only slower. ## A worked review A pull request contains this factory for a report builder: ```cpp Report make_report(const std::vector<Row>& input) { Report rep; rep.rows.assign(input.begin(), input.end()); rep.title = "daily"; return std::move(rep); // reviewer: remove std::move } std::vector<Row> extract_rows(Report rep) { return std::move(rep.rows); // reviewer: keep, a member is not move-eligible } ``` The first `std::move` turns a likely zero-cost NRVO into a guaranteed move and triggers `-Wpessimizing-move`. The second is required: `rep.rows` is a member access, so without `std::move` the whole vector is copied. The same reviewer rule explains both lines. ## Rule to apply in review Remove `std::move` from `return` when the operand is the plain name of a local or by-value parameter. Keep or add it when the operand is a member, a dereference or any other non-name expression.
- Why is return std::move(param); for a by-value parameter called redundant rather than pessimizing?NRVO never applies to parameters, so there is nothing for `std::move` to block. The implicit move on return already treats the parameter name as an rvalue, so the explicit `std::move` produces the same single move. Clang reports it as `-Wredundant-move`: harmless, just noise.
- In a C++ function, should return std::move(result.rows); be kept when result is a local struct?Yes. `result.rows` is a member access, not the name of a local, so it is not move-eligible and `return result.rows;` would copy the vector. `std::move` turns that into a move. The pessimization rule only concerns returning the whole local by name.
- return std::move(local) guarantees the object is moved, which is faster than RVO.
- Without std::move, a returned local is copied.
- std::move on return only matters in debug builds; the optimiser removes it.
- return local.member; is moved implicitly because local is about to be destroyed.
- std::move on a const local still moves it.