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 have
side 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, called
copy elision,
is permitted in the
following circumstances (which may be combined to
eliminate multiple copies):
- in a return statement ([stmt.return]) in
a function with a class return type,
when the expression is the name of a non-volatile
object o with automatic storage duration (other than a function parameter or a variable
introduced by the exception-declaration of a
handler ([except.handle])),
the copy-initialization of the result object can be
omitted by constructing o directly
into the function call's result object;
- in a throw-expression ([expr.throw]), when the operand
is the name of a non-volatile object o with automatic storage duration
(other than a function parameter or
a variable introduced by
the exception-declaration of a handler)
that belongs to a scope that does not contain
the innermost enclosing compound-statement
associated with a try-block (if there is one),
the copy-initialization of the exception object can be omitted by
constructing o directly into the exception object;
- in a coroutine, a copy of a coroutine parameter
can be omitted and references to that copy replaced with references to the
corresponding parameter if the meaning of the program will be unchanged except for
the execution of a constructor and destructor for the parameter copy object;
- when the exception-declaration of a
handler ([except.handle]) declares an object o,
the copy-initialization of o can be omitted by treating
the exception-declaration as an alias for the exception
object if the meaning of the program will be unchanged except for the execution
of constructors and destructors for the object declared by the
exception-declaration.
[
Note 2:
There cannot be a move from the exception object because it is
always an lvalue
. —
end note]
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]