CKTIS / C++
Implicit move from a return parameter
In C++23, `return a;` can move from a by-value parameter. This is not NRVO: the parameter is a separate object from the result.
Version: ISO C++23; implementation support may vary
Syntax
template<class T>
T relay(T a) {
return a;
}A by-value sink parameter T a gives the function its own object to work with. Initializing a from the argument is separate from initializing the result. The phrase implicit move from return parameter describes what happens at return a;: in C++23, the eligible name is an xvalue, an expression designating an existing object that can be moved from. In ordinary uses, the name a is still an lvalue.
For return, the rule applies to the bare name, optionally parenthesized, of an automatic variable declared in the current function or lambda: a non-volatile object or an rvalue reference to a non-volatile object. It does not apply to lvalue reference parameters or expressions such as a.member.
Overload resolution can select T(T&&) or T(const T&). For example, a const T source cannot bind to T&& but can bind to const T&. A selected deleted or inaccessible constructor makes the code ill-formed. Implicit move from by-value parameters dates back to C++11. In C++20, a by-value return could retry overload resolution with an lvalue if the rvalue pass failed; C++23 has no such retry.
NRVO can construct a named non-volatile local object with automatic storage duration directly as the result when its class type matches the return type, ignoring cv-qualification. NRVO is optional and excludes function parameters. Since C++17, return T{}; with return type T instead initializes the result directly from a prvalue, with no copy or move. Prefer return a; here: std::move(a) is redundant for the parameter, and applying std::move to an eligible local blocks NRVO in C++23.
Return T, not T&&, when the result must outlive the parameter: a returned reference does not extend its lifetime. C++23 support varies; check that __cpp_implicit_move is defined and at least 202207L.
Examples
#include <iostream>
struct Token {
Token() = default;
Token(const Token&) { std::cout << "copy\n"; }
Token(Token&&) { std::cout << "move\n"; }
};
Token relay(Token a) {
return a;
}
int main() {
Token input;
Token result = relay(input);
(void)result;
}Result
copy move
`copy` marks initialization of `a` from the lvalue `input`; `move` marks construction of `result` from `a` at `return a;`. There is no third operation. This output does not distinguish C++23 from older modes.
Related lessons
- C++ · Lesson 14 · Advanced · Version: 3
Sources
- C++ working draft — The return statement [stmt.return]
- C++ working draft — Unqualified names [expr.prim.id.unqual]
- C++ working draft — Copy/move elision [class.copy.elision]
- C++ working draft — Compatibility with C++20: Expressions [diff.cpp20.expr]
- C++ working draft — Initialization: General [dcl.init.general]
- Working Draft, Standard for Programming Language C++ (N4971) — Feature-test macros