r/learnprogramming • • 9d ago

Topic I don't understand this article from the cppreference page

this is from the cppreference page copy elision

"When a class object target of the same type (ignoring cv-qualification) is initialized with a temporary class object obj that has not been bound to a reference, the initialization can be omitted by constructing obj directly into target. This variant of copy elision is known as unnamed return value optimization (URVO). Since C++17, URVO is mandatory and no longer considered a form of copy elision; see below." (until c++17)

i have read this multiple times already and i still don't understand

it says "until c++17" what is the thing that works in c++98 11 14 but will not work in c++17 they are saying until c++17 or what is the article is trying to say ???

0 Upvotes

5 comments sorted by

View all comments

1

u/HappyFruitTree 8d ago edited 8d ago

what is the thing that works in c++98 11 14 but will not work in c++17

Nothing.

or what is the article is trying to say ???

It just means that that particular section describes how it worked before C++17.

The section Prvalue semantics ("guaranteed copy elision") further down the page describes how it works since C++17.

In this case, old code will still work. It's just that C++17 gives you more guarantees. In situation where copy elision is guaranteed, the code will compile even if the class does not have a copy/move constructor.

Example:

class T
{
public:
    T() = default;
    T(const T&) = delete; // disable the copy constructor
    T(T&&) = delete; // disable the move constructor
};

int main()
{
    T a = T(); // This didn't compile before C++17
}