C++17 Copy elision object lifetime

138 views Asked by At

From cppreference:

URVO is mandatory and no longer considered a form of copy elision.

When copy elision occurs, the implementation treats the source and target of the omitted copy/move(since C++11) operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization (except that, if the parameter of the selected constructor is an rvalue reference to object type, the destruction occurs when the target would have been destroyed)(since C++11).

Could anyone provide an example of copy elision where the selected constructor is not the move constructor (i.e. not the exception case mentioned in parenthesis) and the target gets destroyed earlier than the source? Why should the lifetime be different (warranting an exception) when the selected constructor is the move constructor - i.e. any problem with the lifetime being the later of the times when the two objects would have been destroyed?

0

There are 0 answers