[class.copy.elision] (original) (raw)

When certain criteria are met, an implementation is allowed to omit the creation of a class object from a source object of the same type (ignoring cv-qualification), even if the selected constructor and/or the destructor for the object haveside effects.

In such cases, the implementation treats the source and target of the omitted initialization as simply two different ways of referring to the same object.

If the first parameter of the selected constructor is an rvalue reference to the object's type, the destruction of that object occurs when the target would have been destroyed; otherwise, the destruction occurs at the later of the times when the two objects would have been destroyed without the optimization.

[Note 1:

Because only one object is destroyed instead of two, and the creation of one object is omitted, there is still one object destroyed for each one constructed.

— _end note_]

This elision of object creation, calledcopy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

Copy elision is not permitted where an expression is evaluated in a context requiring a constant expression ([expr.const]) and in constant initialization ([basic.start.static]).

[Note 3:

It is possible that copy elision is performed if the same expression is evaluated in another context.

— _end note_]