﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-一步一个脚印，走在自己前面！-随笔分类-C++ Performance </title><link>http://www.cppblog.com/zerolee/category/3014.html</link><description>想想自己5年后会是什么样子……</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 03:30:55 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 03:30:55 GMT</pubDate><ttl>60</ttl><item><title>编译期的依赖性系列</title><link>http://www.cppblog.com/zerolee/archive/2007/03/29/20876.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Thu, 29 Mar 2007 07:02:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2007/03/29/20876.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/20876.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2007/03/29/20876.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/20876.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/20876.html</trackback:ping><description><![CDATA[该文章包括3个部分。<img src ="http://www.cppblog.com/zerolee/aggbug/20876.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2007-03-29 15:02 <a href="http://www.cppblog.com/zerolee/archive/2007/03/29/20876.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>编译器何时为你产生默认构造函数</title><link>http://www.cppblog.com/zerolee/archive/2007/03/27/20676.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Tue, 27 Mar 2007 02:21:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2007/03/27/20676.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/20676.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2007/03/27/20676.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/20676.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/20676.html</trackback:ping><description><![CDATA[
		<p class="docText">
				<font face="Courier New" size="2">总结：<br />    有四种情况，会导致“编译器必须为未声明构造函数的classes合成一个默认构造函数”。C++ 标准把那些合成物称为隐含的有用默认构造函数。被合成出来的构造函数只能满足编译器(非程序)的需要。它之所以能够完成任务，是借着“调用成员对象或基类的默认构造函数”或是“为每一个对象初始化其虚函数机制或虚基类机制”而完成的。至于没有存在那四种情况而又没有声明构造函数的类，我们说它们拥有的是隐含的无用默认构造函数，实际上它们并不被合成出来。<br />    在合成的默认构造函数中，只有基类子对象和成员对象会被初始化。所有其它的非静态数据成员，如整数、整数指针、整数数组等等都不会被初始化。这些初始化操作对程序而言有需要，但对编译器而言则没必要。如果程序需要一个“把某指针设为0”的默认构造函数，那么提供它的人应该是程序员。<br />   <br />    C++新手一般有两个常见的误解：<br />1）任何类如果没有定义默认构造函数，编译器就会合成出它来。<br />2）编译器合成出来的默认构造函数会明确设定“类中每一个数据成员的默认值”。<br /><br />正如你所见，上述两个没有一个是真的！<br />-------------------------------------------------------------------------------------------<br />Summary: <br />   There are four characteristics of a class under which the compiler needs to synthesize a default constructor for classes that declare no constructor at all. The Standard refers to these as implicit, nontrivial default constructors. The synthesized constructor fulfills only an implementation need. It does this by invoking member object or base class default constructors or initializing the virtual function or virtual base class mechanism for each object. Classes that do not exhibit these characteristics and that declare no constructor at all are said to have implicit, trivial default constructors. In practice, these trivial default constructors are not synthesized.</font>
		</p>
		<p class="docText">
				<font face="Courier New" size="2">Within the synthesized default constructor, only the base class subobjects and member class objects are initialized. All other nonstatic data members, such as integers, pointers to integers, arrays of integers, and so on, are not initialized. These initializations are needs of the program, not of the implementation. If there is a program need for a default constructor, such as initializing a pointer to 0, it is the programmer's responsibility to provide it in the course of the class implementation.</font>
		</p>
		<p class="docText">
				<font face="Courier New" size="2">Programmers new to C++ often have two common misunderstandings:<br /><br />1) </font>
				<span style="FONT-WEIGHT: bold">
						<span style="FONT-WEIGHT: normal">
								<font face="Courier New" size="2">That a default constructor is synthesized for every class that does not define one<br />2) </font>
						</span>
						<span style="FONT-WEIGHT: normal">
								<font face="Courier New" size="2">That the compiler-synthesized default constructor provides explicit default initializers<br />   for each data member declared within the class</font>
						</span>
				</span>
		</p>
		<p class="docText">
				<font face="Courier New" size="2">As you have seen, neither of these is true.<br /><br />--------------------------------------------------------------------------------------------<br /></font>
		</p>
<img src ="http://www.cppblog.com/zerolee/aggbug/20676.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2007-03-27 10:21 <a href="http://www.cppblog.com/zerolee/archive/2007/03/27/20676.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>输出字符'A'/'a'的解析</title><link>http://www.cppblog.com/zerolee/archive/2007/01/15/17631.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Mon, 15 Jan 2007 02:59:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2007/01/15/17631.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/17631.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2007/01/15/17631.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/17631.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/17631.html</trackback:ping><description><![CDATA[
		<font face="Courier New" size="2">输出字符'A'/'a'的代码段：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1     </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Test1: Directly insert 0x0A and 0x41 to ostream</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0x0A</span><span style="COLOR: #000000">;    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> print type int 10</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0x41</span><span style="COLOR: #000000">;    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> print type int 65, which is decimal number of 'A'.</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Test2: Directly insert '\x41' to ostream</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">10</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">\x41</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> '\x41' is transformed char, which means type char 'A'.<br /></span><span style="COLOR: #008080">11</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />                       </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> So, it prints type char 'A'  on screen.</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">12</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Test3: First assign 0x41 to char variable, then output it.</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">15</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> char_out </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0x41</span><span style="COLOR: #000000">;    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> char_out is equal 'A' which is type int 65.</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">16</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> char_out;        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> it prints type char 'A' on screen.</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">17</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Test4: First cast 0x0A to char, then output it.</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">20</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> static_cast</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">0x41</span><span style="COLOR: #000000">); // it prints type char 'A' on screen.<br /></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br /><br /></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Test5: Directly insert hexdecimal type number of 'A'</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">24 </span><span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> std::hex </span><span id="Codehighlighter1_748_767_Open_Text"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&lt;&lt; std::showbase</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0x41</span><span style="COLOR: #000000">; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> it prints string literal "41" </span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">25</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> std::hex </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0x0A</span><span style="COLOR: #000000">; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> it prints type char 'a' on screen.<br /></span><span style="COLOR: #008080">26</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> int_out </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">0x41</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> int_out; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> it prints type int 8342**</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">28</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #000000"><font color="#008080">29</font><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> endl;</span></div>   0x41是字符'A'的16进制表示方式，它的10进制数是65，\x41是字符'A'的转义字符。而0x0A跟字符'A'没有丝毫关系，顶多就是0x0A借用了字符'A'而已。故如果想在屏幕上输出字符'A'可将其转义字符直接插入输出流中，或者将0x41转化为字符类型，然后插入，不得使用0x41直接插入，因为0x41本身是int类型的10进制数65。同时也不可以将'0x41'直接插入输出流，因为'0x41'是int类型的数。<br />   可以尝试使用控制符std::hex来插入字符'a'到输出流。<br /></font>
