青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆-59  評論-36  文章-0  trackbacks-0
看了如下的文字,對臨時對象有了一些初步了解。

摘自 ISO C++ 2003      P191

12.2 Temporary Object

 

1 Temporaries of class type are created in various contexts: binding an rvalue to a reference (8.5.3), returning an rvalue (6.6.3), a conversion that creates an rvalue (4.1, 5.2.9, 5.2.11, 5.4), throwing an exception (15.1), entering a handler (15.3), and in some initializations (8.5). [Note: the lifetime of exception objects is described in 15.1.] Even when the creation of the temporary object is avoided (12.8), all the semantic restrictions must be respected as if the temporary object was created. [ Example: even if the copy constructor is not called, all the semantic restrictions, such as accessibility (clause 11), shall be satisfied.]

總結:
臨時對象產生于引用綁定右值、函數返回右值、轉換、拋出異常以及一些初始化試。
注,即使有些臨時對象可以被優化掉,但語義的要求必須滿足

例如:

class X 
{
public:
    X (
const X &a); // No copy constructor definition
}
;

X f()
{
    X one;
    
return one;
}

int main()
{
    X one 
= f();  // VC2005中,release編譯時盡管優化掉了復制構造,但仍需要復制構造的存在以滿足語義要求
}

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. ]

總結:
對于f(X(2))這樣形式的參數傳入和值的返回,并不一定必須產生臨時對象,然后由復制構造來構造,也可以直接在目標內存中構造(形參或接受返回值的對象的所在地址)。這就是說,可以不經復制構造而直接構造對象,但如之前所說復制構造必須存在。

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.

總結:
當產生臨時對象的完整表達式計算完時,臨時對象就會被析構

 

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.

總結:
有兩個情況下,不在完整表達式完成時析構。第一種情況是,當一個表達式出現在初始化式中時,當執行完初始化之后才會析構

例如: 

int main()
{
    X a,b;
    X c;
    cout 
<< "----------"<<endl;
    c
=a+b; // 臨時對象在對c進行完賦值后析構,即臨時對象在下面語句執行前析構
    cout << "----------"<<endl;
}



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 (12.6.2) persists until the constructor exits.A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call.A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits.In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound,are destroyed at the end of the full-expression in which they are created and in the reverse order of the completion of their construction.If the lifetime of two or more temporaries to which references are bound ends at the same point, these temporaries are destroyed at that point in the reverse order of the completion of their construction. In addition, the destruction of temporaries bound to references shall take into account the ordering of destruction of objects with static or automatic storage duration (3.7.1, 3.7.2); that is, if obj1 is an object with static or automatic storage duration created before the temporary is created, the temporary shall be destroyed before obj1 is destroyed; if obj2 is an object with static or automatic storage duration created after the temporary is created, the temporary shall be destroyed after obj2 is destroyed.[Example:

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. ]

總結:
第二種情況是,當一個引用綁定臨時對象時,只有當引用或臨時對象超出作用域時析構(因為當臨時對象被析構后,指向該臨時對象的引用也就自然沒用了)

例如

int main()
{
    X a,b;
    cout 
<< "----------"<<endl;
    X 
&c=a+b;  // 臨時對象因被引用,故而延期析構
    cout << "----------"<<endl;
     
return 0// 此時臨時對象析構
}


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

class T
{
int i;
public:
T()
{ i = 99; }
void show(){ cout << i<<endl; }
}
;
int main()
{
    T 
*p;
    cout 
<< "========="<<endl;
    p
=&f();  // f函數一旦調用完,臨時對象就被析構
(*p).show(); // 輸出垃圾值
    cout << "========="<<endl;
    cout 
<< "return"<<endl;
}

posted on 2009-05-24 15:53 zhaoyg 閱讀(835) 評論(1)  編輯 收藏 引用 所屬分類: C/C++學習筆記

