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

asm, c, c++ are my all
-- Core In Computer
posts - 139,  comments - 123,  trackbacks - 0

/********************************************\
|????歡迎轉載, 但請保留作者姓名和原文鏈接, 祝您進步并共勉!???? |
\********************************************/


C++對象模型(6) -? Program Transformation Semantics

作者: Jerry Cat
時間: 2006/05/11
鏈接:?
http://www.shnenglu.com/jerysun0818/archive/2006/05/11/6912.html

2.3 Program Transformation Semantics
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
1). Explicit Initialization:

Given the definition
X x0;
the following three definitions each explicitly initialize its class object with x0:

void foo_bar() {
?? X x1( x0 );
?? X x2 = x0;
?? X x3 = x( x0 );
?? // ...
}
The required program transformation is two-fold:

Each definition is rewritten with the initialization stripped out.
An invocation of the class copy constructor is inserted.
For example, foo_bar() might look as follows after this straightforward, two-fold transformation:

// Possible program transformation Pseudo C++ Code
void foo_bar() {
?? X x1;
?? X x2;
?? X x3;

?? // compiler inserted invocations of copy constructor for X
?? x1.X::X( x0 );
?? x2.X::X( x0 );
?? x3.X::X( x0 );
?? // ...
}
where the call

x1.X::X( x0 );
represents a call of the copy constructor

X::X( const X& xx );

2). Argument Initialization盡量不用傳值法, 要穿指針或引用. 傳值法開銷大效率低,
??? 更要命的是涉及到深淺拷貝以及, 局部變量和臨時對象的銷毀問題.

3). Return Value Initialization(雙重變形, Bjarne Stroutstrup的trick):
(按: 返回值(不是引用或指針,返回的是value), 其實是讓一外部對象的引用做一個"悄然追加"
???? 的參數(編譯器偷著干的, 你是看不見的:), 然后是空返回, 你的返回值呢? 諾, 就是那
???? 以"外追"方式進入函數內部參與處理的引用呵^_^ )

Given the following definition of bar():
X bar()
{
?? X xx;
?? // process xx ...
?? return xx;
}
you may ask how might bar()'s return value be copy constructed from its local object xx?
Stroustrup's solution in cfront is a two-fold transformation:

Add an additional argument of type reference to the class object. This argument will hold the
copy constructed "return value."

Insert an invocation of the copy constructor prior to the return statement to initialize the
added argument with the value of the object being returned.

What about the actual return value, then? A final transformation rewrites the function to have
it not return a value. The transformation of bar(), following this algorithm, looks like this:

// function transformation to reflect application of copy constructor Pseudo C++ Code
void bar( X& __result )
{
?? X xx;

?? // compiler generated invocation of default constructor
?? xx.X::X();
?? // ... process xx

?? // compiler generated invocation of copy constructor
?? __result.X::X( xx );

?? return;
}
Given this transformation of bar(), the compiler is now required to transform each invocation
of bar() to reflect its new definition. For example,

X xx = bar();
is transformed into the following two statements:

// note: no default constructor applied
X xx;
bar( xx );
while an invocation such as

bar().memfunc();
might be transformed into

// compiler generated temporary
X __temp0;
( bar( __temp0 ), __temp0 ).memfunc();
Similarly, if the program were to declare a pointer to a function, such as

X ( *pf )();
pf = bar;
that declaration, too, would need to be transformed:

void ( *pf )( X& );
pf = bar;

4). Optimization at the Compiler Level:
In a function such as bar(), where all return statements return the same named value, it is
possible for the compiler itself to optimize the function by substituting the result argument
for the named return value. For example, given the original definition of bar():

X bar()
{
?? X xx;
?? // ... process xx
?? return xx;
}
__result is substituted for xx by the compiler:

void
bar( X &__result )
{
?? // default constructor invocation Pseudo C++ Code
?? __result.X::X();
?? // ... process in __result directly

?? return;
}
This compiler optimization, sometimes referred to as the Named Return Value (NRV) optimization.

Although the following three initializations are semantically equivalent:

X xx0( 1024 );
X xx1 = X( 1024 );
X xx2 = ( X ) 1024;
in the second and third instances, the syntax explicitly provides for a two-step initialization:
Initialize a temporary object with 1024.

Copy construct the explicit object with the temporary object.

That is, whereas xx0 is initialized by a single constructor invocation

// Pseudo C++ Code
xx0.X::X( 1024 );
a strict implementation of either xx1 or xx2 results in two constructor invocations, a temporary
object, and a call to the destructor of class X on that temporary object:

// Pseudo C++ Code
X __temp0;
__temp0.X::X( 1024 );
xx1.X::X( __temp0 );
__temp0.X::~X();

5). The Copy Constructor: To Have or To Have Not?
??? =============================================
Given the following straightforward 3D point class:

class Point3d {
public:
?? Point3d( float x, float y, float z );
?? // ...
private:
?? float _x, _y, _z;
};
should the class designer provide an explicit copy constructor?