<img src ="http://www.cppblog.com/zerolee/aggbug/17631.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2007-01-15 10:59 <a href="http://www.cppblog.com/zerolee/archive/2007/01/15/17631.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>头文件中的名称</title><link>http://www.cppblog.com/zerolee/archive/2007/01/07/17392.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Sun, 07 Jan 2007 07:00:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2007/01/07/17392.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/17392.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2007/01/07/17392.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/17392.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/17392.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Courier New" size="2">资料整理于《大规模C++程序设计》   <br />   在头文件的文件作用域中声明的名称，可能潜在地与整个系统中任何一个文件的文件作用域名称冲突。即使在一个.cpp文件的文件作用域中声明的带有内部连接的名称也不能保证一定不与.h文件的作用域名称冲突。<br />   主要设计规则：<font color="#ff0000">只有类、结构、联合和自由运算符函数应该在.h文件作用域内声明；只有类、结构、联合和内联函数成员或（内联）自由运算符应该在.h文件的作用域内定义。<br /></font>   以下代码段阐述了上述设计规则。</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #008080"> <font face="Courier New" size="2">1</font></span>
				<font face="Courier New" size="2">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> Driver.h                     </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> fine:comment</span>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 2</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">#ifndef INCLUDED_DRIVER         </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine:internal include guard</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 3</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">#define</span>
								<span style="COLOR: #000000"> INCLUDED_DIRVER</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 4</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
										<br />
								</span>
								<span style="COLOR: #008080"> 5</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />#ifndef INCLUDED_NIFTY          </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine:redundant include guard</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 6</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">#include </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">nifty.h</span>
								<span style="COLOR: #000000">"</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 7</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">#endif</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 8</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
										<br />
								</span>
								<span style="COLOR: #008080"> 9</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">#define</span>
								<span style="COLOR: #000000"> PI 3.141592             </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Avoid:macro constant</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">10</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">#define</span>
								<span style="COLOR: #000000"> MIN(X) ((X)&lt;(Y)?(X):(Y)) </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Avoid:macro constant</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">11</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
										<br />
								</span>
								<span style="COLOR: #008080">12</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">class</span>
								<span style="COLOR: #000000"> ostream;               </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine: class dec.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">13</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">struct</span>
								<span style="COLOR: #000000"> DriverInit;           </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine: class dec.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">14</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">union Uaw;                   </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine: class dec.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">15</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">16</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">extern</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> globalVariable; </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Avoid:external data dec.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">17</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">static</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> fileScopeVariable; </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Avoid:internal data def.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">18</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> BUFFER_SIZE </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">256</span>
								<span style="COLOR: #000000">; </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Avoid:const data def.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">19</span>
								<span style="COLOR: #008000">
										<img id="Codehighlighter1_669_681_Open_Image" onclick="this.style.display='none'; Codehighlighter1_669_681_Open_Text.style.display='none'; Codehighlighter1_669_681_Closed_Image.style.display='inline'; Codehighlighter1_669_681_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_669_681_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_669_681_Closed_Text.style.display='none'; Codehighlighter1_669_681_Open_Image.style.display='inline'; Codehighlighter1_669_681_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">enum</span>
								<span style="COLOR: #000000"> Boolean </span>
								<span id="Codehighlighter1_669_681_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								</span>
								<span id="Codehighlighter1_669_681_Open_Text">
										<span style="COLOR: #000000">{ zero, one }</span>
								</span>
								<span style="COLOR: #000000">;  </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Avoid:enumeration at file scope</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">20</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">typedef </span>
								<span style="COLOR: #0000ff">long</span>
								<span style="COLOR: #000000"> BigInt;         </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> typedef at file scope</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">21</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">22</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_788_1419_Open_Image" onclick="this.style.display='none'; Codehighlighter1_788_1419_Open_Text.style.display='none'; Codehighlighter1_788_1419_Closed_Image.style.display='inline'; Codehighlighter1_788_1419_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_788_1419_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_788_1419_Closed_Text.style.display='none'; Codehighlighter1_788_1419_Open_Image.style.display='inline'; Codehighlighter1_788_1419_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">class</span>
								<span style="COLOR: #000000"> Driver </span>
								<span id="Codehighlighter1_788_1419_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_788_1419_Open_Text">
						<span style="COLOR: #000000">
								<font face="Courier New" size="2">{<br /></font>
						</span>
						<span style="COLOR: #008080">23</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_804_817_Open_Image" onclick="this.style.display='none'; Codehighlighter1_804_817_Open_Text.style.display='none'; Codehighlighter1_804_817_Closed_Image.style.display='inline'; Codehighlighter1_804_817_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_804_817_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_804_817_Closed_Text.style.display='none'; Codehighlighter1_804_817_Open_Image.style.display='inline'; Codehighlighter1_804_817_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />   </span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #0000ff">enum</span>
										<span style="COLOR: #000000"> Color </span>
										<span id="Codehighlighter1_804_817_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										</span>
										<span id="Codehighlighter1_804_817_Open_Text">
												<span style="COLOR: #000000">{ RED, GREEN }</span>
										</span>
										<span style="COLOR: #000000">; </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine:enumeration in class scope</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">24</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   typedef </span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000"> (Dirver::</span>
										<span style="COLOR: #000000">*</span>
										<span style="COLOR: #000000">PMF)(); </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine: typedef in class scope</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">25</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   </span>
										<span style="COLOR: #0000ff">static</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000"> s_count;      </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine:static member dec.</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">26</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   </span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000"> d_size;    </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine: member data def.</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">27</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">28</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #0000ff">private</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">:<br /></span>
										<span style="COLOR: #008080">29</span>
										<span style="COLOR: #000000">
												<img id="Codehighlighter1_1043_1127_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1043_1127_Open_Text.style.display='none'; Codehighlighter1_1043_1127_Closed_Image.style.display='inline'; Codehighlighter1_1043_1127_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
												<img id="Codehighlighter1_1043_1127_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1043_1127_Closed_Text.style.display='none'; Codehighlighter1_1043_1127_Open_Image.style.display='inline'; Codehighlighter1_1043_1127_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">struct</span>
										<span style="COLOR: #000000"> Pnt </span>
										<span id="Codehighlighter1_1043_1127_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										</span>
								</font>
						</font>
						<span id="Codehighlighter1_1043_1127_Open_Text">
								<font face="Courier New">
										<font size="2">
												<span style="COLOR: #000000">{<br /></span>
												<span style="COLOR: #008080">30</span>
												<span style="COLOR: #000000">
														<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      </span>
												<span style="COLOR: #0000ff">short</span>
												<span style="COLOR: #000000"> </span>
												<span style="COLOR: #0000ff">int</span>
										</font>
								</font>
								<font face="Courier New">
										<font size="2">
												<span style="COLOR: #000000"> d_x, d_y;<br /></span>
												<span style="COLOR: #008080">31</span>
												<span style="COLOR: #000000">
														<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      Pnt(</span>
												<span style="COLOR: #0000ff">int</span>
												<span style="COLOR: #000000"> x, </span>
												<span style="COLOR: #0000ff">int</span>
										</font>
								</font>
								<font face="Courier New">
										<font size="2">
												<span style="COLOR: #000000"> y) <br /></span>
												<span style="COLOR: #008080">32</span>
												<span style="COLOR: #000000">
														<img id="Codehighlighter1_1120_1122_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1120_1122_Open_Text.style.display='none'; Codehighlighter1_1120_1122_Closed_Image.style.display='inline'; Codehighlighter1_1120_1122_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
														<img id="Codehighlighter1_1120_1122_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1120_1122_Closed_Text.style.display='none'; Codehighlighter1_1120_1122_Open_Image.style.display='inline'; Codehighlighter1_1120_1122_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />       : d_x(x), d_y(y) </span>
												<span id="Codehighlighter1_1120_1122_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
												</span>
												<span id="Codehighlighter1_1120_1122_Open_Text">
														<span style="COLOR: #000000">{ }</span>
												</span>
										</font>
								</font>
								<span style="COLOR: #000000">
										<br />
								</span>
								<font face="Courier New">
										<font size="2">
												<span style="COLOR: #008080">33</span>
												<span style="COLOR: #000000">
														<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />   }</span>
										</font>
								</font>
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">;              </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine: private struct def.</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">34</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   friend DirverInit;  </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine: friend dec.</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">35</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">36</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #0000ff">public</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">:<br /></span>
										<span style="COLOR: #008080">37</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">static</span>
										<span style="COLOR: #000000"> round(</span>
										<span style="COLOR: #0000ff">double</span>
										<span style="COLOR: #000000"> d);  </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine:static member function dec.</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">38</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   </span>
										<span style="COLOR: #0000ff">void</span>
										<span style="COLOR: #000000"> setSize(</span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000"> size); </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine: member function dec.</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">39</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   </span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000"> cmp(</span>
										<span style="COLOR: #0000ff">const</span>
										<span style="COLOR: #000000"> Driver</span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000">) </span>
										<span style="COLOR: #0000ff">const</span>
										<span style="COLOR: #000000">; </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> fine: const member function dec.</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">40</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />
										</span>
										<span style="COLOR: #000000">}</span>
								</font>
						</font>
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">;               </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine: class def.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">41</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">42</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_1481_1492_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1481_1492_Open_Text.style.display='none'; Codehighlighter1_1481_1492_Closed_Image.style.display='inline'; Codehighlighter1_1481_1492_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_1481_1492_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1481_1492_Closed_Text.style.display='none'; Codehighlighter1_1481_1492_Open_Image.style.display='inline'; Codehighlighter1_1481_1492_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">static</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">class</span>
								<span style="COLOR: #000000"> DriverInit </span>
								<span id="Codehighlighter1_1481_1492_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_1481_1492_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">43</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />  </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> </span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">44</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />
										</span>
										<span style="COLOR: #000000">}</span>
								</font>
						</font>
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> DriverInit;   </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Special class</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">45</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">46</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> min(</span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> x, </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> y);  </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Avoid: free function dec.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">47</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">48</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />inline </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> max(</span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> x, </span>
								<span style="COLOR: #0000ff">int</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> y)<br /></span>
								<span style="COLOR: #008080">49</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_1609_1636_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1609_1636_Open_Text.style.display='none'; Codehighlighter1_1609_1636_Closed_Image.style.display='inline'; Codehighlighter1_1609_1636_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_1609_1636_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">50</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">return</span>
										<span style="COLOR: #000000"> x </span>
										<span style="COLOR: #000000">&gt;</span>
										<span style="COLOR: #000000"> y </span>
										<span style="COLOR: #000000">?</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<font face="Courier New" size="2"> x : y;<br /></font>
						</span>
						<span style="COLOR: #000000">
								<font face="Courier New" color="#008080" size="2">51    </font>}</span>
				</span>
				<span style="COLOR: #000000">   </span>
				<font face="Courier New" size="2">
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> Avoid free inline function def.</span>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">52</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">53</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />inline </span>
								<span style="COLOR: #0000ff">void</span>
								<span style="COLOR: #000000"> Driver::setSize(</span>
								<span style="COLOR: #0000ff">int</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> size) <br /></span>
								<span style="COLOR: #008080">54</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_1715_1735_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1715_1735_Open_Text.style.display='none'; Codehighlighter1_1715_1735_Closed_Image.style.display='inline'; Codehighlighter1_1715_1735_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_1715_1735_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">55</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   d_size </span>
										<span style="COLOR: #000000">=</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> size;<br /></span>
										<span style="COLOR: #008080">56</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">    </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine: inline member function def.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">57</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">58</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />ostream</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">operator</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">&lt;&lt;</span>
								<span style="COLOR: #000000">(ostream</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> o, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Dirver</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> d);  </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine: free operator function def.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">59</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">60</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />inline </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">operator</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">==</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Driver</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> lhs, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Dirver</span>
								<span style="COLOR: #000000">&amp;</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> rhs) <br /></span>
								<span style="COLOR: #008080">61</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_1930_1966_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1930_1966_Open_Text.style.display='none'; Codehighlighter1_1930_1966_Closed_Image.style.display='inline'; Codehighlighter1_1930_1966_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_1930_1966_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">62</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">return</span>
										<span style="COLOR: #000000"> compare(lhs, rhs) </span>
										<span style="COLOR: #000000">==</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">;<br /></span>
										<span style="COLOR: #008080">63</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">    </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine: free inline operator func. def.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">64</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">65</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />inline </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> Driver::round(</span>
								<span style="COLOR: #0000ff">double</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> d)<br /></span>
								<span style="COLOR: #008080">66</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_2048_2098_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2048_2098_Open_Text.style.display='none'; Codehighlighter1_2048_2098_Closed_Image.style.display='inline'; Codehighlighter1_2048_2098_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_2048_2098_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">67</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   retrun d </span>
										<span style="COLOR: #000000">&lt;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">?</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">-</span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">0.5</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">-</span>
										<span style="COLOR: #000000"> d) : </span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">0.5</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">+</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> d);<br /></span>
										<span style="COLOR: #008080">68</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">    </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> fine: inline static member func. def.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">69</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">70</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">#endif</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">71</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
										<br />
								</span>
								<span style="COLOR: #008080">72</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<font face="Courier New" size="2">
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						</font>
				</span>
		</div>
