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

隨筆-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 閱讀(840) 評論(1)  編輯 收藏 引用 所屬分類: C/C++學習筆記

評論:
# re: {偶爾學習C++標準} 之 [初識臨時對象生命期] 2010-08-09 20:57 | 希望2012是真的,真心期待!!!
很不錯?。。。。。。?nbsp; 回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲欧美日韩在线观看a三区| 亚洲欧美日韩一区在线观看| 1024国产精品| 在线看片第一页欧美| 狠狠色狠色综合曰曰| 狠狠做深爱婷婷久久综合一区| 国产主播一区二区三区四区| 国产一区二区精品| 黄色成人在线网址| 亚洲国产高清一区| 亚洲高清毛片| 日韩亚洲一区在线播放| 中文国产成人精品久久一| 亚洲专区一区| 欧美一区二区精美| 久久久精品日韩| 美国十次了思思久久精品导航| 欧美成人蜜桃| 亚洲国产日韩精品| 99re视频这里只有精品| 亚洲视频导航| 欧美中文字幕在线| 欧美xart系列在线观看| 欧美日韩在线观看视频| 国产精品夜夜夜一区二区三区尤| 国产麻豆精品视频| 在线观看日韩av先锋影音电影院| 亚洲精品一区二区三区四区高清| 一区二区不卡在线视频 午夜欧美不卡在| 亚洲一区二区三区涩| 欧美在线网址| 亚洲国产电影| 亚洲综合精品| 老司机凹凸av亚洲导航| 欧美午夜国产| 国产中文一区二区| 一本色道久久88精品综合| 欧美亚洲网站| 亚洲国产高清一区二区三区| 中文成人激情娱乐网| 久久久夜色精品亚洲| 欧美日韩国产免费| 国产亚洲一二三区| 日韩亚洲欧美一区二区三区| 欧美一区激情视频在线观看| 欧美国产欧美亚洲国产日韩mv天天看完整 | 一区二区三区四区五区精品视频| 新片速递亚洲合集欧美合集 | 狠狠做深爱婷婷久久综合一区| 亚洲卡通欧美制服中文| 欧美在线视屏| 亚洲美女电影在线| 久久久久国产精品午夜一区| 国产精品v欧美精品v日韩| 亚洲成色www8888| 亚洲欧美成人在线| 亚洲国产精品毛片| 欧美一区二区三区电影在线观看| 欧美激情一区三区| 韩日在线一区| 性欧美大战久久久久久久免费观看 | 一本久久综合| 蜜桃久久av一区| 午夜精品视频在线| 欧美日韩亚洲一区三区| 亚洲国产高清视频| 久久久久一区二区| 一区二区三区精品视频在线观看| 老司机一区二区| 国产一区在线免费观看| 亚洲男人第一av网站| 亚洲国产一成人久久精品| 久久精品国产精品| 国产九九精品视频| 亚洲一级黄色片| 亚洲日本乱码在线观看| 久久综合狠狠综合久久激情| 国产亚洲欧美日韩美女| 亚洲欧美韩国| 99精品视频免费在线观看| 欧美高清在线观看| 亚洲国产精品欧美一二99| 久久久久久久久岛国免费| 亚洲在线观看| 国产精品区免费视频| 亚洲天堂男人| 日韩亚洲欧美成人一区| 欧美激情一二区| 亚洲片在线资源| 欧美高清视频一二三区| 老司机aⅴ在线精品导航| 在线播放亚洲一区| 久热re这里精品视频在线6| 久久成年人视频| 国产一区欧美日韩| 久久免费视频在线| 欧美资源在线观看| 国产一区二区三区直播精品电影| 久久岛国电影| 欧美伊人久久久久久午夜久久久久| 国产欧美精品在线| 久久精品视频va| 久久国产色av| 亚洲成人在线网| 亚洲丰满在线| 欧美日韩成人在线播放| 一区二区三区四区五区在线 | 久久综合网hezyo| 久久久久久伊人| 亚洲国产精品久久久久秋霞不卡| 欧美大片在线观看一区二区| 蜜臀a∨国产成人精品| 亚洲精品在线三区| 亚洲理论在线观看| 国产精品久久久久久久久久久久 | 亚洲日本欧美在线| 91久久夜色精品国产九色| 欧美日韩人人澡狠狠躁视频| 亚洲一区二区久久| 午夜精品成人在线视频| 永久免费精品影视网站| 亚洲高清视频一区| 欧美体内she精视频在线观看| 亚洲欧美日韩中文在线制服| 欧美一级淫片aaaaaaa视频| 一色屋精品视频免费看| 91久久国产综合久久蜜月精品 | 一本色道久久综合亚洲精品高清 | 欧美成人资源网| 欧美日韩免费区域视频在线观看| 午夜免费在线观看精品视频| 香蕉成人久久| 亚洲国产91精品在线观看| 亚洲伦理在线免费看| 国产欧美一区二区三区在线看蜜臀| 久久久99免费视频| 欧美丰满少妇xxxbbb| 亚洲欧美成人在线| 久久久www成人免费毛片麻豆| 亚洲精品久久久久久久久| 亚洲亚洲精品三区日韩精品在线视频| 国产资源精品在线观看| 亚洲国产精品一区二区久| 国产精品一区=区| 亚洲国产精品一区| 国产模特精品视频久久久久| 欧美激情一区二区三区不卡| 国产精品h在线观看| 狂野欧美激情性xxxx| 欧美日韩综合一区| 女人天堂亚洲aⅴ在线观看| 欧美视频一区二| 欧美aa国产视频| 国产精品欧美在线| 亚洲激情视频在线观看| 国产一区二区三区四区五区美女| 亚洲黄色三级| 韩国成人福利片在线播放| 亚洲免费观看视频| 在线观看成人一级片| 亚洲一区二区成人在线观看| 亚洲精品国产系列| 欧美一区二区三区免费视| 正在播放亚洲| 美女视频黄a大片欧美| 久久精品首页| 国产精品久久久久久久午夜| 亚洲电影在线| 一区二区在线看| 亚洲一区免费在线观看| 夜夜爽99久久国产综合精品女不卡 | 亚洲国产精品久久人人爱蜜臀 | 在线播放日韩专区| 午夜精品999| 亚洲综合不卡| 欧美日韩精品免费看| 欧美高清在线视频| 狠狠色丁香婷婷综合| 亚洲欧美国产毛片在线| 亚洲午夜精品一区二区| 欧美国产激情二区三区| 免费黄网站欧美| 激情欧美一区| 欧美一区三区二区在线观看| 午夜国产精品视频| 欧美日韩你懂的| 亚洲伦理网站| 99成人精品| 欧美大片一区二区三区| 欧美韩日一区二区三区| 一区二区视频免费完整版观看| 亚洲欧美日韩精品久久| 亚洲自拍三区| 国产精品yjizz| 一本色道久久综合亚洲精品不卡| aa日韩免费精品视频一| 欧美大片免费久久精品三p| 欧美黄色大片网站| 亚洲人精品午夜| 欧美成熟视频|