Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Copy elision and NRVO in C++

Returning by value does not necessarily copy an object. Distinguish optional NRVO for a named local from guaranteed direct construction from a same-type prvalue.

Version: ISO C++23; implementation support may vary

Syntax

T make_named() { T local{}; return local; }  // Optional NRVO
T make_direct() { return T{}; }              // Same-type prvalue: direct construction

The distinction between copy elision and NRVO matters when returning by value. Copy elision can omit copy or move construction even if the constructor has side effects. NRVO (named return value optimization) is optional: with return local;, the implementation may construct local directly as the result object. The named local must be a non-volatile object with automatic storage duration and the same class type as the result, ignoring const and volatile. Function parameters and parameters declared in catch are excluded.

Without NRVO, C++23 treats an eligible local in return local; as an xvalue. Overload resolution can select a move or copy constructor, and the selected constructor must be accessible and not deleted. A deleted move constructor, if selected, does not trigger a retry with copying. Do not write return std::move(local); for this case: the expression is no longer the local's name, so NRVO is unavailable.

Since C++17, in return T{...}; from a function returning T by value, the same-type prvalue initializes the result object directly. There is no separate temporary to copy or move from. This is a language rule commonly called guaranteed copy elision, not optional NRVO. The copy and move constructors may be deleted, but the destructor must be accessible and not deleted.

In const std::vector<int>&, & declares an lvalue reference; it does not take an address. The reference lets you read the vector without copying it and does not take ownership. Binding a local const reference directly to the by-value result extends the temporary's lifetime to that of the reference. Do not return a reference or pointer to a local object destroyed on function exit: it will dangle. To transfer ownership, return the container by value or use an appropriate owning type such as std::unique_ptr.

Examples

#include <iostream>
#include <vector>

struct Token {
    int value;
    explicit Token(int n) : value(n) {}
    Token(const Token&) = delete;
    Token(Token&&) = delete;
};

std::vector<int> make_values() {
    std::vector<int> values{10, 20, 30};
    return values;
}

Token make_token() {
    return Token{42};
}

int main() {
    const std::vector<int>& values = make_values();
    Token token = make_token();
    std::cout << values.size() << ' ' << token.value << '\n';
}

Result

3 42

`make_values()` permits NRVO, but the printed output does not prove it happened. `make_token()` demonstrates direct construction even though its copy and move constructors are deleted.

Related lessons

  • C++ · Lesson 18 · Advanced · Version: 3

Sources