CKTIS / C++
Copy elision, NRVO and implicit move
Distinguish guaranteed construction from a prvalue, optional NRVO, and implicit move in C++23. Returning by value does not necessarily copy anything, but program correctness must not depend on NRVO being performed.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
T direct() { return T{}; }
T named() { T local; return local; }
T forward_value(T value) { return value; }Copy elision permits omitting a copy or move construction in specified circumstances, even when this changes observable constructor and destructor effects. Do not base program logic on the number of these calls.
Guaranteed copy elision is the common name for prvalue rules applying since C++17. In T direct() { return T{}; }, the result is constructed directly in the destination object of the same type, ignoring cv-qualifiers when comparing types. This is not optional NRVO: no copy or move constructor is needed, but the destructor must be accessible and non-deleted.
NRVO (named return value optimization) can apply to return local; when local is a non-volatile automatic object of the appropriate type, not a function parameter or an exception-handler variable. NRVO remains optional in C++23; requirements on the selected constructor still apply. Writing return std::move(local); fails NRVO's requirement that the expression name the object.
Implicit move does not omit construction. In C++23, a move-eligible expression is treated as an xvalue; this includes typical returns of local objects or by-value parameters. Overload resolution can select a move constructor or an available copy taking const T&; a deleted selected move constructor causes an error rather than a retry using copying. forward_value demonstrates a parameter: it is excluded from NRVO but supports implicit move. This entry describes C++23; the cited pages are an evolving draft, not a frozen C++23 edition.
Reference identifiers covered by this entry: Copy elision, NRVO and implicit move.
Examples
#include <iostream>
struct T {
int value;
explicit T(int n = 0) : value(n) {}
T(const T&) = delete;
T(T&&) = default;
};
T direct() {
return T{10};
}
T named() {
T local{20};
return local;
}
T forward_value(T value) {
return value;
}
int main() {
T a = direct();
T b = named();
T c = forward_value(T{30});
std::cout << a.value << ' ' << b.value << ' ' << c.value << '\n';
}
Result
10 20 30
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.