skip to content

In C++17, what separates return Report{}; from returning a named local Report, and which of the two elisions is guaranteed?

level: middleimportance: must knowfreq 60%

answer

  1. a prvalue is not yet an object
  2. permitted versus required
  3. the named local's conditions
  4. -fno-elide-constructors as a probe

basics

~20 s

return Report{}; returns a prvalue, and C++17 guarantees it initializes the caller's object directly with no copy or move. Returning a named local relies on NRVO, which compilers may apply but need not; without it the local is moved.

solid answer

~40 s

Since C++17 a prvalue such as `Report{}` is not a temporary that gets copied; it initializes whatever object it is used to initialize, here the caller's result object. That is often called RVO, and it is **guaranteed**: no copy or move constructor runs, and none needs to exist. Returning a named local, `Report r; return r;`, is **NRVO**: the standard *permits* the compiler to construct `r` directly in the result object when the operand is the name of a non-volatile automatic object (not a parameter) of the return type. It is not required, so the copy or move constructor must still be accessible, and when it is skipped its side effects disappear. With Clang and `-fno-elide-constructors` in C++17 mode the prvalue version still constructs once, while the named version prints an extra move.

code

cpp · 22 lines
cpp
#include <cstdio>

struct Report {
    Report() { std::puts("  construct"); }
    Report(const Report&) { std::puts("  copy"); }
    Report(Report&&) noexcept { std::puts("  move"); }
};

Report make_prvalue() { return Report{}; }        // RVO: guaranteed since C++17
Report make_named() {
    Report r;                                      // NRVO: permitted, not required
    return r;
}

int main() {
    std::puts("prvalue:");
    Report a = make_prvalue();
    std::puts("named:");
    Report b = make_named();
    (void)a;
    (void)b;
}

go deeper

for a junior

Know the names: returning a prvalue such as Report{} is guaranteed elision since C++17, and returning a named local is NRVO.

for a middle

Explain why C++17's prvalue rule needs no constructor at all, while NRVO is optional and still needs an accessible copy or move constructor.

for a senior

Use -fno-elide-constructors and constructor logging to show which returns are optional elisions, and stop code from depending on constructor side effects.

for a principal

Explain to a team why C++17 is the baseline that makes value-returning factories, including ones for non-movable types, a safe default.

## Two kinds of return operand A `return` statement copy-initializes the function's result object from its operand. What that means depends on the operand's **value category**: - `return Report{};` returns a **prvalue**: an expression that says how to initialize an object, not an object itself. - `Report r; return r;` returns the name of an existing **local object**. Interviewers often call the first "RVO" and the second "NRVO" (named return value optimisation). The standard's umbrella term is **copy elision**. ## What C++17 changed for prvalues C++17 rewrote initialization so that a prvalue of the same class type initializes the destination directly. The standard's own example is `T x = T(T(T()));`, which value-initializes `x` once. Applied to returns: - no temporary is created, so there is nothing to copy or move; - the copy and move constructors are not called, and they need not exist or be accessible; - the behaviour does not depend on the optimisation level or on `-fno-elide-constructors`, because it is a language rule rather than an optimisation. Before C++17 the same elision was merely permitted, and the code still had to compile as if a copy or move might happen. ## NRVO: permitted, not promised The copy-elision clause lets an implementation omit the copy or move from a local into the result object when: 1. the operand is the **name** of an object, not an arbitrary expression; 2. that object is **non-volatile** with **automatic storage duration**; 3. it is **not a function parameter** and not a variable declared in a `catch` handler; 4. it has the **same class type** as the return type, ignoring cv-qualification. When those hold, the compiler may build the local in the result object's storage, and the "two" objects are one. Even then a matching copy or move constructor must be usable, because the elision is optional. And because it is allowed "even if the selected constructor and/or the destructor have side effects", a logging copy constructor may simply not run. ## Seeing the difference The example program prints from each constructor. Compiled with Clang 21: | Build | `return Report{};` prints | `return r;` prints | |---|---|---| | `-std=c++17` | construct | construct | | `-std=c++17 -fno-elide-constructors` | construct | construct, move | | `-std=c++14 -fno-elide-constructors` | construct, move, move | construct, move, move | The flag turns off the optional elisions only. In C++14 mode it also exposes the temporaries that C++17 abolished: one move into the function's return temporary and one from that temporary into the variable. ## Consequences you can rely on - A function may return a prvalue of a type that is **neither copyable nor movable**, because nothing is copied or moved. - Code must not depend on a copy constructor's side effects, because elision may skip them in both forms. - A named return still needs an accessible copy or move constructor, whether or not NRVO happens. - Clang applies NRVO without optimisation flags, so `-O0` is not a reliable way to observe the copies. ## Related cases that follow the same rule Guaranteed elision is not limited to `return`. Anywhere a prvalue of the same class type initializes an object, no temporary is created: - `Report r = Report{};` and `Report r = make_report();` initialize `r` directly; - passing `make_report()` to a parameter declared `Report` initializes the parameter directly; - `return cond ? Report{1} : Report{2};` is still a prvalue of type `Report`, so it is elided too. The rule does not apply when the types differ. `Base b = Derived{};` creates a `Derived` temporary and then constructs `b` from it, slicing off the derived part, because the source type is not the destination type. Likewise a prvalue bound to a reference, as in `const Report& ref = make_report();`, is materialized into a temporary object so the reference has something to refer to; that case belongs to the rules for temporaries and lifetime extension. ## Talking about it in an interview Use the precise split: since C++17, returning a prvalue is guaranteed elision; returning a named local is NRVO, which is optional; if NRVO does not happen, the local is implicitly moved. Saying "RVO is guaranteed" without saying which form is the most common imprecision.

  • Why does the named-return form still need an accessible move or copy constructor if NRVO removes the call?
    Because NRVO is permitted, not required. The program must be valid whether or not the compiler elides, so overload resolution for the copy-initialization still runs and must find a usable constructor. Only the prvalue form escapes that, since C++17 defines it as direct initialization with no constructor call to check.
  • Is a C++ program allowed to rely on a copy constructor's side effects, such as a counter, during return?
    No. The copy-elision rule explicitly allows omitting the construction even when the constructor or destructor has side effects, and for prvalues since C++17 there is no construction at all. A counter in the copy constructor may read lower than the number of returns, and that is conforming behaviour.

  • C++17 guarantees NRVO whenever a function returns a single named local.
  • RVO and NRVO are both mandatory since C++17.
  • A returned prvalue still needs an accessible move constructor in C++17.
  • NRVO cannot skip a copy constructor that prints or counts something.
  • -fno-elide-constructors brings back the copy for return Report{}; in C++17.

Assumes C++17 or later, where elision of a returned prvalue is guaranteed; NRVO has been optional in every standard, C++23 included.