評論:
# re: {偶爾學習C++標準} 之 [初識臨時對象生命期] 2010-08-09 20:57 | 希望2012是真的,真心期待!!!
很不錯!!!!!!!  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            一本色道久久88精品综合| 亚洲精品在线看| 欧美一级片一区| 亚洲欧美色婷婷| 国产日韩精品一区观看| 久久精品人人| 久久亚洲一区二区三区四区| 亚洲电影av在线| 91久久黄色| 国产精品高清免费在线观看| 亚洲欧美日本伦理| 欧美制服丝袜第一页| 一区二区三区无毛| 91久久久久久久久| 欧美日韩综合不卡| 久久九九99| 欧美激情按摩| 欧美一区二区三区日韩视频| 久久久亚洲欧洲日产国码αv | 一本色道久久综合亚洲精品不| 欧美日韩一区二区在线视频 | 欧美影院视频| 久久亚洲美女| 亚洲自拍三区| 久久久欧美一区二区| 一本大道久久a久久精品综合| 在线亚洲欧美专区二区| 激情欧美日韩一区| 99精品国产在热久久| 国产欧美视频一区二区| 亚洲国产老妈| 国产欧美91| 亚洲国产一成人久久精品| 国产精品久久久久aaaa| 欧美a级片网站| 欧美午夜大胆人体| 欧美国产日韩亚洲一区| 国产精品二区二区三区| 欧美激情成人在线| 国产欧美日本| 亚洲精品一区在线观看| 精品成人乱色一区二区| 亚洲手机在线| 日韩亚洲欧美在线观看| 久久精品一区蜜桃臀影院| 亚洲欧美日韩国产中文| 欧美超级免费视 在线| 久久久久久久久久久久久9999| 欧美精品综合| 欧美激情一区二区| 国内精品久久国产| 亚洲性图久久| 中文在线资源观看网站视频免费不卡| 久久精品国产欧美亚洲人人爽| 亚洲欧美一区二区视频| 欧美乱妇高清无乱码| 麻豆久久久9性大片| 国产亚洲欧美日韩一区二区| 亚洲视频免费观看| 一区二区三区波多野结衣在线观看| 久久综合五月天婷婷伊人| 久久婷婷麻豆| 国产一区二区视频在线观看| 亚洲欧美国产精品专区久久| 亚洲免费在线精品一区| 欧美日韩高清在线| 亚洲精品美女在线观看播放| 亚洲理论电影网| 欧美极品影院| 日韩香蕉视频| 亚洲一区二区视频在线观看| 欧美日韩一区二区在线| 99精品欧美一区二区蜜桃免费| 亚洲无限乱码一二三四麻| 欧美日韩伊人| 亚洲午夜在线视频| 午夜欧美不卡精品aaaaa| 国产精品午夜在线观看| 午夜久久tv| 久久一区二区三区av| 激情综合在线| 久久免费视频网站| 亚洲成人自拍视频| 亚洲美女在线一区| 欧美偷拍一区二区| 亚洲制服丝袜在线| 久久女同精品一区二区| 在线欧美小视频| 欧美激情第六页| 这里只有精品视频在线| 久久av最新网址| 亚洲国产成人porn| 欧美好骚综合网| 亚洲一二三区精品| 久色婷婷小香蕉久久| 亚洲人成在线影院| 国产精品久久综合| 久久精品一区| 99精品视频免费观看视频| 久久高清福利视频| 亚洲精品视频免费观看| 国产精品国产三级欧美二区| 久久精品论坛| 99亚洲伊人久久精品影院红桃| 欧美在线播放视频| 日韩系列在线| 国产日韩欧美在线一区| 欧美顶级少妇做爰| 亚洲欧美国产日韩天堂区| 欧美大尺度在线| 午夜精品久久久久久久99热浪潮 | 亚洲国产欧美一区二区三区久久| 欧美三级韩国三级日本三斤| 久久精品主播| 亚洲欧美不卡| 亚洲精品在线观看视频| 久久久久久夜| 亚洲一区在线播放| 亚洲三级影院| 今天的高清视频免费播放成人| 欧美日韩激情小视频| 可以免费看不卡的av网站| 亚洲性感美女99在线| 亚洲精品乱码久久久久久久久| 久久综合成人精品亚洲另类欧美| 亚洲欧美日韩一区二区| 亚洲美女一区| 亚洲激情午夜| 影音先锋国产精品| 国产亚洲精久久久久久| 国产精品久久久久久模特| 欧美电影电视剧在线观看| 久久久久久久一区| 欧美伊人影院| 欧美自拍偷拍午夜视频| 亚洲一区美女视频在线观看免费| 亚洲另类在线视频| 最近中文字幕日韩精品| 欧美激情国产日韩精品一区18| 久久综合网络一区二区| 欧美专区日韩视频| 篠田优中文在线播放第一区| 午夜视频在线观看一区二区三区| 在线一区二区三区四区五区| 一本久久综合亚洲鲁鲁| 夜夜嗨av一区二区三区中文字幕| 亚洲破处大片| 亚洲日本国产| 一区二区三区日韩欧美| 一区二区福利| 亚洲一区二区三区成人在线视频精品| 99国产一区二区三精品乱码| 亚洲精品在线观看免费| 亚洲视频香蕉人妖| 亚洲一区二区三区免费视频| 午夜精品久久久久久久久| 久久se精品一区精品二区| 久久久欧美精品sm网站| 美女被久久久| 亚洲国产精品小视频| 亚洲精品视频在线看| 一区二区三区久久| 亚洲免费在线观看| 欧美一区二区三区四区在线 | 亚洲福利专区| 99re成人精品视频| 亚洲欧美日韩一区在线观看| 久久成人精品无人区| 欧美a一区二区| 欧美日韩综合一区| 国产精品一区视频网站| 精久久久久久| 亚洲视频成人| 久久久91精品| 亚洲电影在线免费观看| 99国产精品| 欧美影院久久久| 欧美国产日韩精品| 国产精品美女久久久久久2018| 国内精品一区二区| 日韩亚洲欧美在线观看| 久久精品国产第一区二区三区最新章节 | 久久综合一区二区三区| 亚洲国产毛片完整版| 亚洲婷婷综合久久一本伊一区| 欧美一区观看| 欧美视频国产精品| 在线不卡欧美| 亚洲摸下面视频| 男同欧美伦乱| 亚洲一本大道在线| 免费欧美高清视频| 国产精品视频导航| 日韩视频永久免费| 久久免费黄色| 中国亚洲黄色| 欧美另类变人与禽xxxxx| 国内精品伊人久久久久av一坑| 亚洲丝袜av一区| 亚洲国产精品久久久久秋霞蜜臀|