:
struct S {
S();
S(int);
friend S operator+(const S&, const S&);
~S();
};
S obj1;
const S& cr = S(16)+S(23);
S obj2;
The expression
S(16) + S(23)
creates three temporaries:
a first temporary
T1
to hold the result of the expression
S(16),
a second temporary
T2
to hold the result of the expression
S(23),
and a third temporary
T3
to hold the result of the addition of these two expressions
. The temporary
T3
is then bound to the reference
cr. It is unspecified whether
T1
or
T2
is created first
. On an implementation where
T1
is created before
T2,
T2
shall be destroyed before
T1. The temporaries
T1
and
T2
are bound to the reference parameters of
operator+;
these temporaries are destroyed at the end of the full-expression
containing the call to
operator+. The temporary
T3
bound to the reference
cr
is destroyed at the end of
cr's
lifetime, that is, at the end of the program
. In addition, the order in which
T3
is destroyed takes into account the destruction order of other objects with
static storage duration
. That is, because
obj1
is constructed before
T3,
and
T3
is constructed before
obj2,
obj2
shall be destroyed before
T3,
and
T3
shall be destroyed before
obj1. —
end example