<img src ="http://www.cppblog.com/zerolee/aggbug/17392.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2007-01-07 15:00 <a href="http://www.cppblog.com/zerolee/archive/2007/01/07/17392.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>The Return Value Optimization[2]</title><link>http://www.cppblog.com/zerolee/archive/2006/11/13/15137.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Mon, 13 Nov 2006 11:36:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2006/11/13/15137.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/15137.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2006/11/13/15137.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/15137.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/15137.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Courier New" size="2">-------&gt;<br />Without any optimization, the compile-generated(pseduo) code for Complex_Add() is<br /></font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">void</span>
								<span style="COLOR: #000000"> Complex_Add(</span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> __tempResult, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> c1, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> c2)<br /></span>
								<span style="COLOR: #008080"> 2</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_84_352_Open_Image" onclick="this.style.display='none'; Codehighlighter1_84_352_Open_Text.style.display='none'; Codehighlighter1_84_352_Closed_Image.style.display='inline'; Codehighlighter1_84_352_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_84_352_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080"> 3</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">struct</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> Complex retVal;<br /></span>
										<span style="COLOR: #008080"> 4</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   retVal.Complex::Complex(); </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> Constructor retval</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080"> 5</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   retVal.real </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> a.real </span>
										<span style="COLOR: #000000">+</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> b.real;<br /></span>
										<span style="COLOR: #008080"> 6</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   retVal.imag </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> a.imag </span>
										<span style="COLOR: #000000">+</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> b.imag;<br /></span>
										<span style="COLOR: #008080"> 7</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   __tempResult.Complex::Complex(retVal); </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> copy-constructor</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080"> 8</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">  retVal.Complex::</span>
										<span style="COLOR: #000000">~</span>
										<span style="COLOR: #000000">Complex(); </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> destroy retVal</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080"> 9</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">  </span>
										<span style="COLOR: #0000ff">return</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">;<br /></span>
										<span style="COLOR: #008080">10</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Courier New" size="2">The compiler can optimize Complex_Add() by eliminating the local object retVal and replacing it with __tempResult. This is the Return Value Optimization:<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">void</span>
								<span style="COLOR: #000000"> Complex_Add(</span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> __tempResult, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> c1, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> c2)<br /></span>
								<span style="COLOR: #008080">2</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_84_242_Open_Image" onclick="this.style.display='none'; Codehighlighter1_84_242_Open_Text.style.display='none'; Codehighlighter1_84_242_Closed_Image.style.display='inline'; Codehighlighter1_84_242_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_84_242_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">3</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   __tempResult.Complex::Complex();  </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> construcotr __tempResult</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">4</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   __tempResult.real </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> a.real </span>
										<span style="COLOR: #000000">+</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> b.real;<br /></span>
										<span style="COLOR: #008080">5</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   __tempResult.imag </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> a.imag </span>
										<span style="COLOR: #000000">+</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> b.imag;<br /></span>
										<span style="COLOR: #008080">6</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">return</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">;<br /></span>
										<span style="COLOR: #008080">7</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Courier New" size="2">The RVO eliminated the local retVal object and therefore saved us a constructor as well as a destructor computation.<br />To get a numerical feel for all this efficiency discussion, we measured the impact of RVO on execution speed. We coded two versions of operator +(), one of which was optimized and the other not. The measured code consisted of a million loop iterations:<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">int</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> main()<br /></span>
								<span style="COLOR: #008080"> 2</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_11_176_Open_Image" onclick="this.style.display='none'; Codehighlighter1_11_176_Open_Text.style.display='none'; Codehighlighter1_11_176_Closed_Image.style.display='inline'; Codehighlighter1_11_176_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_11_176_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080"> 3</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   Complex a(</span>
										<span style="COLOR: #000000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #000000">0</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #008080"> 4</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   Complex b(</span>
										<span style="COLOR: #000000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #000000">0</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #008080"> 5</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   Complex c;<br /></span>
										<span style="COLOR: #008080"> 6</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> begin timing here</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080"> 7</span>
										<span style="COLOR: #008000">
												<img id="Codehighlighter1_126_148_Open_Image" onclick="this.style.display='none'; Codehighlighter1_126_148_Open_Text.style.display='none'; Codehighlighter1_126_148_Closed_Image.style.display='inline'; Codehighlighter1_126_148_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
										</span>
										<span style="COLOR: #000000">   </span>
										<span style="COLOR: #0000ff">for</span>
										<span style="COLOR: #000000"> (</span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000"> i </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">1000000</span>
										<span style="COLOR: #000000">; i </span>
										<span style="COLOR: #000000">&gt;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">; </span>
										<span style="COLOR: #000000">--</span>
										<span style="COLOR: #000000">i) </span>
										<span id="Codehighlighter1_126_148_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										</span>
								</font>
						</font>
						<span id="Codehighlighter1_126_148_Open_Text">
								<font face="Courier New">
										<font size="2">
												<span style="COLOR: #000000">{<br /></span>
												<span style="COLOR: #008080"> 8</span>
												<span style="COLOR: #000000">
														<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      c </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000"> a </span>
												<span style="COLOR: #000000">+</span>
										</font>
								</font>
								<font face="Courier New">
										<font size="2">
												<span style="COLOR: #000000"> b;<br /></span>
												<span style="COLOR: #008080"> 9</span>
												<span style="COLOR: #000000">
														<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />   }</span>
										</font>
								</font>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">10</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> stoping timing here</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">11</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />
										</span>
										<span style="COLOR: #000000">}</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Courier New" size="2">The second version, without RVO, executed in 1.89 seconds. The first version, with RVO applied was much faster --1.30 seconds.<br /><br />Compiler optimizations, naturally, must preserve the correctness of the original computation. In the case of the RVO, this is not always easy. Since the RVO is not mandatory, the compiler will not perform it on comlicated functions. For example, if the function has multiple return statements returning objects of different names, RVO will not be applied. You must return the same named object to have a chance at the RVO.<br />One compiler we tested refused to apply the RVO to this particular version of operator +:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">1</span><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">Complex </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Complex</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> a, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Complex</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> b)<br /></span><span style="COLOR: #008080">2</span><span style="COLOR: #000000"><img id="Codehighlighter1_55_189_Open_Image" onclick="this.style.display='none'; Codehighlighter1_55_189_Open_Text.style.display='none'; Codehighlighter1_55_189_Closed_Image.style.display='inline'; Codehighlighter1_55_189_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /></span><span id="Codehighlighter1_55_189_Open_Text"><span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> operator + version 1</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">4</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #000000">   Complex retVal;<br /></span><span style="COLOR: #008080">5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   retVal.real </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> a.real </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> b.real;<br /></span><span style="COLOR: #008080">6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   retVal.imag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> a.imag </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> b.imag;<br /></span><span style="COLOR: #008080">7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> retVal;<br /></span><span style="COLOR: #008080">8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>It did, however, apply the RVO to this version:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">1</span><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">Complex </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Complex</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> a, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Complex</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> b)<br /></span><span style="COLOR: #008080">2</span><span style="COLOR: #000000"><img id="Codehighlighter1_55_171_Open_Image" onclick="this.style.display='none'; Codehighlighter1_55_171_Open_Text.style.display='none'; Codehighlighter1_55_171_Closed_Image.style.display='inline'; Codehighlighter1_55_171_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /></span><span id="Codehighlighter1_55_171_Open_Text"><span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> operator + version 2</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">4</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #000000">   </span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000"> r </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> a.real </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> b.real;<br /></span><span style="COLOR: #008080">5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000"> i </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> a.imag </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> b.imag;<br /></span><span style="COLOR: #008080">6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> Complex(r, i);<br /></span><span style="COLOR: #008080">7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />   </span></div>We speculated that the difference may lie in the fact that Version 1 used a named variable(retVal) as a return value whereas Version 2 used an unnamed variable. Version 2 used a constructor call in the return statement but never named it. It may be the case that this particular compiler implementation chose to avoid optimizing away named variables.<br />Our speculation was boosted by some additional evidence. We tested two more versions of operator +:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">Complex </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Complex</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> a, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Complex</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> b)<br /></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #000000"><img id="Codehighlighter1_55_155_Open_Image" onclick="this.style.display='none'; Codehighlighter1_55_155_Open_Text.style.display='none'; Codehighlighter1_55_155_Closed_Image.style.display='inline'; Codehighlighter1_55_155_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /></span><span id="Codehighlighter1_55_155_Open_Text"><span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> operator + version 3</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #000000">   Complex retVal(a.real </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> b.real, a.imag </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> b.imag);<br /></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> retVal;<br /></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />and<br /></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />Complex </span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Complex</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> a, </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> Complex</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> b)<br /></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img id="Codehighlighter1_216_298_Open_Image" onclick="this.style.display='none'; Codehighlighter1_216_298_Open_Text.style.display='none'; Codehighlighter1_216_298_Closed_Image.style.display='inline'; Codehighlighter1_216_298_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /></span><span id="Codehighlighter1_216_298_Open_Text"><span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> operator + version 4</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">11</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #000000">   </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> Complex(a.real </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> b.real, a.imag </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> b.imag);<br /></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>As speculated, the RVO was applied to Version 4 but not to Version 3.<br />In addition, you must also define a copy constructor to "Turn on" the Return Value Optimization. If the class involved does not have a copy constructor defined, the RVO is quietly turned off.<br /><br /><font color="#006400">Key Points:<br />[1] If you must return an object by value, the Return Value Optimization will help performance by eliminating the nedd for creation and destruction of a local object.<br /><br />[2] The application of the RVO is up to the direction of the compiler implementation. You need to consult your compile documentation or experiment to find if and when RVO is applied.<br /><br />[3] You will have a better shot at RVO by deploying the computational constructor.</font></font>
