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

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)  編輯 收藏 引用

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用鏈接

留言簿(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>
            久久久久久欧美| 欧美综合国产| 亚洲人屁股眼子交8| 亚洲激情偷拍| 亚洲另类在线一区| 亚洲一本视频| 久久xxxx精品视频| 欧美福利视频在线| 亚洲无限av看| 欧美成ee人免费视频| 欧美日韩亚洲不卡| 韩日欧美一区二区| 亚洲视频久久| 欧美成人免费小视频| 免费欧美日韩国产三级电影| 国产欧美一区二区精品性色| 亚洲精品免费在线| 米奇777超碰欧美日韩亚洲| 一区二区三区日韩欧美| 久久久天天操| 国产亚洲欧美色| 亚洲制服av| av成人国产| 欧美久久久久久久久久| 黄色亚洲精品| 亚洲精品自在久久| 免费不卡视频| 在线观看精品视频| 欧美一区二区成人| 一区二区三区国产在线观看| 国产亚洲精品一区二区| 亚洲人成绝费网站色www| 国产精品免费在线| 亚洲亚洲精品三区日韩精品在线视频| 欧美影院视频| 亚洲在线视频免费观看| 久久综合影视| 最新日韩欧美| 亚洲国产一区二区视频| 老司机午夜精品| 欧美一区二区高清在线观看| 欧美激情欧美狂野欧美精品| 亚洲区国产区| 欧美专区18| 亚洲午夜激情| 欧美精品videossex性护士| 久久综合五月天婷婷伊人| 久久成人久久爱| 一区二区在线观看视频在线观看| 猫咪成人在线观看| 国产欧美一区二区三区沐欲 | 日韩视频―中文字幕| 欧美夫妇交换俱乐部在线观看| 国产精品中文字幕欧美| 欧美在线观看www| 欧美日韩国产综合视频在线| 亚洲欧美国产77777| 亚洲女人天堂av| 国产一区清纯| 欧美好吊妞视频| 欧美巨乳波霸| 欧美激情精品久久久久久黑人| 国语精品中文字幕| 久久精品99国产精品日本| 欧美一区二区三区在线免费观看| 国产精品久久久一区二区三区| 一本色道久久综合精品竹菊 | 欧美午夜宅男影院| 欧美在线国产精品| 国产精自产拍久久久久久| 亚洲手机视频| 欧美影院久久久| 国产真实久久| 久久艳片www.17c.com| 一区二区三区欧美日韩| 午夜精品久久久| 亚洲国产欧美在线人成| 麻豆精品精品国产自在97香蕉| 欧美成人亚洲成人日韩成人| 亚洲精美视频| 欧美日韩成人| 亚洲免费在线精品一区| 久久精品综合| 国产精品久久影院| 香蕉视频成人在线观看| 一区二区三区日韩欧美精品| 国产精品xxx在线观看www| 亚洲综合成人在线| 另类酷文…触手系列精品集v1小说| 国产一在线精品一区在线观看| 久久成人一区二区| 亚洲福利视频专区| 伊人久久大香线蕉综合热线| 乱码第一页成人| 99精品视频免费观看视频| 欧美一区视频| 亚洲国产精品激情在线观看| 亚洲欧美日韩精品一区二区 | 亚洲欧美日韩人成在线播放| 国内成+人亚洲| 欧美激情综合在线| 午夜综合激情| 亚洲欧洲一区二区三区久久| 在线观看欧美黄色| 欧美日韩在线高清| 久久婷婷影院| 亚洲网站视频福利| 亚洲丶国产丶欧美一区二区三区| 亚洲男女自偷自拍| 亚洲日本中文字幕免费在线不卡| 国产精品视频免费观看www| 在线视频亚洲欧美| 亚洲综合成人在线| 激情欧美一区二区三区在线观看 | 欧美亚洲一区二区在线观看| 亚洲欧美日韩在线观看a三区| 在线免费观看视频一区| 浪潮色综合久久天堂| 亚洲天堂激情| 亚洲激情女人| 模特精品裸拍一区| 亚洲精品视频中文字幕| 韩国成人福利片在线播放| 欧美日韩一卡二卡| 欧美电影在线观看| 久久久免费精品视频| 午夜宅男欧美| 亚洲在线中文字幕| 一本到高清视频免费精品| 亚洲国产精品va在线看黑人| 久久综合九色99| 亚洲麻豆av| 亚洲国产精品免费| 一区二区在线免费观看| 久久综合久久综合九色| 欧美国产大片| 久久久999| 久久成人久久爱| 欧美在线观看日本一区| 亚洲欧美日本在线| 亚洲欧美一区二区在线观看| 国产精品国产三级国产专区53 | 亚洲视频第一页| 亚洲乱亚洲高清| 亚洲三级影院| 亚洲精品免费看| 夜夜狂射影院欧美极品| 日韩视频免费在线| 午夜国产精品影院在线观看| 亚洲一级二级在线| 亚洲欧美在线免费| 性久久久久久久久久久久| 国内精品免费在线观看| 国产一区二区三区久久| 国产综合精品| 亚洲国产岛国毛片在线| 亚洲精品国产系列| 一区二区三区久久久| 性高湖久久久久久久久| 欧美中文在线免费| 麻豆国产精品777777在线| 亚洲成人自拍视频| 欧美一区二区精品| 久久午夜电影| 欧美国产一区二区在线观看| 亚洲精品五月天| 亚洲欧美日韩成人高清在线一区| 欧美在线免费视频| 猛男gaygay欧美视频| 欧美乱大交xxxxx| 国产精品系列在线| 亚洲国产日韩欧美在线动漫| 一级日韩一区在线观看| 亚洲国产精品va| 中文日韩在线视频| 久久久久久久一区二区三区| 欧美国产精品va在线观看| 9色国产精品| 久久国产精品网站| 欧美另类在线播放| 国内精品伊人久久久久av影院| 亚洲精品乱码久久久久久日本蜜臀| 亚洲制服av| 亚洲国产99| 久久av免费一区| 欧美色大人视频| 欧美日韩一区在线播放| 国产综合精品| 国产精品99久久不卡二区| 麻豆国产精品va在线观看不卡 | 女女同性女同一区二区三区91| 日韩亚洲欧美高清| 久久免费观看视频| 国产精品综合av一区二区国产馆| 亚洲国产免费看| 久久国产精品99久久久久久老狼| 亚洲精选一区| 麻豆成人在线| 韩日精品视频一区| 欧美一级欧美一级在线播放|