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

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

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

例如:

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

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

int main()
{
    X one 
= f();  // VC2005中,release編譯時盡管優(yōu)化掉了復制構(gòu)造,但仍需要復制構(gò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)

例如: 

int main()
{
    X a,b;
    X c;
    cout 
<< "----------"<<endl;
    c
=a+b; // 臨時對象在對c進行完賦值后析構(gòu),即臨時對象在下面語句執(zhí)行前析構(gòu)
    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. ]

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

例如

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


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

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

posted on 2009-05-24 15:53 zhaoyg 閱讀(829) 評論(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>
            国模套图日韩精品一区二区| 欧美日韩精品一本二本三本| 国产农村妇女精品| 亚洲欧美日本另类| 亚洲女ⅴideoshd黑人| 国产日韩成人精品| 麻豆精品传媒视频| 欧美另类videos死尸| 一区二区免费看| 亚洲欧美视频在线观看视频| 国产一区二区三区久久久久久久久| 久久全国免费视频| 欧美激情国产日韩| 欧美专区福利在线| 欧美 日韩 国产精品免费观看| 一区二区三区国产在线观看| 午夜影院日韩| 亚洲精品在线电影| 午夜免费在线观看精品视频| 亚洲人成网站在线播| 亚洲视频精品| 亚洲激情在线激情| 亚洲综合第一| 一区二区欧美日韩| 久久精品在线视频| 亚洲宅男天堂在线观看无病毒| 久久久久久一区二区| 在线亚洲国产精品网站| 久久免费视频网站| 性欧美videos另类喷潮| 欧美激情按摩| 老**午夜毛片一区二区三区| 欧美性jizz18性欧美| 欧美大胆a视频| 国产一区二区精品丝袜| 99热免费精品| 日韩视频一区二区三区在线播放免费观看| 性欧美xxxx大乳国产app| 亚洲午夜精品在线| 男人的天堂成人在线| 久久亚洲私人国产精品va| 国产精品国产三级国产aⅴ9色| 欧美国产激情| 极品av少妇一区二区| 亚洲欧美精品中文字幕在线| 亚洲中午字幕| 欧美视频中文字幕| 亚洲美女福利视频网站| 亚洲人成7777| 牛人盗摄一区二区三区视频| 免费成年人欧美视频| 国内精品视频在线观看| 亚洲欧美在线免费观看| 欧美一级在线播放| 国产精品入口66mio| 国产精品99久久久久久人| 亚洲婷婷综合久久一本伊一区| 欧美精品久久一区二区| 最新国产の精品合集bt伙计| 亚洲激情在线激情| 欧美激情第8页| 亚洲免费观看高清完整版在线观看熊 | 欧美成人tv| 欧美激情一区二区在线 | 欧美日韩国产在线播放网站| 亚洲国产日韩一区| 中日韩高清电影网| 国产精品青草综合久久久久99| 亚洲深夜影院| 久久久久99精品国产片| 亚洲第一精品影视| 欧美精品一二三| 亚洲图中文字幕| 久久黄色小说| 最新国产成人在线观看 | 午夜精品成人在线| 久久免费视频在线观看| 亚洲国产精品第一区二区| 欧美不卡在线视频| 国产精品99久久久久久白浆小说| 欧美亚洲自偷自偷| 亚洲福利视频在线| 欧美日韩亚洲另类| 小辣椒精品导航| 欧美国内亚洲| 亚洲欧美日韩一区二区三区在线观看 | 欧美激情视频一区二区三区免费| 最新日韩av| 欧美在线观看视频一区二区三区| 激情视频亚洲| 欧美日韩亚洲一区| 欧美在线中文字幕| 亚洲欧洲一区二区在线观看| 亚洲欧美一区二区视频| 尤物精品在线| 国产精品va在线播放| 久久精品毛片| 在线视频中文亚洲| 欧美成人一区在线| 欧美一区二区免费观在线| 亚洲国产精品va| 国产精品久久久久久久久婷婷 | 久久一区欧美| 亚洲视频在线二区| 亚洲激情一区二区三区| 久久久久久**毛片大全| 夜夜爽夜夜爽精品视频| 激情成人综合网| 国产精品女主播| 欧美黄色一级视频| 久久一区二区三区国产精品| 午夜精品久久| 这里只有视频精品| 亚洲欧洲久久| 欧美高清视频一区二区三区在线观看 | a91a精品视频在线观看| 樱桃成人精品视频在线播放| 国产精品一区二区久久国产| 欧美精品观看| 欧美顶级艳妇交换群宴| 久久久综合香蕉尹人综合网| 亚洲欧美在线免费| 亚洲自拍偷拍一区| 中文精品视频| 日韩一区二区精品在线观看| 欧美国产高潮xxxx1819| 麻豆精品精品国产自在97香蕉| 欧美中文在线字幕| 午夜综合激情| 欧美一级久久久久久久大片| 亚洲免费影视| 亚洲欧美日韩中文播放| 亚洲一区国产视频| 午夜精品久久99蜜桃的功能介绍| 亚洲视频电影图片偷拍一区| 中文久久精品| 亚洲一级黄色| 午夜精品在线视频| 午夜久久福利| 久久精品国产第一区二区三区| 亚久久调教视频| 久久精品国产成人| 久久综合伊人77777麻豆| 麻豆精品在线视频| 欧美大胆成人| 亚洲日本理论电影| 日韩午夜三级在线| 亚洲小说欧美另类社区| 亚洲欧美日韩在线综合| 欧美一区中文字幕| 久久夜色精品国产噜噜av| 免费成人黄色片| 欧美伦理91| 国产精品毛片a∨一区二区三区|国| 国产精品久久久久久亚洲调教| 国产欧美日韩另类一区| 伊人色综合久久天天五月婷| 亚洲黄色免费| 亚洲一区二区日本| 久久久久久9999| 亚洲国产成人91精品| 一本色道久久综合亚洲91| 亚洲综合国产激情另类一区| 久久爱91午夜羞羞| 欧美激情日韩| 国产欧美精品| 最新国产成人av网站网址麻豆| 亚洲天堂久久| 欧美成人精品在线播放| 夜夜嗨av色一区二区不卡| 性视频1819p久久| 欧美激情视频在线播放| 国产日韩精品久久久| 亚洲精品一区二区三区樱花| 亚洲欧美另类综合偷拍| 久热精品视频在线观看一区| 日韩视频三区| 久久午夜视频| 国产精品综合视频| 亚洲精品一区二区三区婷婷月| 欧美专区日韩专区| 亚洲激情成人| 久久九九精品99国产精品| 欧美天堂亚洲电影院在线观看| 黑人巨大精品欧美黑白配亚洲| 亚洲一级免费视频| 亚洲高清免费| 久久黄色网页| 国产精品私拍pans大尺度在线| 91久久精品国产91性色| 久久精品一区蜜桃臀影院| 亚洲免费电影在线| 欧美电影电视剧在线观看| 国产亚洲精品福利| 亚洲欧美日韩在线一区| 亚洲精品一级| 欧美激情影院| 亚洲精品免费观看| 你懂的视频欧美| 久久久久久九九九九|