The default copy constructor is considered trivial. There are no member or base class objects
with a copy constructor that need to be invoked. Nor is there a virtual base class or virtual
function associated with the class. So, by default, a memberwise initialization of one Point3d
class object with another results in a bitwise copy. This is efficient. But is it safe?

The answer is yes. The three coordinate members are stored by value. Bitwise copy results in
neither a memory leak nor address aliasing. Thus it is both safe and efficient.

So, how would you answer the question, should the class designer provide an explicit copy
constructor? The obvious answer, of course, is no. There is no reason to provide an instance
of the copy constructor, as the compiler automatically does the best job for you. The more subtle
answer is to ask whether you envision the class's requiring a good deal of memberwise
initialization, in particular, returning objects by value? If the answer is yes, then it makes
excellent sense to provide an explicit inline instance of the copy constructor that is, provided
your compiler provides the NRV optimization(虛擬語氣).

For example, the Point3d class supports the following set of functions:

Point3d operator+( const Point3d&, const Point3d& );
Point3d operator-( const Point3d&, const Point3d& );
Point3d operator*( const Point3d&, int );
etc.
all of which fit nicely into the NRV template
{
?? Point3d result;
?? // compute result
?? return result
}
The simplest method of implementing the copy constructor is as follows:

Point3d::Point3d( const Point3d &rhs )
{
?? _x = rhs._x;
?? _y = rhs._y;
?? _z = rhs._z;
};
This is okay, but use of the C library memcpy() function would be more efficient:

