[class.temporary] (original) (raw)

:

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 expressionS(16) + S(23)creates three temporaries: a first temporaryT1to hold the result of the expressionS(16), a second temporaryT2to hold the result of the expressionS(23), and a third temporaryT3to hold the result of the addition of these two expressions.

The temporaryT3is then bound to the referencecr.

It is unspecified whetherT1orT2is created first.

On an implementation whereT1is created beforeT2,T2shall be destroyed beforeT1.

The temporariesT1andT2are bound to the reference parameters ofoperator+; these temporaries are destroyed at the end of the full-expression containing the call tooperator+.

The temporaryT3bound to the referencecris destroyed at the end ofcr's lifetime, that is, at the end of the program.

In addition, the order in whichT3is destroyed takes into account the destruction order of other objects with static storage duration.

That is, becauseobj1is constructed beforeT3, andT3is constructed beforeobj2,obj2shall be destroyed beforeT3, andT3shall be destroyed beforeobj1.

end example