<img src ="http://www.cppblog.com/zerolee/aggbug/15137.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2006-11-13 19:36 <a href="http://www.cppblog.com/zerolee/archive/2006/11/13/15137.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>The Return Value Optimization[1]</title><link>http://www.cppblog.com/zerolee/archive/2006/11/13/15136.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Mon, 13 Nov 2006 11:01:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2006/11/13/15136.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/15136.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2006/11/13/15136.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/15136.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/15136.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Courier New" color="#ff0000" size="2">Resource from the book: <br />Dov Bulka &amp; David Mayhew &lt;Efficient C++--- Performance programming Techiques&gt; <br /><br /></font>
				<font face="Courier New" size="2">Anytime you can skip the creation and destruction of an object, you are looking at a performance gain. We will discuss an optimization often performed by compilers to speed  up your source code by transforming it and eliminating object creation. This optimization is referred to as the Return Value Optimization(RVO). Prior to delving into the RVO we need to understand how return-by-value works. We will walk through it with a simple example.<br /><br />The Mechanics of Return-by-Value<br />The Complex class implements a representation for complex numbers:</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">class</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> Complex<br /></span>
								<span style="COLOR: #008080"> 2</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_14_426_Open_Image" onclick="this.style.display='none'; Codehighlighter1_14_426_Open_Text.style.display='none'; Codehighlighter1_14_426_Closed_Image.style.display='inline'; Codehighlighter1_14_426_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_14_426_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080"> 3</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<font color="#a52a2a">
												<span style="COLOR: #008000">//</span>
												<span style="COLOR: #008000"> Complex addition operator</span>
										</font>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080"> 4</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #000000">friend Complex </span>
										<span style="COLOR: #0000ff">operator</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">+</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #0000ff">const</span>
										<span style="COLOR: #000000"> Complex</span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000">, </span>
										<span style="COLOR: #0000ff">const</span>
										<span style="COLOR: #000000"> Complex</span>
										<span style="COLOR: #000000">&amp;</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #008080"> 5</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #0000ff">public</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">:<br /></span>
										<span style="COLOR: #008080"> 6</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008000"> default constructor<br /></span>
										<span style="COLOR: #008080"> 7</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008000"> Value defaults to 0 unless otherwise specified<br /></span>
										<span style="COLOR: #008080"> 8</span>
										<span style="COLOR: #008000">
												<img id="Codehighlighter1_243_245_Open_Image" onclick="this.style.display='none'; Codehighlighter1_243_245_Open_Text.style.display='none'; Codehighlighter1_243_245_Closed_Image.style.display='inline'; Codehighlighter1_243_245_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />   </span>
										<span style="COLOR: #000000">Complex(</span>
										<span style="COLOR: #0000ff">double</span>
										<span style="COLOR: #000000"> r </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0.0</span>
										<span style="COLOR: #000000">, </span>
										<span style="COLOR: #0000ff">double</span>
										<span style="COLOR: #000000"> i </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0.0</span>
										<span style="COLOR: #000000">):real(r), imag(i) </span>
										<span id="Codehighlighter1_243_245_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										</span>
										<span id="Codehighlighter1_243_245_Open_Text">
												<span style="COLOR: #000000">{ }</span>
										</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080">
								<font face="Courier New" size="2"> 9</font>
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
												<br />
										</span>
										<span style="COLOR: #008080">10</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> copy constructor</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">11</span>
										<span style="COLOR: #008000">
												<img id="Codehighlighter1_321_323_Open_Image" onclick="this.style.display='none'; Codehighlighter1_321_323_Open_Text.style.display='none'; Codehighlighter1_321_323_Closed_Image.style.display='inline'; Codehighlighter1_321_323_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />   </span>
										<span style="COLOR: #000000">Complex(</span>
										<span style="COLOR: #0000ff">const</span>
										<span style="COLOR: #000000"> Complex</span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> c):real(c.real), imag(c.imag) </span>
										<span id="Codehighlighter1_321_323_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										</span>
										<span id="Codehighlighter1_321_323_Open_Text">
												<span style="COLOR: #000000">{ }</span>
										</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080">
								<font face="Courier New" size="2">12</font>
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
												<br />
										</span>
										<span style="COLOR: #008080">13</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #008000">//</span>
										<span style="COLOR: #008000"> Assignment operator</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">14</span>
										<span style="COLOR: #008000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #000000">Complex</span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">operator</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #0000ff">const</span>
										<span style="COLOR: #000000"> Complex</span>
										<span style="COLOR: #000000">&amp;</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> c);<br /></span>
										<span style="COLOR: #008080">15</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
												<br />
										</span>
										<span style="COLOR: #008080">16</span>
										<span style="COLOR: #000000">
												<img id="Codehighlighter1_400_402_Open_Image" onclick="this.style.display='none'; Codehighlighter1_400_402_Open_Text.style.display='none'; Codehighlighter1_400_402_Closed_Image.style.display='inline'; Codehighlighter1_400_402_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />   </span>
										<span style="COLOR: #000000">~</span>
										<span style="COLOR: #000000">Complex() </span>
										<span id="Codehighlighter1_400_402_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										</span>
										<span id="Codehighlighter1_400_402_Open_Text">
												<span style="COLOR: #000000">{ }</span>
										</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
						</span>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #008080">17</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										</span>
										<span style="COLOR: #0000ff">private</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">:<br /></span>
										<span style="COLOR: #008080">18</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">double</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> real;<br /></span>
										<span style="COLOR: #008080">19</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<span style="COLOR: #000000">
						<font face="Courier New" size="2">;</font>
				</span>
		</div>
		<font face="Courier New" size="2">The addition operator returns a Complex object by value, as in:<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #000000">Complex </span>
								<span style="COLOR: #0000ff">operator</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> a, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> b)<br /></span>
								<span style="COLOR: #008080">2</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_55_166_Open_Image" onclick="this.style.display='none'; Codehighlighter1_55_166_Open_Text.style.display='none'; Codehighlighter1_55_166_Closed_Image.style.display='inline'; Codehighlighter1_55_166_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_55_166_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">3</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    Complex retVal;<br /></span>
										<span style="COLOR: #008080">4</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    retVal.real </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> a.real </span>
										<span style="COLOR: #000000">+</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> b.real;<br /></span>
										<span style="COLOR: #008080">5</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    retVal.imag </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> a.imag </span>
										<span style="COLOR: #000000">+</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> b.imag;<br /></span>
										<span style="COLOR: #008080">6</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
										<span style="COLOR: #0000ff">return</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> retVal;<br /></span>
										<span style="COLOR: #008080">7</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Courier New" size="2">Suppose c1, c2, and c3 are Complex and we excute<br />c3 = c1 + c2;<br />How do we get the value of c1 + c2 into c3? One popular technique used by compilers is to create a temporary __result object and pass it into Complex::operator +() as a third argument. It is passed by referece. So the compiler rewrites<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #000000">Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> Complex::</span>
								<span style="COLOR: #0000ff">operator</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> c1, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> c2)<br /></span>
								<span style="COLOR: #008080">2</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_67_75_Open_Image" onclick="this.style.display='none'; Codehighlighter1_67_75_Open_Text.style.display='none'; Codehighlighter1_67_75_Closed_Image.style.display='inline'; Codehighlighter1_67_75_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_67_75_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">3</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />  <img src="http://www.cppblog.com/images/dot.gif" /><br /></span>
										<span style="COLOR: #008080">4</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Courier New" size="2">into a slightly different function:<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">void</span>
								<span style="COLOR: #000000"> Complex_Add(</span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> __result, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
								<span style="COLOR: #000000"> c1, </span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> Complex</span>
								<span style="COLOR: #000000">&amp;</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> c2)<br /></span>
								<span style="COLOR: #008080">2</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_80_89_Open_Image" onclick="this.style.display='none'; Codehighlighter1_80_89_Open_Text.style.display='none'; Codehighlighter1_80_89_Closed_Image.style.display='inline'; Codehighlighter1_80_89_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_80_89_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">3</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   <img src="http://www.cppblog.com/images/dot.gif" /><br /></span>
										<span style="COLOR: #008080">4</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Courier New" size="2">Now the original source statement<br />c3 = c1 + c2;<br />is transformed into(pseudocode):<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">struct</span>
								<span style="COLOR: #000000"> Complex __tempResult; </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Storage. No constructor here.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">2</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">Complex_Add(__tempResult, c1, c2); </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> All argument passed by reference.</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">3</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">c3 </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> __tempResult; </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Feed result back into left-hand-side.</span>
						</font>
				</font>
		</div>
		<font face="Courier New" size="2">This return-by-value implementation opens up an optimization opportunity by eliminating the local object RetVal(inside operator +()) and computing the return value directly into the __tempResult temporary object. This is the Return Value Optimization.</font>
