Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Direct construction of a prvalue result

Since C++17, a same-type class prvalue constructs the result directly, without copying or moving the whole object. NRVO is optional, and member initialization can still copy data.

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

Syntax

T make() { return T{args}; }  // same-type prvalue: constructs the result directly
T value = make();             // initialization, not assignment
T make_named() { T local{args}; return local; }  // optional NRVO

Since C++17, including C++23, return T{args}; in a function returning T by value constructs the result object directly. T{args} is a prvalue of the same class type as the result, ignoring const and volatile. This is prvalue result construction: there is no separate temporary T to copy or move from. “Guaranteed copy elision” is the common name for this language rule, not an optional optimization.

In T value = make();, value is the result object of the call returning T by value. The = in this declaration means initialization, not assignment; even the term “copy-initialization” does not imply a copy constructor call. If T{args} selects a constructor of T, that constructor must be usable. For an aggregate T, T{args} can instead perform aggregate initialization without calling a constructor of T. T's destructor must be accessible at the return and not deleted. Do not assume the same guarantee for a base-class subobject.

return OrderBatch{kOrders}; does not copy or move an entire OrderBatch. That does not prevent copies of input data: in the example, orders(source) copies the std::string from kOrders into the result's own member. The local kOrders is destroyed when the function exits; batch is destroyed when its block in main exits.

return local; is different: for a named non-volatile local object with automatic storage duration and the same class type (ignoring cv-qualification), other than a function or exception-handler parameter, named return value optimization (NRVO) is permitted but not required. With NRVO, the local and the result are the same object. In C++23, overload resolution treats such an operand as an xvalue: it can select a move or a copy, and the return is ill-formed if no usable constructor is selected, even if the compiler could apply NRVO.

Examples

#include <iostream>
#include <string>

struct OrderBatch {
    std::string orders;

    explicit OrderBatch(const std::string& source) : orders(source) {}
    OrderBatch(const OrderBatch&) = delete;
    OrderBatch(OrderBatch&&) = delete;
};

OrderBatch make_batch() {
    const std::string kOrders{"order-42"};
    return OrderBatch{kOrders};
}

int main() {
    OrderBatch batch = make_batch();
    std::cout << batch.orders << '\n';
}

Result

order-42

The program is expected to print `order-42`. The `OrderBatch` copy and move constructors are deleted; the `std::string` copy constructor is not. Replace the prvalue return with `OrderBatch local{kOrders}; return local;` to see the expected compile error.

Related lessons

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

Sources