摘自 ISO C++ 2003 P191
12.2 Temporary Object
1 Temporaries of class type are created in various contexts: binding an rvalue to a reference (
總結(jié):
臨時對象產(chǎn)生于引用綁定右值、函數(shù)返回右值、轉(zhuǎn)換、拋出異常以及一些初始化試。注,即使有些臨時對象可以被優(yōu)化掉,但語義的要求必須滿足
例如:






















2 [Example:
class X {
// ...
public:
// ...
X(int);
X(const X&);
˜X();
};
X f(X);
void g()
{
X a(1);
X b = f(X(2));
a = f(a);
}
Here, an implementation might use a temporary in which to construct X(2) before passing it to f() using X’s copy-constructor; alternatively, X(2) might be constructed in the space used to hold the argument. Also, a temporary might be used to hold the result of f(X(2)) before copying it to b using X’s copyconstructor; alternatively, f()’s result might be constructed in b. On the other hand, the expression a=f(a) requires a temporary for either the argument a or the result of f(a) to avoid undesired aliasing of a. ]
總結(jié):
對于f(X(2))這樣形式的參數(shù)傳入和值的返回,并不一定必須產(chǎn)生臨時對象,然后由復制構(gòu)造來構(gòu)造,也可以直接在目標內(nèi)存中構(gòu)造(形參或接受返回值的對象的所在地址)。這就是說,可以不經(jīng)復制構(gòu)造而直接構(gòu)造對象,但如之前所說復制構(gòu)造必須存在。
3 When an implementation introduces a temporary object of a class that has a non-trivial constructor (12.1), it shall ensure that a constructor is called for the temporary object. Similarly, the destructor shall be called for a temporary with a non-trivial destructor (12.4). Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception.
總結(jié):
當產(chǎn)生臨時對象的完整表達式計算完時,臨時對象就會被析構(gòu)
4 There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. The first context is when an expression appears as an initializer for a declarator defining an object.In that context,the temporary that holds the result of the expression shall persist until the object’s initialization is complete. The object is initialized from a copy of the temporary; during this copying, an implementation can call the copy constructor many times; the temporary is destroyed after it has been copied, before or when the initialization completes. If many temporaries are created by the evaluation of the initializer, the temporaries are destroyed in reverse order of the completion of their construction.
總結(jié):
有兩個情況下,不在完整表達式完成時析構(gòu)。第一種情況是,當一個表達式出現(xiàn)在初始化式中時,當執(zhí)行完初始化之后才會析構(gòu)
例如:











5 The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer (
class C {
// ...
public:
C();
C(int);
friend C operator+(const C&, const C&);
˜C();
};
C obj1;
const C& cr = C(16)+C(23);
C obj2;
the expression C(16) + C(23) creates three temporaries. A first temporary T1 to hold the result of the expression C(16), a second temporary T2 to hold the result of the expression C(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, it is guaranteed that T2 is 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, it is guaranteed that obj2 is destroyed before T3, and that T3 is destroyed before obj1. ]
總結(jié):
第二種情況是,當一個引用綁定臨時對象時,只有當引用或臨時對象超出作用域時析構(gòu)(因為當臨時對象被析構(gòu)后,指向該臨時對象的引用也就自然沒用了)
例如











另,發(fā)現(xiàn)對于指針,則完全沒有什么因臨時對象被“引用”而延長生命期這一說法
