<img src ="http://www.cppblog.com/zerolee/aggbug/15136.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2006-11-13 19:01 <a href="http://www.cppblog.com/zerolee/archive/2006/11/13/15136.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Template and Inheritance</title><link>http://www.cppblog.com/zerolee/archive/2006/11/13/15124.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Mon, 13 Nov 2006 05:37:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2006/11/13/15124.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/15124.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2006/11/13/15124.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/15124.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/15124.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Courier New" size="2">As Meyers noted in Item 24 of <font color="#006400">Effective C++,</font><font color="#ff0000"><strong>the inability to inline a virtual function is its biggest performace penalty.<br /></strong></font>Virtual functions seems to inflict a performace cost in several ways:<br /><font color="#006400">[1] The vptr must be initialized in the constructor.<br />[2] A virtual function is invoked via pointer indirection. We must fetch the pointer to the function table and then access the correct function offset.<br />[3] Inlining is a compile-time decision. The compiler cannot inline virtual functions whose resolution takes place at run-time.<br /></font>The true cost of virtual functions then boils down to the third item only. <br /><br />-------------------------------------------------------------------------------------------<br />Virtual function calls that can be resolved only at rum-time will inhibit inling. At times, that may pose a performace problem that we must solve. Dynamic binding of a function call is a consequence of inheritance. One way to eliminate dyanamic binding is to replace inheritance with a template-based design. Templates are more performance-friendly in the sense that they push the resolution step from run-time to compile-time. Compile-time, as far as we are concerned, is free.<br /><br />The desing space for inheritance and templates has some overlap. We will discuss one such example.<br /><br />Suppose you wanted to develop a thread-safe string class that may be manipulated safely by concurrent threads in a Win32 environment. In that environment you have a choice of multiple synchronization schemes such as<font color="#006400"></font><em><font color="#006400">critical</font><font color="#006400">section</font></em>, <em><font color="#006400">mutex</font></em>, and <em><font color="#006400">semanphores</font></em>, just to name a few. You would like your thread-safe string to offer the flexibility to use any of those schemes, and at different times you may have a reason to prefer one scheme over another. Inheritance would be a reasonable choice to capture the commonality among synchronization mechanisms.<br /><br />The Locker abstract base class will declare the common interface:</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 304px; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #008080"> 1</span>
				<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> Locker<br /></span>
				<span style="COLOR: #008080"> 2</span>
				<span style="COLOR: #000000">
						<img id="Codehighlighter1_13_126_Open_Image" onclick="this.style.display='none'; Codehighlighter1_13_126_Open_Text.style.display='none'; Codehighlighter1_13_126_Closed_Image.style.display='inline'; Codehighlighter1_13_126_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
				</span>
				<span id="Codehighlighter1_13_126_Open_Text">
						<span style="COLOR: #000000">{<br /></span>
						<span style="COLOR: #008080"> 3</span>
						<span style="COLOR: #000000">
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000">:<br /></span>
						<span style="COLOR: #008080"> 4</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_36_38_Open_Image" onclick="this.style.display='none'; Codehighlighter1_36_38_Open_Text.style.display='none'; Codehighlighter1_36_38_Closed_Image.style.display='inline'; Codehighlighter1_36_38_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />    Locker() </span>
						<span id="Codehighlighter1_36_38_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						</span>
						<span id="Codehighlighter1_36_38_Open_Text">
								<span style="COLOR: #000000">{ }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080"> 5</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_62_64_Open_Image" onclick="this.style.display='none'; Codehighlighter1_62_64_Open_Text.style.display='none'; Codehighlighter1_62_64_Closed_Image.style.display='inline'; Codehighlighter1_62_64_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">virtual</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">~</span>
						<span style="COLOR: #000000">Locker() </span>
						<span id="Codehighlighter1_62_64_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						</span>
						<span id="Codehighlighter1_62_64_Open_Text">
								<span style="COLOR: #000000">{ }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080"> 6</span>
						<span style="COLOR: #000000">
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">virtual</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">lock</span>
						<span style="COLOR: #000000">() </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">0</span>
						<span style="COLOR: #000000">;<br /></span>
						<span style="COLOR: #008080"> 7</span>
						<span style="COLOR: #000000">
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">virtual</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> unlock() </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">0</span>
						<span style="COLOR: #000000">;<br /></span>
						<span style="COLOR: #008080"> 8</span>
						<span style="COLOR: #000000">
								<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">;<br /></span>
				<span style="COLOR: #008080"> 9</span>
				<span style="COLOR: #000000">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<br />
				</span>
				<span style="COLOR: #008080">10</span>
				<span style="COLOR: #000000">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> CriticalSectionLock : </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> Locker<br /></span>
				<span style="COLOR: #008080">11</span>
				<span style="COLOR: #000000">
						<img id="Codehighlighter1_172_179_Open_Image" onclick="this.style.display='none'; Codehighlighter1_172_179_Open_Text.style.display='none'; Codehighlighter1_172_179_Closed_Image.style.display='inline'; Codehighlighter1_172_179_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
				</span>
				<span id="Codehighlighter1_172_179_Open_Text">
						<span style="COLOR: #000000">{ <br /></span>
						<span style="COLOR: #008080">12</span>
						<span style="COLOR: #000000">
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								<img src="http://www.cppblog.com/images/dot.gif" />
								<br />
						</span>
						<span style="COLOR: #008080">13</span>
						<span style="COLOR: #000000">
								<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">;<br /></span>
				<span style="COLOR: #008080">14</span>
				<span style="COLOR: #000000">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> MutexLock : </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> Locker<br /></span>
				<span style="COLOR: #008080">15</span>
				<span style="COLOR: #000000">
						<img id="Codehighlighter1_214_221_Open_Image" onclick="this.style.display='none'; Codehighlighter1_214_221_Open_Text.style.display='none'; Codehighlighter1_214_221_Closed_Image.style.display='inline'; Codehighlighter1_214_221_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
				</span>
				<span id="Codehighlighter1_214_221_Open_Text">
						<span style="COLOR: #000000">{<br /></span>
						<span style="COLOR: #008080">16</span>
						<span style="COLOR: #000000">
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								<img src="http://www.cppblog.com/images/dot.gif" /> <br /></span>
						<span style="COLOR: #008080">17</span>
						<span style="COLOR: #000000">
								<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">;</span>
		</div>Because you prefer not to re-invent the wheel, you made the choice to derive the thread-safe string from the existing standard string. The remaining design choices are:<br /><br />[1] <font color="#ff0000">Hard coding</font>. You could derive three distinct classes from string::CriticalSectionString, MutexString, and SemaphoreString, each class implementing its implied synchronization mechanism.<br />[2] <font color="#ff0000">Inheritance</font>. You could derive a single ThreadSafeString class that contains a pointer to a Locker object. Use polynorphism to select the particular synchronization mechanism at run-time.<br />[3] <font color="#ff0000">Templates</font>. Create a template-based string class parameterized by the Locker type.<br />////////////////////////////////////////////////////////////////////////////////////////////<br />Here we only talk about the Template implementation.<br /><br />The templates-based design combines the best of both worlds-reuse and efficiency. The ThreadSafeString is implemented as a template parameterized by the Locker template argument:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">template </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> LOCKER</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> ThreadSafeString : </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #000000"><img id="Codehighlighter1_63_177_Open_Image" onclick="this.style.display='none'; Codehighlighter1_63_177_Open_Text.style.display='none'; Codehighlighter1_63_177_Closed_Image.style.display='inline'; Codehighlighter1_63_177_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /></span><span id="Codehighlighter1_63_177_Open_Text"><span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   ThreadSafeString(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> s) <br /></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img id="Codehighlighter1_124_126_Open_Image" onclick="this.style.display='none'; Codehighlighter1_124_126_Open_Text.style.display='none'; Codehighlighter1_124_126_Closed_Image.style.display='inline'; Codehighlighter1_124_126_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />   : </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">(s) </span><span id="Codehighlighter1_124_126_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"></span><span id="Codehighlighter1_124_126_Open_Text"><span style="COLOR: #000000">{ }</span></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   <img src="http://www.cppblog.com/images/dot.gif" /><br /></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> length();<br /></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   LOCKER </span><span style="COLOR: #0000ff">lock</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span></div>The length method implementation is similar to the previous ones:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">template </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> LOCKER</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />inline<br /></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> ThreadSafeString</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">LOCKER</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"> :: length()<br /></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #000000"><img id="Codehighlighter1_72_151_Open_Image" onclick="this.style.display='none'; Codehighlighter1_72_151_Open_Text.style.display='none'; Codehighlighter1_72_151_Closed_Image.style.display='inline'; Codehighlighter1_72_151_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_72_151_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_72_151_Closed_Text.style.display='none'; Codehighlighter1_72_151_Open_Image.style.display='inline'; Codehighlighter1_72_151_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span id="Codehighlighter1_72_151_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_72_151_Open_Text"><span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />  </span><span style="COLOR: #0000ff">lock</span><span style="COLOR: #000000">.</span><span style="COLOR: #0000ff">lock</span><span style="COLOR: #000000">();<br /></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />  </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> len </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">::length();<br /></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />  </span><span style="COLOR: #0000ff">lock</span><span style="COLOR: #000000">.unlock();<br /></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />  </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> len;<br /></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>If you want critical section protection, you will instantiate the template with a CriticalSectionLock:<br />{<br />   ThreadSafeString&lt;CriticalSectionLock&gt; csString = "Hello";<br />   ...<br />}<br />or you may go with a mutex:<br />{<br />   ThreadSafeString&lt;MutexLock&gt; mtxString = "Hello";<br />   ...<br />}<br /><br />This design also provides a relief from the virtual function calls to lock() and unlock(). The declaration of a <font color="#006400"><em>ThreadSafeString</em></font> selects a particular type of synchronization upon template instantiation time. Just like hard coding, this enables the compiler to resolve the virtual calls and inline them.<br /><br />As you can see, templates can make a positive performace contribution by pushing computations out of the excution-time and into compile-time, enabling inling in the process.<br /><img src ="http://www.cppblog.com/zerolee/aggbug/15124.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2006-11-13 13:37 <a href="http://www.cppblog.com/zerolee/archive/2006/11/13/15124.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于vector的使用</title><link>http://www.cppblog.com/zerolee/archive/2006/10/14/13671.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Sat, 14 Oct 2006 07:05:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2006/10/14/13671.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/13671.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2006/10/14/13671.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/13671.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/13671.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Courier New" size="2">
						<font color="#ff1493">Item1: vector的使用</font>
						<br />
						<font color="#006400">JG问题</font>：<br />void f(vector&lt;int&gt;&amp; v) {<br />   v[0] = 1;   // A<br />   v[1] = 2;   // B<br />}<br /><font color="#006400">GURU问题</font>:</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 342px; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080"> 1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #000000">vector</span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000">&gt;</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> v;<br /></span>
								<span style="COLOR: #008080"> 2</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />v.reserve(</span>
								<span style="COLOR: #000000">2</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">);<br /></span>
								<span style="COLOR: #008080"> 3</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />assert(v.capacity() </span>
								<span style="COLOR: #000000">==</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">2</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">);<br /></span>
								<span style="COLOR: #008080"> 4</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />v[</span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">] </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">1</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080"> 5</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />v[</span>
								<span style="COLOR: #000000">1</span>
								<span style="COLOR: #000000">] </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">2</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080"> 6</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">for</span>
								<span style="COLOR: #000000"> (vector</span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000">&gt;</span>
								<span style="COLOR: #000000">::iterator i </span>
								<span style="COLOR: #000000">=</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> v.begin(); <br /></span>
								<span style="COLOR: #008080"> 7</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_144_170_Open_Image" onclick="this.style.display='none'; Codehighlighter1_144_170_Open_Text.style.display='none'; Codehighlighter1_144_170_Closed_Image.style.display='inline'; Codehighlighter1_144_170_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />       i </span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #000000"> v.end(); i</span>
								<span style="COLOR: #000000">++</span>
								<span style="COLOR: #000000">) </span>
								<span id="Codehighlighter1_144_170_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_144_170_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080"> 8</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    cout </span>
										<span style="COLOR: #000000">&lt;&lt;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">*</span>
										<span style="COLOR: #000000">i </span>
										<span style="COLOR: #000000">&lt;&lt;</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> endl;<br /></span>
										<span style="COLOR: #008080"> 9</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">10</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />cout </span>
								<span style="COLOR: #000000">&lt;&lt;</span>
								<span style="COLOR: #000000"> v[</span>
								<span style="COLOR: #000000">0</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">];<br /></span>
								<span style="COLOR: #008080">11</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />v.reserve(</span>
								<span style="COLOR: #000000">100</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">);<br /></span>
								<span style="COLOR: #008080">12</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />assert(v.capacity() </span>
								<span style="COLOR: #000000">==</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">100</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">);<br /></span>
								<span style="COLOR: #008080">13</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />cout </span>
								<span style="COLOR: #000000">&lt;&lt;</span>
								<span style="COLOR: #000000"> v[</span>
								<span style="COLOR: #000000">0</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">];<br /></span>
								<span style="COLOR: #008080">14</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />v[</span>
								<span style="COLOR: #000000">2</span>
								<span style="COLOR: #000000">] </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">  </span>
								<span style="COLOR: #000000">3</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">15</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />v[</span>
								<span style="COLOR: #000000">3</span>
								<span style="COLOR: #000000">] </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">4</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">16</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> ...</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">17</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">v[</span>
								<span style="COLOR: #000000">99</span>
								<span style="COLOR: #000000">] </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">100</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">18</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">for</span>
								<span style="COLOR: #000000"> (vector</span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000">&gt;</span>
								<span style="COLOR: #000000">::iterator i </span>
								<span style="COLOR: #000000">=</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> v.begin();<br /></span>
								<span style="COLOR: #008080">19</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_353_379_Open_Image" onclick="this.style.display='none'; Codehighlighter1_353_379_Open_Text.style.display='none'; Codehighlighter1_353_379_Closed_Image.style.display='inline'; Codehighlighter1_353_379_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />       i </span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #000000"> v.end(); i</span>
								<span style="COLOR: #000000">++</span>
								<span style="COLOR: #000000">) </span>
								<span id="Codehighlighter1_353_379_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_353_379_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">20</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    cout </span>
										<span style="COLOR: #000000">&lt;&lt;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">*</span>
										<span style="COLOR: #000000">i </span>
										<span style="COLOR: #000000">&lt;&lt;</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> endl;<br /></span>
										<span style="COLOR: #008080">21</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Courier New" size="2">对于以上两个问题，可能存在如下的建议：<br />1) JG问题中B与A的区别<br />   如果V非空，则A与B无任何区别，如果V为空，则B行将一定会抛出一个std::out_of_range异常，至于A的行为，标准并未加任何说明。<br />   原因在于：at()成员函数会对其进行越界检查，而operator[]操作符则不会，秉承于数组[]下标运算符也不会检查越界一样。<br /><br />2) GURU问题<br />代码：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 41px; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #000000">vector</span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000">&gt;</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> v;<br /></span>
								<span style="COLOR: #008080">2</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />v.reserve(</span>
								<span style="COLOR: #000000">2</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">);<br /></span>
								<span style="COLOR: #008080">3</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />assert(v.capacity() </span>
								<span style="COLOR: #000000">==</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">2</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">);<br /></span>
								<span style="COLOR: #008080">4</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
		</div>
		<font face="Courier New" size="2"> 1&gt; 断言可能会失败。reserve()函数的调用会保证v的容量至少为2，也可能大于2，而且容量是呈指数速度上升的。应该改为assert(v.capacity() &gt;= 2);<br /> 2&gt; 但是改进后的断言显的有点多余。因为标准已保证断言的内容，写上只会带来不必要的混乱，除非不相信标准。<br />代码：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #000000">v[</span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">] </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">1</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">2</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />v[</span>
								<span style="COLOR: #000000">1</span>
								<span style="COLOR: #000000">] </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">2</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">3</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
		</div>
		<font face="Courier New" size="2">  size ——&gt;resize, capacity ——&gt; reserve<br />1&gt; size告诉你容器中目前实际有多少元素，而对应的resize会在容器的尾部添加或删除一些元素，来调整容器当中的实际内容，使容器达到指定大小。对list、deque和vector适用，其他容器则不适用；<br />2&gt; capacity则告诉你最少添加多少元素才会导致容器重分配内存。而reserve在必要时总是使容器的内存缓冲区扩至一个更大的容量。仅对vector有用。<br />3&gt; 但我们实际上并未向v中添加任何元素，v仍然为空！我们只能使用at()或[]去改变那些确实存在于容器中的元素，与容器大小息息相关的。<br />因此v[0] = 1; v[1] = 2;在某些情况下会“顺利”执行，建议最好不要如此使用，可以使用resize()函数来替代reserve()函数。<br /><br />代码：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">for</span>
								<span style="COLOR: #000000"> (vector</span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000">&gt;</span>
								<span style="COLOR: #000000">::iterator i </span>
								<span style="COLOR: #000000">=</span>
						</font>
				</font>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #000000"> v.begin(); <br /></span>
								<span style="COLOR: #008080">2</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_68_94_Open_Image" onclick="this.style.display='none'; Codehighlighter1_68_94_Open_Text.style.display='none'; Codehighlighter1_68_94_Closed_Image.style.display='inline'; Codehighlighter1_68_94_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />       i </span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #000000"> v.end(); i</span>
								<span style="COLOR: #000000">++</span>
								<span style="COLOR: #000000">) </span>
								<span id="Codehighlighter1_68_94_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_68_94_Open_Text">
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000">{<br /></span>
										<span style="COLOR: #008080">3</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    cout </span>
										<span style="COLOR: #000000">&lt;&lt;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">*</span>
										<span style="COLOR: #000000">i </span>
										<span style="COLOR: #000000">&lt;&lt;</span>
								</font>
						</font>
						<font face="Courier New">
								<font size="2">
										<span style="COLOR: #000000"> endl;<br /></span>
										<span style="COLOR: #008080">4</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Courier New">
						<font size="2">
								<span style="COLOR: #008080">5</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
		</div>
		<p>
				<font face="Courier New" size="2">以上代码有诸如以下的建议：<br />1&gt; 尽量使用const.未修改v中的内容，可使用const_iterator;<br />2&gt; 尽量使用!=而不用&lt;来比较迭代器。 != 对任何迭代器都有效，而&lt; 只对随机访问迭代器有效。list不支持&lt;;<br />3&gt; 尽量使用++前缀，而非后缀。可以从++前后缀的实现代码中得知，除非一些特殊场合；<br />4&gt; 避免无谓的重复求值。v.end()函数在重复求值，而整个循环期间都未改变。除非end()被编译器进行内联，无调用开销，最好是将其提到循环外面；<br />5&gt; 尽量使用'\n'而非endl。原因在于endl会迫使输出流刷新其内部缓冲区，可以在整个循环之后写一个刷新语句。<br />6&gt; 尽量使用标准库中算法. 如copy()、for_each()。例如这里就可以这样写：<br />copy(v.begin(), v.end(), ostream_iterator&lt;int&gt;(cout, '\n'));<br />这样一来也避免了1&gt; ~ 5的麻烦。<br /></font>
		</p>
		<p>
				<font face="Courier New" size="2">// zero</font>
		</p>
<img src ="http://www.cppblog.com/zerolee/aggbug/13671.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2006-10-14 15:05 <a href="http://www.cppblog.com/zerolee/archive/2006/10/14/13671.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>修炼成C++高手必看的C++书单</title><link>http://www.cppblog.com/zerolee/archive/2006/09/12/12335.html</link><dc:creator>仄洛</dc:creator><author>仄洛</author><pubDate>Tue, 12 Sep 2006 05:11:00 GMT</pubDate><guid>http://www.cppblog.com/zerolee/archive/2006/09/12/12335.html</guid><wfw:comment>http://www.cppblog.com/zerolee/comments/12335.html</wfw:comment><comments>http://www.cppblog.com/zerolee/archive/2006/09/12/12335.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zerolee/comments/commentRss/12335.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zerolee/services/trackbacks/12335.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Courier New" size="2">
						<font color="#ff1493">
								<strong>增添于网上的一些书单：</strong>
						</font>
						<br />C++/OPP/OOD系列: <br /><strong><font color="#0000ff">层级一：语法/语意(C++)</font></strong><br />[Lippman2000] Essential C++ <br />Essential C++,by Stanley B. Lippman Addison Wesley Longman 2000,276 pages <br />Essential C++ 中文版 ，侯俊杰 译，282页 <br />Desc: 这本书概要性的介绍了C++核心的东西，但讲得较浅显，适合刚入门的人阅读。<br /></font>
		</p>
		<p>
				<font face="Courier New" size="2">[Andrew Koeing &amp; Barbara MOO] Accelerated C++<br />Accelerated c++, Andrew Koeing &amp; Barbara MOO, Addison Wesley, 2000<br />Desc: 这本书相当不错，讲解的C++编程的实践东西。也相当适合预巩固C++语法知识的人阅读。<br /></font>
				<font face="Courier New" size="2">
						<br />
				</font>
				<font face="Courier New" size="2">[Eckel2000] Thinking in C++ <br />Thinking in C++ 2/e Bruce Eckel 2000 1470 pages Prentice Hall <br />C++ 编程思想，刘宗田等 译，420页 </font>
		</p>
		<p>
				<font face="Courier New" size="2">[Lippman98] C++Primer <br />C++ Primer,3rd Editoin,by Stanley Lippman and Josee Lajoie <br />Addison Wesley Longman,1998 1237 pages <br />C++ Primer 中文版，侯俊杰 译，1999，1237页 </font>
		</p>
		<p>
				<font face="Courier New" size="2">[Struostrup2000] The C++ Programming Language <br />The C++ Programming Language,Special Editoin,by Bjarne Stroustrup <br />Addison Wesley Longman,2000,1017 pages <br /></font>
				<font face="Courier New" size="2">
						<br />[ANSI C++] C++规格书 1998.9.1 PDF格式 <br />ANSI C++ 1996 Draft </font>
		</p>
		<p>
				<br />
				<font face="Courier New" size="2">
						<font color="#0000ff">
								<strong>层级二：专家经验(C++/OOP)</strong>
						</font>
						<br />[Meyers96] More Effective C++ <br />More Effective C++, by Scott Meyers,Addison Wesley,1996,318pages <br />More Effective C++中文版，侯俊杰，培生 2000. 318页 </font>
		</p>
		<p>
				<font face="Courier New" size="2">[Meyers98] Effective C++ <br />Effective C++, Second Edition,by Scott Meyers,Addison Wesley Longman,1998.256pages <br />Effective C++ 2/e 中文版,侯俊杰,培生 2000.256页<br />Effective C++, Third Edition, by Scott Meyers, Addison Wesley Longman.</font>
		</p>
		<p>
				<font face="Courier New" size="2">[Sutter99] Exceptional C++ <br />Exceptional C++，by Herb Sutter,Addison Wesley Longman,2000.208pages <br />Exceptional C++中文版，侯俊杰,培生 2000.248页 </font>
		</p>
		<p>
				<font face="Courier New" size="2">[Sutter2001]More Exceptional C++ <br />More Exceptional C++ by Herb Sutter, Addison Wesley Longman, 2001. <br /><br />[Sutter2004]Exception C++ Style<br />Exception C++ Style by Herb Sutter, Addison Wesley Longman, 2004.</font>
		</p>
		<p>
				<font face="Courier New" size="2">
						<font color="#0000ff">
								<strong>层级三：底层机制(C++ Object Model)</strong>
						</font>
						<br />[Ellis90] The Annotated C++ Reference Manual <br />The Annotated C++ Reference Manual,by Margaret A.Ellis and Bjarne Stroustrup <br />Addison Wesley Longman,1990,447 pages. </font>
		</p>
		<p>
				<font face="Courier New" size="2">[Lippman96] Inside the C++ Object Model <br />Inside the C++ Object Model,by Stanley Lippman,Addison Wesley Longman,1996,280pages <br />深度探索C++物件模型，侯俊杰 译 <br /></font>
				<br />
				<font face="Courier New" size="2">
						<font color="#0000ff">
								<strong>层级四：设计观念的复用(C++/Patterns)</strong>
						</font>
						<br />[Gamma95] Design Patterns：Elements of Reusable Object Oriented Software, <br />by Erich Gamma,Richard Helm,Ralph Johnson,and John Vlissides,Addison Wesley,1995.395pages <br />设计模式,李英军等译,机械工业出版社,2000.254页 </font>
		</p>
		<p>
				<font face="Courier New" size="2">[Alex2001]Modern C++ Design: Generic Programming and Design Patterns Applied <br />by Andrei Alexandrescu,Addison-Wesley,2001,352Paper </font>
		</p>
		<p>
				<br />
				<font face="Courier New" size="2">
						<font color="#0000ff">
								<strong>Genericity/STL系列(与层级二同步）:</strong>
						</font>
						<br />第一个境界是使用STL: <br />[Josuttis99]:The C++ Standard Library －A Tutorial and Reference,by Nicolai M.Josuttis, <br />Addison Wesley 1999.799pages </font>
		</p>
		<p>
				<font face="Courier New" size="2">第二个境界是了解泛型技术的内涵与STL的学理: <br />[Austern98]:Generic Programming and the STL -Using and Extending the C++ Standard <br />Template library,by Matthew H.Austern,Addison Wesley 1998.548page </font>
		</p>
		<p>
				<br />
				<font size="2">
						<font face="Courier New">第三个境界是扩充STL: <br />[Stepanov2001]:C++ Standard Template Library by P.J.Plauger,Alexander A.Stepanov, <br />Meng Lee,David R.Musser,Prentice Hall 2001 <br /><br /><strong><font color="#ff1493">其他书目</font></strong>：<br />1. <strong>Large-scale C++ software Design</strong>, John Lako, Addison Wesley, 1996<br />2. <strong>Effective STL</strong>, Scott Meyers, Addison Wesley, 1995<br />3. <strong>C++ FAQs,</strong> 2nd, Marshall Cline, Greg Lomow, Mike Girou, Addison Wesley, 1998<br />4. <strong>C++ Gotchas</strong>, Stephen Dewhurst, Addison Wesley, 2002<br />5. <strong>C++ templates</strong>, the complete Guide, Daveed Vandevoorde &amp; Nicolar M.Josuttis, Addison Wesley, 2002<br />6. <strong>Standard C++ iostreams and Locals</strong>, Angelika Langer &amp; Klaus Kreft, Addison Wesley, 2000<br />7. <strong>Design &amp; Evolution of C++,</strong> BS, Addison Wesley, 1994<br />8. <strong>Modern C++ Design</strong>, Andrie Alexandrescu, Addison Wesley, 2001<br />9. <strong>Generative Programming</strong>, Krzysztof Czarnecki &amp; Ulrich Eisencecker, Addison Wesley, 2000<br />10.<strong>Pattern-oriented software architecture</strong>, Vol1:A system of patterns, Frank Buschmann, 1996<br />11. <strong>STL 源码剖析</strong>，侯杰<br />12. <strong>C++ Coding Standards 101 Rules Guidelines</strong>, Andrie Alexandrescu &amp; Herb Sutter, Addison Wesley, 2005<br /></font>
						<br />
				</font>
		</p>
<img src ="http://www.cppblog.com/zerolee/aggbug/12335.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zerolee/" target="_blank">仄洛</a> 2006-09-12 13:11 <a href="http://www.cppblog.com/zerolee/archive/2006/09/12/12335.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>