Point3d::Point3d( const Point3d &rhs )
{
?? memcpy( this, &rhs, sizeof( Point3d );
};
Use of both memcpy() and memset(), however, works only if the classes do not contain any
compiler-generated internal members. If the Point3d class declares one or more virtual functions
or contains a virtual base class, use of either of these functions will result in overwriting the
values the compiler set for these members. For example, given the following declaration:

class Shape {
public:
?? // oops: this will overwrite internal vptr!
?? Shape() { memset( this, 0, sizeof( Shape ));
?? virtual ~Shape();
?? // ...
};
the compiler augmentation for the constructor generally looks like this:

// Expansion of constructor Pseudo C++ Code
Shape::Shape()
{
?? // vptr must be set before user code executes
?? __vptr__Shape = __vtbl__Shape;

?? // oops: memset zeros out value of vptr
?? memset( this, 0, sizeof( Shape ));
};
As you can see, correct use of the memset() and memcpy() functions requires some knowledge of the
C++ Object Model semantics! 嘿, 把C庫扯進來了, 強! C庫中許多強調性能,效率的函數是用匯編寫的

Summary: 編譯器盡可能地"優化掉"拷貝構造函數, 代之以NRV...
---------------------------------------------------------
Application of the copy constructor requires the compiler to more or less transform portions of
your program. In particular, consider a function that returns a class object by value for a class
in which a copy constructor is either explicitly defined or synthesized. The result is profound
program transformations both in the definition and use of the function. Also, the compiler
optimizes away the copy constructor invocation where possible, replacing the NRV with an additional
first argument within which the value is stored directly. Programmers who understand these
transformations and the likely conditions for copy constructor optimization can better control the
runtime performance of their programs.

posted on 2006-05-11 03:33 Jerry Cat 閱讀(591) 評論(0)  編輯 收藏 引用

<2006年10月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用鏈接

留言簿(7)

隨筆檔案

最新隨筆

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲另类一区二区| 一区二区高清| 蜜臀av性久久久久蜜臀aⅴ| 久久午夜精品一区二区| 亚洲人成在线观看| 午夜精品久久久久久久久久久久| 黄色日韩在线| 一区二区三区波多野结衣在线观看| 国产精品羞羞答答| 亚洲风情在线资源站| 欧美日韩美女在线观看| 久久夜色精品国产欧美乱| 欧美日韩在线一区二区三区| 狂野欧美激情性xxxx欧美| 欧美日精品一区视频| 米奇777在线欧美播放| 国产麻豆午夜三级精品| 日韩性生活视频| 亚洲免费福利视频| 美女网站久久| 亚洲高清视频在线观看| 亚洲成人资源网| 久久亚洲一区二区| 免费成人av在线看| 韩日精品视频| 久久九九久精品国产免费直播 | 亚洲综合好骚| 国产精品视频观看| 性欧美大战久久久久久久免费观看| 亚洲一区二区三区中文字幕在线| 欧美日韩精品二区| 亚洲中字黄色| 男人天堂欧美日韩| 99综合在线| 国产午夜精品一区二区三区欧美| 久久精品国产清自在天天线| 欧美成人午夜激情| 亚洲视频观看| 红桃视频一区| 欧美日韩伦理在线| 午夜国产不卡在线观看视频| 久久久久久黄| 一本色道久久综合狠狠躁的推荐| 国产精品男女猛烈高潮激情| 久久久久综合网| 99爱精品视频| 男女激情久久| 欧美亚洲尤物久久| 亚洲性av在线| 一本色道久久综合亚洲二区三区| 国产欧美日韩精品丝袜高跟鞋| 久久久久欧美精品| 欧美一区二区三区四区视频| 99国产精品国产精品久久 | 亚洲国产精品黑人久久久| 国产精品久久久久久久7电影| 欧美中文在线观看| 欧美亚洲综合另类| 亚洲在线成人| 亚洲制服av| 亚洲欧美日韩国产| 香蕉久久夜色精品国产| 亚洲小说欧美另类婷婷| 一区二区三区你懂的| 亚洲欧洲三级电影| 嫩模写真一区二区三区三州| 亚洲淫性视频| 中国成人黄色视屏| 一区二区三区日韩在线观看| 亚洲免费成人av| 亚洲精品黄网在线观看| 亚洲黄色影片| 99成人精品| 亚洲永久免费视频| 欧美一区二区三区在线看| 午夜一区二区三视频在线观看| 亚洲天堂成人在线视频| 日韩天天综合| 亚洲欧美一区二区激情| 欧美一区二区免费观在线| 久久精品99无色码中文字幕| 久久亚洲精品一区二区| 免费日韩视频| 亚洲欧洲免费视频| 欧美一区二区三区视频| 欧美xxxx在线观看| 国产精品成人v| 在线观看日韩av| 亚洲在线黄色| 欧美激情1区2区3区| 亚洲一区二区三区免费观看| 久久精品一区中文字幕| 欧美成人伊人久久综合网| 欧美日韩成人一区二区| 激情欧美日韩一区| 国产精品视频免费一区| 久久精品在这里| 欧美另类极品videosbest最新版本| 亚洲国产欧美日韩| 亚洲福利一区| 亚洲一区久久| 国产精品久久一级| 亚洲韩国精品一区| 亚洲你懂的在线视频| 亚洲欧洲一区二区在线播放| 亚洲视频一区在线观看| 久久久久久亚洲精品杨幂换脸 | 国产欧美午夜| 国产日产欧美a一级在线| 日韩视频免费观看高清在线视频 | 亚洲欧美视频在线观看| 欧美小视频在线观看| 亚洲视频一区二区| 亚洲丁香婷深爱综合| 香蕉av777xxx色综合一区| 国产精品网站在线播放| 国产日韩欧美在线播放不卡| 久久久久久亚洲精品中文字幕 | 久久精品国产99国产精品| 韩日在线一区| 亚洲大胆美女视频| 欧美三级资源在线| 免费av成人在线| 欧美日韩国产一级片| 亚洲精品男同| 亚洲你懂的在线视频| 亚洲电影网站| 免费在线日韩av| 9色porny自拍视频一区二区| 国产日韩欧美中文在线播放| 久久精品国产欧美激情 | 欧美亚洲免费电影| 欧美日韩岛国| 亚洲精品欧美激情| 亚洲精品一二三| 亚洲一区二区三区中文字幕在线| 欧美精品成人| 久久亚洲一区二区| 国产欧美综合在线| 亚洲二区免费| 亚洲高清资源| 久久精品导航| 久久影院午夜论| 国产一区99| 一本久久知道综合久久| 99热在这里有精品免费| 亚洲欧美日韩精品综合在线观看| 99国产精品一区| 欧美日韩不卡| 亚洲天堂成人| 亚洲欧美在线观看| 猛男gaygay欧美视频| 91久久国产精品91久久性色| 亚洲国产成人在线视频| 欧美99在线视频观看| 亚洲欧洲一区| 亚洲一区二区在线看| 国产亚洲成av人片在线观看桃| 宅男噜噜噜66一区二区| 亚洲欧美日韩系列| 欧美激情综合五月色丁香| 亚洲乱码国产乱码精品精| 亚洲一区二区三区精品动漫| 久久米奇亚洲| 亚洲国产天堂久久综合网| 亚洲精品中文在线| 国产精品自拍小视频| 农夫在线精品视频免费观看| 99精品视频一区| 另类国产ts人妖高潮视频| 在线欧美视频| 国产欧美日韩免费看aⅴ视频| 久久大香伊蕉在人线观看热2| 亚洲国产精品ⅴa在线观看 | 欧美在线短视频| 亚洲精选视频免费看| 国产精品成人一区二区三区吃奶 | 国产精品久久久久aaaa| 久久九九热re6这里有精品 | 在线免费高清一区二区三区| 欧美日韩四区| 欧美风情在线观看| 亚洲精品国偷自产在线99热| 欧美激情按摩在线| 欧美 日韩 国产在线| 亚洲影院免费| 性欧美xxxx视频在线观看| 亚洲国产精品激情在线观看| 国产精品成人一区二区网站软件 | 久久亚洲午夜电影| 99精品视频一区| 99亚洲精品| 国产精品亚洲视频| 国产一区二区三区无遮挡| 国内揄拍国内精品久久| 在线观看国产欧美| 亚洲精品日产精品乱码不卡| 国产精品99久久久久久人| 西西人体一区二区| 欧美成人嫩草网站|