<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en" xml:base="http://fsfoundry.org/codefreak/wp-atom.php">
	<title type="text">COdE fr3@K</title>
	<subtitle type="text">Weblog of a lively geek.</subtitle>

	<updated>2009-07-02T04:07:39Z</updated>
	<generator uri="http://wordpress.org/" version="2.8">WordPress</generator>

	<link rel="alternate" type="text/html" href="http://fsfoundry.org/codefreak" />
	<id>http://fsfoundry.org/codefreak/feed/atom/</id>
	

			<link rel="license" type="text/html" href="http://creativecommons.org/licenses/by-nc-sa/2.5/" /><logo>http://creativecommons.org/images/public/somerights20.gif</logo><link rel="self" href="http://feeds.feedburner.com/codefreak" type="application/atom+xml" /><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Status Update, Mid 2009]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/ij4HMrHYKsQ/" />
		<id>http://fsfoundry.org/codefreak/?p=377</id>
		<updated>2009-06-28T02:27:45Z</updated>
		<published>2009-06-27T15:59:03Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[接下來幾個月, 打算把較多的閒暇時間用在一個 &#8220;低調計畫&#8221; 上. 因此, 預期將減少產出有意義的原創文字.
這個 project 當然是用 C++ 寫些東西. Scope 不大, 因此只要不擺爛, 一個人也一定寫得完. 簡單的說是要 re-implement 一個已存在並被不少人使用的 FOSS project. 將大量使用 Boost. 特別是已經一年多沒碰的 Asio, 以及很可惜一直沒玩到的 Multi-Index. 可以說是拿 Asio 來挑戰 libevent.
如果能較前人的貢獻有更好的 performance 當然最好. 即便不如期望, 也希望可以有機會找出不能 out-perform 的原因或線索. 或許能回饋給 Boost/Asio, 提供改善的方向. 無論如何, this is gonna be fun.
另, 如果你覺得閱讀小弟的文字不算是一件太浪費生命的事, 建議參觀/訂閱我的 Shared Items (via Google Reader).
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/06/27/status-update-mid-2009/">&lt;p&gt;接下來幾個月, 打算把較多的閒暇時間用在一個 &amp;#8220;低調計畫&amp;#8221; 上. 因此, 預期將減少產出有意義的原創文字.&lt;/p&gt;
&lt;p&gt;這個 project 當然是用 C++ 寫些東西. Scope 不大, 因此只要不擺爛, 一個人也一定寫得完. 簡單的說是要 re-implement 一個已存在並被不少人使用的 FOSS &lt;a href="http://www.danga.com/memcached/" rel="nofollow"&gt;project&lt;/a&gt;. 將大量使用 &lt;a href="http://boost.org" rel="nofollow"&gt;Boost&lt;/a&gt;. 特別是已經一年多沒碰的 &lt;a href="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio.html" rel="nofollow"&gt;Asio&lt;/a&gt;, 以及很可惜一直沒玩到的 &lt;a href="http://www.boost.org/doc/libs/1_39_0/libs/multi_index/doc/index.html" rel="nofollow"&gt;Multi-Index&lt;/a&gt;. 可以說是拿 Asio 來挑戰 &lt;a href="http://www.monkey.org/~provos/libevent/" rel="nofollow"&gt;libevent&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;如果能較前人的貢獻有更好的 performance 當然最好. 即便不如期望, 也希望可以有機會找出不能 out-perform 的原因或線索. 或許能回饋給 Boost/Asio, 提供改善的方向. 無論如何, this is gonna be fun.&lt;/p&gt;
&lt;p&gt;另, 如果你覺得閱讀小弟的文字不算是一件太浪費生命的事, 建議參觀/訂閱我的 &lt;a href="http://www.google.com/reader/shared/user/15909074279984661889/state/com.google/broadcast" rel="nofollow"&gt;Shared Items&lt;/a&gt; (via Google Reader).&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/?p=377#comments" title="Comments on &amp;quot;Status Update, Mid 2009&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?377" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=ij4HMrHYKsQ:jQWSMd3c-DI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=ij4HMrHYKsQ:jQWSMd3c-DI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/06/27/status-update-mid-2009/#comments" thr:count="3" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/06/27/status-update-mid-2009/feed/atom/" thr:count="3" />
		<thr:total>3</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/06/27/status-update-mid-2009/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[STL Performance Boost in VS2010]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/vh1UD_HgCgI/" />
		<id>http://fsfoundry.org/codefreak/?p=348</id>
		<updated>2009-06-29T07:54:06Z</updated>
		<published>2009-06-27T14:18:32Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="C++0x" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="rvalue-references" />		<summary type="html"><![CDATA[幾個月前曾在一篇文字裡寫道: 正確的運用 Rvalue References, 有可能帶來可觀的 performance gain. 前幾天看到 Visual C++ Team Blog 新貼出對 STL (VS2010 vs. VS2008) 的 performance benchmark. 在一個程度上, 該篇文字可以說驗證了這個不只我一個人獨有的看法與對 C++0x 的期待.
Rvalue-References 帶來的 performance boost 或許比不上 Multi-Core STL 來得那麼 &#8220;方便&#8221;, 但絕對是你不會想要錯過的 low hanging fruit.
C++0x 的巨浪就要來了, 你準備好了嗎?
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/06/27/stl-performance-boost-in-vs2010/">&lt;p&gt;幾個月前曾在一篇文字裡寫道: &lt;a href="http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/"&gt;正確的運用 Rvalue References, 有可能帶來可觀的 performance gain&lt;/a&gt;. 前幾天看到 Visual C++ Team Blog 新貼出對 STL (VS2010 vs. VS2008) 的 performance &lt;a href="http://blogs.msdn.com/vcblog/archive/2009/06/23/stl-performance.aspx" rel="nofollow"&gt;benchmark&lt;/a&gt;. 在一個程度上, 該篇文字可以說驗證了這個不只我一個人獨有的看法與對 &lt;a href="http://fsfoundry.org/codefreak/tag/cpp0x/"&gt;C++0x&lt;/a&gt; 的期待.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://fsfoundry.org/codefreak/tag/rvalue-references/"&gt;Rvalue-References&lt;/a&gt; 帶來的 performance boost 或許比不上 &lt;a href="http://fsfoundry.org/codefreak/2008/04/15/the-free-launch-is-back/"&gt;Multi-Core STL&lt;/a&gt; 來得那麼 &amp;#8220;方便&amp;#8221;, 但絕對是你不會想要錯過的 low hanging fruit.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://fsfoundry.org/codefreak/2008/10/29/incoming-strom-cpp0x/"&gt;C++0x 的巨浪就要來了&lt;/a&gt;, 你準備好了嗎?&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/?p=348#comments" title="Comments on &amp;quot;STL Performance Boost in VS2010&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?348" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=vh1UD_HgCgI:cMwMM3H6B1Q:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=vh1UD_HgCgI:cMwMM3H6B1Q:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/06/27/stl-performance-boost-in-vs2010/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/06/27/stl-performance-boost-in-vs2010/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/06/27/stl-performance-boost-in-vs2010/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Wordpress Installation Upgraded]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/OswE9y41DOc/" />
		<id>http://fsfoundry.org/codefreak/?p=357</id>
		<updated>2009-06-25T12:11:49Z</updated>
		<published>2009-06-25T12:08:37Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="FSfoundry" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[成功把 wordpress 從 2.3.x 升級到 2.8. 照著 官網上的說明 一步一步做, 過程不算輕鬆但也不困難.
或許是因為本來就很陽春, foundation theme 似乎完全沒有問題, 這下沒那麼急迫需要換 theme 了.
考慮換成 Pixel theme, 慢慢改裝中.
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/06/25/wordpress-installation-upgraded/">&lt;p&gt;成功把 wordpress 從 2.3.x 升級到 2.8. 照著 &lt;a href="http://codex.wordpress.org/Upgrading_WordPress_Extended"&gt;官網上的說明&lt;/a&gt; 一步一步做, 過程不算輕鬆但也不困難.&lt;/p&gt;
&lt;p&gt;或許是因為本來就很陽春, foundation theme 似乎完全沒有問題, 這下沒那麼急迫需要換 theme 了.&lt;/p&gt;
&lt;p&gt;考慮換成 &lt;a href="http://wordpress.org/extend/themes/pixel" rel="nofollow"&gt;Pixel theme&lt;/a&gt;, 慢慢改裝中.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/?p=357#comments" title="Comments on &amp;quot;Wordpress Installation Upgraded&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?357" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=OswE9y41DOc:7JEtPSQ371M:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=OswE9y41DOc:7JEtPSQ371M:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/06/25/wordpress-installation-upgraded/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/06/25/wordpress-installation-upgraded/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/06/25/wordpress-installation-upgraded/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Blog Pwnd, Again]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/8_QZhcxq2M4/" />
		<id>http://fsfoundry.org/codefreak/2009/06/24/blog-pwnd-again/</id>
		<updated>2009-06-24T10:43:22Z</updated>
		<published>2009-06-24T10:43:22Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="FSfoundry" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="security" />		<summary type="html"><![CDATA[又被 hack 了, (上一次的紀錄) 這次的狀況跟 這邊 說的一樣. 準備升級至 Wordpress 2.8.
一直用的 foundation theme 早已是個孤兒沒有人維護, 換哪個 theme 才好呢?
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/06/24/blog-pwnd-again/">&lt;p&gt;又被 hack 了, (&lt;a href="http://fsfoundry.org/codefreak/2008/04/06/knock-knock-you-ve-been-hacked/"&gt;上一次的紀錄&lt;/a&gt;) 這次的狀況跟 &lt;a href="http://www.simpleseo.cn/wordpress/wordpress251-hacked" rel="nofollow"&gt;這邊&lt;/a&gt; 說的一樣. 準備升級至 Wordpress 2.8.&lt;/p&gt;
&lt;p&gt;一直用的 foundation theme 早已是個孤兒沒有人維護, 換哪個 theme 才好呢?&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/06/24/blog-pwnd-again/#comments" title="Comments on &amp;quot;Blog Pwnd, Again&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?349" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=8_QZhcxq2M4:2Yl6uirjm94:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=8_QZhcxq2M4:2Yl6uirjm94:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/06/24/blog-pwnd-again/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/06/24/blog-pwnd-again/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/06/24/blog-pwnd-again/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[The &#8220;Zw&#8221; Prefix]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/ApAiyFppYQE/" />
		<id>http://fsfoundry.org/codefreak/2009/06/04/the-zw-prefix/</id>
		<updated>2009-06-05T03:02:07Z</updated>
		<published>2009-06-04T14:53:50Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[I&#8217;ve always wondered: What does the &#8220;Zw&#8221; prefix (of some NT kernel functions) mean?
As it turns out &#8211; nothing.
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/06/04/the-zw-prefix/">&lt;p&gt;I&amp;#8217;ve always wondered: What does the &amp;#8220;Zw&amp;#8221; prefix (of some NT kernel functions) mean?&lt;/p&gt;
&lt;p&gt;As it turns out &amp;#8211; &lt;a href="http://blogs.msdn.com/oldnewthing/archive/2009/06/03/9687937.aspx" rel="nofollow"&gt;nothing&lt;/a&gt;.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/06/04/the-zw-prefix/#comments" title="Comments on &amp;quot;The &amp;#8220;Zw&amp;#8221; Prefix&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?346" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=ApAiyFppYQE:zDmICmMHdiw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=ApAiyFppYQE:zDmICmMHdiw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/06/04/the-zw-prefix/#comments" thr:count="5" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/06/04/the-zw-prefix/feed/atom/" thr:count="5" />
		<thr:total>5</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/06/04/the-zw-prefix/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[It&#8217;s Better Without Asus]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/ePSjd-87aXA/" />
		<id>http://fsfoundry.org/codefreak/2009/05/30/its-better-without-asus/</id>
		<updated>2009-05-30T11:07:02Z</updated>
		<published>2009-05-30T11:05:43Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[Okay. Asus, this move is not cool.
Perhaps it&#8217;s better without you, altogether. Bye.
Cited from: The Register.
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/05/30/its-better-without-asus/">&lt;p&gt;Okay. Asus, this move is &lt;a href="http://www.itsbetterwithwindows.com/"&gt;not cool&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Perhaps it&amp;#8217;s better without you, altogether. Bye.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Cited from: &lt;a href="http://www.theregister.co.uk/2009/05/30/its_better_with_windows/" rel="nofollow"&gt;The Register&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/05/30/its-better-without-asus/#comments" title="Comments on &amp;quot;It&amp;#8217;s Better Without Asus&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?345" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=ePSjd-87aXA:YDcuF6WTjFw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=ePSjd-87aXA:YDcuF6WTjFw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/05/30/its-better-without-asus/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/05/30/its-better-without-asus/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/05/30/its-better-without-asus/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Observer Pattern, Done Differently]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/o8VVDxFvrsM/" />
		<id>http://fsfoundry.org/codefreak/2009/05/24/observer-done-differently/</id>
		<updated>2009-05-25T18:47:47Z</updated>
		<published>2009-05-24T12:43:59Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="boost" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="commentary" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[In a recent post of Scott Wheeler's - C and C++ are not the same language. He talked about differences between C and C++, and applications of different programming languages (C, C++, Java, Ruby) in his company.
In the post, Wheeler implemented observer pattern in C, C++ and Java, demonstrating some of his points - including [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/05/24/observer-done-differently/">&lt;p&gt;In a recent post of Scott Wheeler's - &lt;a href="http://blog.directededge.com/2009/05/21/c-and-c-are-not-the-same-language/"&gt;C and C++ are not the same language&lt;/a&gt;. He talked about differences between C and C++, and applications of different programming languages (C, C++, Java, Ruby) in his company.&lt;/p&gt;
&lt;p&gt;In the post, Wheeler implemented observer pattern in C, C++ and Java, demonstrating some of his points - including how C and C++ are different from each other, and how C++ and Java are actually more alike.&lt;/p&gt;
&lt;p&gt;Though I do agree with Wheeler's conclusion for the most part, I failed to share his view in C, C++ and Java comparison. IMHO, his observer implementation in C++ is rather old school, or should I say it's &lt;a href="http://fsfoundry.org/codefreak/2009/03/03/caffeine-intoxication/"&gt;so Java&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span id="more-342"&gt;&lt;/span&gt;In C++, oftentimes, there are more than one way to get you from point A to point B, while OO (inheritance) is the one feature that expresses coupling the most. In modern C++, when there is a way to do something without involving inheritance (read "tight coupling"), it is usually &lt;a href="http://fsfoundry.org/codefreak/2007/05/22/avoid-pointer-parameters-and-inheritance/"&gt;appreciated&lt;/a&gt;. One such way to implement the &lt;em&gt;subject&lt;/em&gt; in observer pattern is:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-1"&gt;Listing 1&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-4"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-4'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-4"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; subject&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt;:&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; register_observer&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; boost::&lt;span style="color: #00eeff;"&gt;function&lt;/span&gt;&amp;lt;void &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&amp;gt;&amp;amp; observer&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; observers_.&lt;span style="color: #00eeff;"&gt;connect&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;observer&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; notify_observers&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; observers_&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;private&lt;/span&gt;:&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; boost::&lt;span style="color: #00eeff;"&gt;signal&lt;/span&gt;&amp;lt;void &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&amp;gt; observers_;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;With help from Boost, nothing from &lt;em&gt;subject&lt;/em&gt;, (or the &lt;code&gt;Listener&lt;/code&gt; interface used in Wheeler's implementation) except the signature of expected &lt;em&gt;observers&lt;/em&gt;, are exposed to &lt;em&gt;Observers&lt;/em&gt; (actually, the signature is exposed mostly to programmers). They are decoupled from each other. Next, let's see what types of &lt;em&gt;observers&lt;/em&gt; can be utilized with this &lt;em&gt;subject&lt;/em&gt; implementation:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-2"&gt;Listing 2&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-5"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-5'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-5"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; foo_observer&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt;:&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; notify&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000dd;"&gt;cout&lt;/span&gt; &amp;lt;&amp;lt;__PRETTY_FUNCTION__ &amp;lt;&amp;lt;endl;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; bar_observer&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt;:&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; bar_observer&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;unsigned&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; id&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; : id_&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;id&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; operator&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000dd;"&gt;cout&lt;/span&gt; &amp;lt;&amp;lt;__PRETTY_FUNCTION__ &amp;lt;&amp;lt;&lt;span style="color: #666666;"&gt;'['&lt;/span&gt; &amp;lt;&amp;lt;id_ &amp;lt;&amp;lt;&lt;span style="color: #666666;"&gt;']'&lt;/span&gt; &amp;lt;&amp;lt;endl;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;private&lt;/span&gt;:&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;unsigned&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; id_;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; free_observer&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000dd;"&gt;cout&lt;/span&gt; &amp;lt;&amp;lt;__PRETTY_FUNCTION__ &amp;lt;&amp;lt;endl;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;And how are they put together:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-3"&gt;Listing 3&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-6"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-6'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-6"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; main&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; subject sub;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; foo_observer foo;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; bar_observer bar1&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000dd;color:#800000;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; sub.&lt;span style="color: #00eeff;"&gt;register_observer&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; boost::&lt;span style="color: #00eeff;"&gt;bind&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&amp;amp;foo_observer::&lt;span style="color: #00eeff;"&gt;notify&lt;/span&gt;, &amp;amp;foo&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; sub.&lt;span style="color: #00eeff;"&gt;register_observer&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;bar1&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; sub.&lt;span style="color: #00eeff;"&gt;register_observer&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;bar_observer&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000dd;color:#800000;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; sub.&lt;span style="color: #00eeff;"&gt;register_observer&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&amp;amp;free_observer&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; sub.&lt;span style="color: #00eeff;"&gt;notify_observers&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #0000dd;color:#800000;"&gt;0&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;This &lt;em&gt;subject&lt;/em&gt; implementation can be used with &lt;strong&gt;general objects&lt;/strong&gt; (e.g. &lt;code&gt;foo_observer&lt;/code&gt;), &lt;strong&gt;functional objects&lt;/strong&gt; (e.g. &lt;code&gt;bar_observer&lt;/code&gt;), and of course &lt;strong&gt;free functions&lt;/strong&gt; (e.g. &lt;code&gt;free_observer&lt;/code&gt;) and &lt;strong&gt;more&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Don't be mistaken, though his C++ observer implementation is not among the best I've seen, Wheeler's post is nevertheless a good read. Enjoy.&lt;/p&gt;
&lt;p&gt;Full source code: &lt;a href="http://codepad.org/5LvOwk88" rel="nofollow"&gt;observer.cpp&lt;/a&gt;&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/05/24/observer-done-differently/#comments" title="Comments on &amp;quot;Observer Pattern, Done Differently&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?342" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=o8VVDxFvrsM:mGQ-mRmTUbs:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=o8VVDxFvrsM:mGQ-mRmTUbs:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/05/24/observer-done-differently/#comments" thr:count="2" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/05/24/observer-done-differently/feed/atom/" thr:count="2" />
		<thr:total>2</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/05/24/observer-done-differently/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[My First JavaScript &#8211; Konami Code]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/8WMejRijJ28/" />
		<id>http://fsfoundry.org/codefreak/2009/05/24/my-first-javascript-konami-code/</id>
		<updated>2009-06-08T13:11:18Z</updated>
		<published>2009-05-24T07:50:29Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="javascript" /><category scheme="http://fsfoundry.org/codefreak" term="konami-code" />		<summary type="html"><![CDATA[上週在我的 feed reader 上看到了個好玩的東西 - Konami Code.
原文作者引用了 jQuery 主站上的一段 JavaScript, 也明白的說了這個 script 的一個缺陷 - 隨著愈多的 keypress, 它會愈跑愈慢:
It’s a bit sloppy, though: the kkeys array will increase in size with every keypress, which means the page will get slower and slower as you press keys.
把 coding style 調整成自己比較習慣的風格, 加上幾行處理過多 user key log 的 code 以修正原文作者所說的問題, 補上了注解. [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/05/24/my-first-javascript-konami-code/">&lt;p&gt;上週在我的 feed reader 上看到了個好玩的東西 - &lt;a href="http://www.nicollet.net/2009/05/konami-code/" rel="nofollow" rel="lightbox"&gt;Konami Code&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;原文作者引用了 jQuery 主站上的一段 JavaScript, 也明白的說了這個 script 的一個缺陷 - 隨著愈多的 keypress, 它會愈跑愈慢:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;It’s a bit sloppy, though: the kkeys array will increase in size with every keypress, which means the page will get slower and slower as you press keys.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;span id="more-339"&gt;&lt;/span&gt;把 coding style 調整成自己比較習慣的風格, 加上幾行處理過多 user key log 的 code 以修正原文作者所說的問題, 補上了注解. Here it is, 我的第一個 JavaScript 程式:&lt;/p&gt;
&lt;div id="js" /&gt;
&lt;ul&gt;
&lt;div class="igBar"&gt;&lt;span id="ljavascript-8"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('javascript-8'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;JavaScript:&lt;/span&gt;
&lt;div id="javascript-8"&gt;
&lt;div class="javascript"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;window.&lt;span style="color: #006600;"&gt;addEventListener&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #66cc66;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; target = &lt;span style="color: #3366CC;"&gt;"http://fsfoundry.org/codefreak/2009/05/24/my-first-javascript-konami-code/"&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; konami_code = &lt;span style="color: #3366CC;"&gt;"38,38,40,40,37,39,37,39,66,65"&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; user_key_log = &lt;span style="color: #66cc66;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#93;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; max_keys_to_log = &lt;span style="color: #CC0000;color:#800000;"&gt;10&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; window.&lt;span style="color: #006600;"&gt;addEventListener&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #3366CC;"&gt;"keydown"&lt;/span&gt;,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;ev&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #66cc66;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; user_key_log.&lt;span style="color: #006600;"&gt;push&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;ev.&lt;span style="color: #006600;"&gt;keyCode&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;; &lt;span style="color: #009900; font-style: italic;"&gt;// push_back&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; user_keys = user_key_log.&lt;span style="color: #006600;"&gt;toString&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900; font-style: italic;"&gt;// Searches for konami_code in user_keys.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #000066; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;user_keys.&lt;span style="color: #006600;"&gt;indexOf&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;konami_code&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;&amp;gt;= &lt;span style="color: #CC0000;color:#800000;"&gt;0&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; window.&lt;span style="color: #006600;"&gt;location&lt;/span&gt; = target;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900; font-style: italic;"&gt;// Cleans up user_key_log, if its length out&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900; font-style: italic;"&gt;// numbered max_keys_to_log.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #000066; font-weight: bold;"&gt;while&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;user_key_log.&lt;span style="color: #006600;"&gt;length&lt;/span&gt;&amp;gt; max_keys_to_log&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user_key_log.&lt;span style="color: #006600;"&gt;shift&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;; &lt;span style="color: #009900; font-style: italic;"&gt;// pop_front&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #66cc66;"&gt;&amp;#125;&lt;/span&gt;,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #66cc66;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #66cc66;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;只要你沒把 browser 的 JavaScript 關掉, 在 COdE fr3@K 的任何一頁輸入 &lt;a href="http://zh.wikipedia.org/wiki/%E7%A7%91%E4%B9%90%E7%BE%8E%E7%A7%98%E6%8A%80" rel="nofollow"&gt;上上下下左右左右BA&lt;/a&gt;, 就會把你帶回到這篇文字.&lt;/p&gt;
&lt;p&gt;Tested to work with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Firefox 3.0.10&lt;/li&gt;
&lt;li&gt;Opera 9.64&lt;/li&gt;
&lt;li&gt;Epiphany Web Browser 2.24.1&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note: I don't know why, but as the original script from jQuery, my version doesn't work with IE 6 on my XP box at office.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Something interesting that I can't help wondering, 連我這個從沒認真看過 JavaScript 更別說寫過的人都能在知道問題後稍微 google 一下把這問題修掉, 難道 jQuery 網站的作者是覺得這段 code 純屬搞笑而隨便寫寫嗎?&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/05/24/my-first-javascript-konami-code/#comments" title="Comments on &amp;quot;My First JavaScript &amp;#8211; Konami Code&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?339" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=8WMejRijJ28:UyIRggvvMVo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=8WMejRijJ28:UyIRggvvMVo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/05/24/my-first-javascript-konami-code/#comments" thr:count="2" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/05/24/my-first-javascript-konami-code/feed/atom/" thr:count="2" />
		<thr:total>2</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/05/24/my-first-javascript-konami-code/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[I Like to Move It]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/MlPvEf1wIgo/" />
		<id>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/</id>
		<updated>2009-05-24T15:01:01Z</updated>
		<published>2009-05-19T04:03:20Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="C++0x" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="rvalue-references" /><category scheme="http://fsfoundry.org/codefreak" term="video" />		<summary type="html"><![CDATA[喜愛 C++ 並關心 C++0x 標準的朋友一定會知道, C++0x 引進了許多新的元素. 不但對 Standard Library 有為數眾多的 addition 與 enhancement, 連 core language 都新增了不少東西. 而其中最重要的, 在我看來, 就是 Rvalue References. 即便不理會 C++0x 其他的新玩意 Rvalue References 依然是一個職業 C++ programmer 該要掌握的新 feature. 原因很簡單, 正確的運用 Rvalue References, 有可能帶來可觀的 performance gain.

The Stage
兩週前, 在 一篇文字 中提到了一個因為貪玩寫下的 object 字串化/格式化 utility. 並 預告 了將在另一篇文字討論, 在 C++0x 的標準下如何對這個 utility 進行優化. [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/">&lt;p&gt;喜愛 C++ 並關心 C++0x 標準的朋友一定會知道, C++0x 引進了許多新的元素. 不但對 Standard Library 有為數眾多的 addition 與 enhancement, 連 core language 都新增了不少東西. 而其中最重要的, 在我看來, 就是 &lt;a href="http://fsfoundry.org/codefreak/tag/rvalue-references/"&gt;Rvalue References&lt;/a&gt;. 即便不理會 C++0x 其他的新玩意 Rvalue References 依然是一個職業 C++ programmer 該要掌握的新 feature. 原因很簡單, 正確的運用 Rvalue References, 有可能帶來可觀的 performance gain.&lt;br /&gt;
&lt;span id="more-340"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;The Stage&lt;/h3&gt;
&lt;p&gt;兩週前, 在 &lt;a href="http://fsfoundry.org/codefreak/2009/05/04/ostream-type-preservation/"&gt;一篇文字&lt;/a&gt; 中提到了一個因為貪玩寫下的 &lt;a href="http://fsfoundry.org/tmp/str.cpp"&gt;object 字串化/格式化 utility&lt;/a&gt;. 並 &lt;a href="http://fsfoundry.org/codefreak/2009/05/04/ostream-type-preservation/#footnote-1-332"&gt;預告&lt;/a&gt; 了將在另一篇文字討論, 在 C++0x 的標準下如何對這個 utility 進行優化. Well, here it is. 先回頭看看原來的 code:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-1"&gt;Listing 1&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-13"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-13'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-13"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;class T&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;string str&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; T&amp;amp; x&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; boost::&lt;span style="color: #00eeff;"&gt;lexical_cast&lt;/span&gt;&amp;lt;string&amp;gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;x&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;class T&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;string operator%&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;string s, &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; T&amp;amp; x&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; s += str&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;x&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;這個 utility 包含了兩個 function template. 使用方法如下:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-2"&gt;Listing 2&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-14"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-14'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-14"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;string output = str&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000dd;color:#800000;"&gt;123&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; % &lt;span style="color: #666666;"&gt;' '&lt;/span&gt; % &lt;span style="color: #666666;"&gt;"abc"&lt;/span&gt;; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;簡單的說, 使用時需先以 function template &lt;strong&gt;&lt;code&gt;str&lt;/code&gt;&lt;/strong&gt; 觸發這字串化 utility, 接下來以另一個 function template - &lt;strong&gt;&lt;code&gt;operator%&lt;/code&gt;&lt;/strong&gt; - 串接繼續要被字串化的的 object.&lt;/p&gt;
&lt;h3&gt;The Problem&lt;/h3&gt;
&lt;p&gt;你知道簡單如 &lt;a href="#listing-2"&gt;Listing 2&lt;/a&gt; 的 use case 會觸發幾次 &lt;code&gt;string&lt;code&gt; 的建構 (與解構) 嗎? 假設每次 &lt;code&gt;lexical_cast&lt;/code&gt; 的 invocation 只會觸發一次 &lt;code&gt;string&lt;/code&gt; instantiation. 在 compiler 進行了 &lt;a href="http://www.cs.cmu.edu/~gilpin/c++/performance.html#returnvalue"&gt;RVO&lt;/a&gt; 之後, 仍有五次 &lt;code&gt;string&lt;/code&gt; instantiation.&lt;/p&gt;
&lt;p&gt;其中三次是建構從 &lt;code&gt;str/lexical_cast&lt;/code&gt; 傳回的 &lt;code&gt;string&lt;code&gt; object,&lt;sup&gt;&lt;a href="#footnote-1-340" id="footnote-link-1-340" class="footnote-link footnote-identifier-link" title="經過 RVO 之後, 已無法區分 str 與 lexical_cast 傳回的 instance. 兩者融為一體."&gt;1&lt;/a&gt;&lt;/sup&gt; 另外兩次是 &lt;code&gt;operator%&lt;/code&gt; 返回時的 copy construction. 前者可說是無中生有,&lt;sup&gt;&lt;a href="#footnote-2-340" id="footnote-link-2-340" class="footnote-link footnote-identifier-link" title="從沒有 string object 到有 string object."&gt;2&lt;/a&gt;&lt;/sup&gt; 是躲不掉的開銷. 而後者兩次的 construction 雖然一樣是閃不掉, 但在 C++0x 的 Rvalue References 加持下, 其 copy 的 overhead 是可以避開的.&lt;/p&gt;
&lt;h3&gt;The Move&lt;/h3&gt;
&lt;p&gt;C++0x 的 &lt;a href="http://fsfoundry.org/codefreak/tag/rvalue-references/"&gt;Rvalue References&lt;/a&gt; 可說是為了達成 move semantics 而誕生的產物. 這裡的 move, 是把一個 object 管理的 resource 轉移到另外一個 object 身上的動作. 而 move construction, 則是把一個 object 的 resource 讓出來, 以創建另一個 object:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-3"&gt;Listing 3&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-15"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-15'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-15"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #ff0000;"&gt;// a function that returns a temporary string.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;string unnamed_source&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; move_construction_demo&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #ff0000;"&gt;// move-construct a string using unnamed source.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; string target&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;unnamed_source&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; string named_source&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;...&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #ff0000;"&gt;// move-construct another string using named source;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; string another_target&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;move&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;named_source&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #ff0000;"&gt;// &amp;quot;named_source&amp;quot; is now destructible, but not usable.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #ff0000;"&gt;// Any reference to it may result undefined behavior.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;請參考在之前的文字介紹過的一個字串 class 的 &lt;a href="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/#listing-11"&gt;move constructor&lt;/a&gt; 以及 &lt;a href="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/#listing-13"&gt;&lt;code&gt;move&lt;/code&gt;&lt;/a&gt; function template 的 definition.&lt;/p&gt;
&lt;h3&gt;The Solution&lt;/h3&gt;
&lt;p&gt;有了 Rvalue References, 我們就可以把 &lt;a href="#listing-1"&gt;Listing 1&lt;/a&gt; 的 &lt;code&gt;operator%&lt;/code&gt; template 修改成:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-4"&gt;Listing 4&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-16"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-16'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-16"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;class T&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;string operator%&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;string s, &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; T&amp;amp; x&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; move&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;s += str&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;x&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;在這個例子中, 只要在回傳前對 &lt;code&gt;s += str(x)&lt;/code&gt; 的結果 - an lvalue (reference to a string) - 呼叫 &lt;code&gt;move&lt;/code&gt;, 就能把原本的 copy construction 變成 move construction. 當然也避開了 copy 的開銷.&lt;/p&gt;
&lt;p&gt;那為什麼不需要對另外一個 function template - &lt;code&gt;str&lt;/code&gt; - 一併做 move? 由於來源是 rvalue (&lt;code&gt;lexical_cast&lt;/code&gt; 的傳回值), 而 C++0x 的 &lt;code&gt;string&lt;/code&gt; class 也有 move constructor. 在這組合的配合下, &lt;strong&gt;move 會自動發生&lt;/strong&gt;. 即便 &lt;code&gt;string&lt;/code&gt; class 沒有 move ctor, 從 &lt;code&gt;lexical_cast&lt;/code&gt; 傳回的 &lt;code&gt;string&lt;/code&gt; instance 也會被一路 RVO 到 &lt;code&gt;str&lt;/code&gt; 的 caller,&lt;sup&gt;&lt;a href="#footnote-3-340" id="footnote-link-3-340" class="footnote-link footnote-identifier-link" title="至於這樣的 RVO 會不會真的發生, 就要看 compiler 優化的功力了."&gt;3&lt;/a&gt;&lt;/sup&gt; 其效果就跟被 move 了是十分接近的.&lt;/p&gt;
&lt;h3&gt;The Conclusion&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://fsfoundry.org/codefreak/2008/10/29/incoming-strom-cpp0x/"&gt;C++0x 就要來了&lt;/a&gt;. 你能做的最好準備, 就是讓自己熟悉 Rvalue References. 它不是個 fancy feature, 而是能夠實實在在主宰程式的效能的 killer feature.&lt;/p&gt;
&lt;p&gt;當 function 是以 return by value 方式傳回一個 named instance, 請記得: "We like to~~ MOVE IT!!!"&lt;/p&gt;
&lt;div style="text-align: center;"&gt;&lt;object width="425" height="344"&gt;&lt;param name="movie" value="http://www.youtube.com/v/0x3W6hutEj8&amp;#038;hl=zh_TW&amp;#038;fs=1&amp;#038;rel=0&amp;#038;color1=0x3a3a3a&amp;#038;color2=0x999999"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/0x3W6hutEj8&amp;#038;hl=zh_TW&amp;#038;fs=1&amp;#038;rel=0&amp;#038;color1=0x3a3a3a&amp;#038;color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/div&gt;
&lt;p&gt;Full source code: &lt;a href="http://fsfoundry.org/tmp/move-it.cpp"&gt;move-it.cpp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Compile using g&amp;#43;&amp;#43; 4.3.2: &lt;code&gt;$ g&amp;#43;&amp;#43; -std=c++0x move-it.cpp&lt;/code&gt;&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comments" title="Comments on &amp;quot;I Like to Move It&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?340" alt="Comments" /&gt;&lt;/a&gt;&lt;ol start="1" class="footnotes"&gt;&lt;li id="footnote-1-340" class="footnote"&gt;經過 RVO 之後, 已無法區分 &lt;code&gt;str&lt;/code&gt; 與 &lt;code&gt;lexical_cast&lt;/code&gt; 傳回的 instance. 兩者融為一體. [&lt;a href="#footnote-link-1-340" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-2-340" class="footnote"&gt;從沒有 &lt;code&gt;string&lt;/code&gt; object 到有 &lt;code&gt;string&lt;/code&gt; object. [&lt;a href="#footnote-link-2-340" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-3-340" class="footnote"&gt;至於這樣的 RVO 會不會真的發生, 就要看 compiler 優化的功力了. [&lt;a href="#footnote-link-3-340" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;/ol&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=MlPvEf1wIgo:NqWnqq-whuE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=MlPvEf1wIgo:NqWnqq-whuE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comments" thr:count="7" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/feed/atom/" thr:count="7" />
		<thr:total>7</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[New Job Openings at Work]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/iepAt21eR3o/" />
		<id>http://fsfoundry.org/codefreak/2009/05/07/new-job-openings-at-work/</id>
		<updated>2009-07-02T02:43:54Z</updated>
		<published>2009-05-07T10:34:17Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="TM" />		<summary type="html"><![CDATA[今天下午老闆的老闆跟我說, 他的手上也就是我現在服務的部門新增了二十個職缺, 並鼓勵我對外宣傳這個招兵買馬的訊息.
工作會是跟我同一個部門, 但應該不會是同一個 可能也有機會來我的 team. 細節還不清楚, 我就這個部門 common 的部份說明.
上班地點在台北市. 部門的主要責任是建置 cloud computing 的 infrastructure, 以及設計研發運行於這個 infrastructure 之上的 framework 與 application.
一定會用到的 hard skill 有:

Unix based. CentOS 5.3 is the choice of development and target platform of my team, choices among teams may vary.
System/network programming.
Proficiency at one or more of the following system programming languages; C, [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/05/07/new-job-openings-at-work/">&lt;p&gt;今天下午老闆的老闆跟我說, 他的手上也就是我現在服務的部門新增了二十個職缺, 並鼓勵我對外宣傳這個招兵買馬的訊息.&lt;/p&gt;
&lt;p&gt;工作會是跟我同一個部門, &lt;del datetime="2009-05-08T16:32:01+00:00"&gt;但應該不會是同一個&lt;/del&gt; &lt;ins datetime="2009-05-08T16:32:01+00:00"&gt;可能也有機會來我的&lt;/ins&gt; team. &lt;del datetime="2009-05-08T16:35:21+00:00"&gt;細節還不清楚,&lt;/del&gt; 我就這個部門 common 的部份說明.&lt;/p&gt;
&lt;p&gt;上班地點在台北市. 部門的主要責任是建置 cloud computing 的 infrastructure, 以及設計研發運行於這個 infrastructure 之上的 framework 與 application.&lt;/p&gt;
&lt;p&gt;一定會用到的 hard skill 有:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;big&gt;Unix based&lt;/big&gt;&lt;/strong&gt;. CentOS 5.3 is the choice of development and target platform of my team, choices among teams may vary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;big&gt;System/network programming&lt;/big&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Proficiency at one or more of the following system programming languages; &lt;strong&gt;&lt;big&gt;C, C++ and Java&lt;/big&gt;&lt;/strong&gt;. There are teams, like mine, which does C++ mostly, and there are other teams those do Java or pure C.&lt;/li&gt;
&lt;li&gt;Familarity with &lt;strong&gt;&lt;big&gt;Free and Open Source Software&lt;/big&gt;&lt;/strong&gt;. It would be foolish and arrogant if we were not to leverage existing and proven FOSS technologies when available. We use FOSS in conformance to their licenses.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;還有一定會加分, 多數人遲早要用到, 但不是每個人都得現在就會的技術:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Knowledge in &lt;strong&gt;&lt;big&gt;networking protocols&lt;/big&gt;&lt;/strong&gt;. General TCP/IP and HTTP in particular. The more the better.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;big&gt;Distributed computing&lt;/big&gt;&lt;/strong&gt;. e.g. distributed filesystem/database, MapReduce.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;big&gt;Relational database&lt;/big&gt;&lt;/strong&gt;. e.g. PostgreSQL, MySQL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;big&gt;Scripting&lt;/big&gt;&lt;/strong&gt;. Most teams, if not all, use scripts. Just in case if you are curious, my team uses Perl for scripting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;big&gt;Data mining&lt;/big&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Knowledge in &lt;strong&gt;&lt;big&gt;Information Security&lt;/big&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;不蓋你, 在個人這十年 software engineering 工作職涯裏, 就屬現在服務的公司最把軟體研發當一回事, 員工的福利也最好.&lt;/p&gt;
&lt;p&gt;有興趣, 熱愛挑戰的朋友可以把履歷 email 給我.&lt;sup&gt;&lt;a href="#footnote-1-336" id="footnote-link-1-336" class="footnote-link footnote-identifier-link" title="雖然小弟我沒有錄用與否的決定權, 但可以確保你的履歷被 forward 到正確的人手上, 免去被看不懂的人 filter 掉的風險."&gt;1&lt;/a&gt;&lt;/sup&gt; 也歡迎把這個訊息轉告給其他的朋友. 轉貼時最好以 link 的方式進行, 以方便取得更新資訊.&lt;/p&gt;
&lt;p id="email" /&gt;
連絡方式:&lt;/p&gt;
&lt;ul&gt;
&lt;pre&gt;
echo gsfbl.gtgpvoesz@psh | tr b-za.@ a-z@.
&lt;/pre&gt;
&lt;/ul&gt;
&lt;p&gt;If you can't figure out my email address from the above code, you really shouldn't be emailing me. Sorry.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/05/07/new-job-openings-at-work/#comments" title="Comments on &amp;quot;New Job Openings at Work&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?336" alt="Comments" /&gt;&lt;/a&gt;&lt;ol start="1" class="footnotes"&gt;&lt;li id="footnote-1-336" class="footnote"&gt;雖然小弟我沒有錄用與否的決定權, 但可以確保你的履歷被 forward 到正確的人手上, 免去被看不懂的人 filter 掉的風險. [&lt;a href="#footnote-link-1-336" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;/ol&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=iepAt21eR3o:2wqqjQuUNb0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=iepAt21eR3o:2wqqjQuUNb0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/05/07/new-job-openings-at-work/#comments" thr:count="8" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/05/07/new-job-openings-at-work/feed/atom/" thr:count="8" />
		<thr:total>8</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/05/07/new-job-openings-at-work/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Author of NoScript Apologized]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/wp0dxyZ9IfE/" />
		<id>http://fsfoundry.org/codefreak/2009/05/07/author-of-noscript-apologized/</id>
		<updated>2009-05-24T13:44:01Z</updated>
		<published>2009-05-07T03:38:56Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="firefox" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="security" />		<summary type="html"><![CDATA[Giorgio (author of NoScript) apologized and explained things from a somewhat different (i.e. his) perspective.
As Giorgio said himself, it was wrong and there is no excuse. Though, from a user's point of view, I do not agree with his doings in this instance. However, from a geek's (i.e. my) point of view, they were not [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/05/07/author-of-noscript-apologized/">&lt;p&gt;Giorgio (author of NoScript) &lt;a href="http://hackademix.net/2009/05/04/dear-adblock-plus-and-noscript-users-dear-mozilla-community/"&gt;apologized and explained&lt;/a&gt; things from a somewhat different (i.e. his) perspective.&lt;/p&gt;
&lt;p&gt;As Giorgio said himself, it was wrong and there is no excuse. Though, from a user's point of view, I do not agree with his doings in &lt;a href="http://fsfoundry.org/codefreak/2009/05/05/noscript-gone-bad/"&gt;this instance&lt;/a&gt;. However, from a geek's (i.e. my) point of view, they were not unthinkable.&lt;/p&gt;
&lt;p&gt;My take, Giorgio was hacking, not for the greater good obviously, but there was no intention to takeover the world (or helping bad guys doing so) neither. That is if it is his true thinking that he told us.&lt;/p&gt;
&lt;p&gt;For now, I am keeping NoScript.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/05/07/author-of-noscript-apologized/#comments" title="Comments on &amp;quot;Author of NoScript Apologized&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?335" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=wp0dxyZ9IfE:jj5KjyFs7J8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=wp0dxyZ9IfE:jj5KjyFs7J8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/05/07/author-of-noscript-apologized/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/05/07/author-of-noscript-apologized/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/05/07/author-of-noscript-apologized/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[NoScript Gone Bad!? WTF!]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/LrRSl-RZuQE/" />
		<id>http://fsfoundry.org/codefreak/2009/05/05/noscript-gone-bad/</id>
		<updated>2009-05-24T13:43:47Z</updated>
		<published>2009-05-04T16:55:38Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="firefox" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="security" />		<summary type="html"><![CDATA[Users of NoScirpt, read this (in Chinese) and this (in English)!
I am researching for alternatives to NoScript, if anyone has any idea, please drop me a line.
[Update]
Author of NoScript apologized.
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/05/05/noscript-gone-bad/">&lt;p&gt;Users of NoScirpt, read &lt;a href="http://antimalicious.blogspot.com/2009/05/noscript.html"&gt;this&lt;/a&gt; (in Chinese) and &lt;a href="http://adblockplus.org/blog/attention-noscript-users"&gt;this&lt;/a&gt; (in English)!&lt;/p&gt;
&lt;p&gt;I am researching for alternatives to NoScript, if anyone has any idea, please drop me a line.&lt;/p&gt;
&lt;p&gt;[Update]&lt;br /&gt;
Author of NoScript &lt;a href="http://fsfoundry.org/codefreak/2009/05/07/author-of-noscript-apologized/"&gt;apologized&lt;/a&gt;.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/05/05/noscript-gone-bad/#comments" title="Comments on &amp;quot;NoScript Gone Bad!? WTF!&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?333" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=LrRSl-RZuQE:L-IOCYHjiTo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=LrRSl-RZuQE:L-IOCYHjiTo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/05/05/noscript-gone-bad/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/05/05/noscript-gone-bad/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/05/05/noscript-gone-bad/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Output Stream Type Preservation]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/5puwiRfPfPY/" />
		<id>http://fsfoundry.org/codefreak/2009/05/04/type-preservation-on-iostream/</id>
		<updated>2009-05-19T04:37:29Z</updated>
		<published>2009-05-03T17:05:24Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="boost" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[上週看到了一篇引起我興趣的 blog - C++ Format String using boost::format. 冒著再次被人指為 屁的半死 的風險, 身為男子漢的我, 還是決定把心中的 替代解法 寫給了 Hubert 參考. Hubert 建議用 Boost.Format 的解法很好, 可我就是就是忍不住貪玩啊.
緣由交代完了. 有興趣看給 Hubert 參考的解法的朋友請自便, 因為接下來要談的不是它1, 而是另一個沒那麼漂亮, 但卻挺有趣的解法.


相信很多人都有這樣的需求 - 把 object 轉成 human readable string, 或許經過簡單的 formatting. 我一開始想到的就是:

Listing 1
PLAIN TEXT
C++:




// Assuming `foo` is an integer.


ostringstream oss;


oss &#60;&#60;"The value of foo is: " &#60;&#60;foo;


string output = [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/05/04/ostream-type-preservation/">&lt;p&gt;上週看到了一篇引起我興趣的 blog - &lt;a href="http://blog.hubert.tw/2009/01/03/c-format-string-using-boostformat/"&gt;C++ Format String using boost::format&lt;/a&gt;. 冒著再次被人指為 &lt;a href="http://fsfoundry.org/codefreak/2006/05/05/is-your-singleton-broken/#comment-5641"&gt;屁的半死&lt;/a&gt; 的風險, 身為男子漢的我, 還是決定把心中的 &lt;a href="http://fsfoundry.org/tmp/str.cpp"&gt;替代解法&lt;/a&gt; 寫給了 Hubert 參考. Hubert 建議用 &lt;a href="http://www.boost.org/doc/libs/1_38_0/libs/format/index.html"&gt;Boost.Format&lt;/a&gt; 的解法很好, 可我就是就是忍不住貪玩啊.&lt;/p&gt;
&lt;p&gt;緣由交代完了. 有興趣看給 Hubert 參考的解法的朋友請自便, 因為接下來要談的不是它&lt;sup&gt;&lt;a href="#footnote-1-332" id="footnote-link-1-332" class="footnote-link footnote-identifier-link" title=""&gt;1&lt;/a&gt;&lt;/sup&gt;, 而是另一個沒那麼漂亮, 但卻挺有趣的解法.&lt;br /&gt;
&lt;span id="more-332"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;hr /&gt;
&lt;p&gt;相信很多人都有這樣的需求 - 把 object 轉成 human readable string, 或許經過簡單的 formatting. 我一開始想到的就是:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-1"&gt;Listing 1&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-21"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-21'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-21"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #ff0000;"&gt;// Assuming `foo` is an integer.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;ostringstream oss;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;oss &amp;lt;&amp;lt;&lt;span style="color: #666666;"&gt;"The value of foo is: "&lt;/span&gt; &amp;lt;&amp;lt;foo;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;string output = oss.&lt;span style="color: #00eeff;"&gt;str&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;雖然這完全是能達到我的目的, 但也正如 Hubert 說的:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;總覺得透過 operator += 以及 stringstream 等東西，並沒有辦法像 sprintf 那樣直覺而優雅的將變數置換至字串當中。&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;那把對 &lt;code&gt;oss&lt;/code&gt; 做 insertion 的 code 跟對其呼叫 &lt;code&gt;str()&lt;/code&gt; member function 的 code 寫在一起會不會稍微優雅些? 把 &lt;a href="#listing-1"&gt;Listing 1&lt;/a&gt; 的三個 statement 縮減成兩個:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-2"&gt;Listing 2&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-22"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-22'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-22"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;ostringstream oss;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;string output =&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;oss &amp;lt;&amp;lt;&lt;span style="color: #666666;"&gt;"The value of foo is: "&lt;/span&gt; &amp;lt;&amp;lt;foo&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;.&lt;span style="color: #00eeff;"&gt;str&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;的確就稍微好那麼一些些 &gt;&lt;. 如果這段 code snip 可以 compile 的話.&lt;/p&gt;
&lt;p&gt;對 &lt;code&gt;ostringstream&lt;/code&gt; 的 instance (actually lvalue) 做 insertion 呼叫到的實際上是它的 base class - &lt;a href="http://www.dinkumware.com/manuals/?manual=compleat&amp;#038;page=ostream.html#operator%3C%3C"&gt;&lt;code&gt;ostream&lt;/code&gt; 的 inserter&lt;/a&gt;. 這些 inserter 的 return value 當然也是 &lt;code&gt;ostream&amp;#038;&lt;/code&gt;. 換句話說, 對 &lt;code&gt;ostream&lt;/code&gt; 的 derived class 的 instance 做 insertion, 在沒有特殊的 overload 的狀況下, return value 都不會是 derived class 的 reference. 也因此無法對這些 return value 呼叫 derived class 的特異功能; 例如 &lt;a href="http://www.dinkumware.com/manuals/?manual=compleat&amp;#038;page=sstream.html#basic_ostringstream::str"&gt;&lt;code&gt;ostringstream::str()&lt;/a&gt;&lt;/code&gt; - 把 insert 到 &lt;code&gt;ostringstream&lt;/code&gt; 的 buffer 的內容轉成 &lt;code&gt;string&lt;/code&gt; 傳回來.&lt;/p&gt;
&lt;p&gt;在我看來, &lt;a href="#listing-2"&gt;Listing 2&lt;/a&gt; 雖不合法, 但會是好用的語法, 可以把 code 寫的更乾淨利落. 於是我考慮了下面的 inserter template:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-3"&gt;Listing 3&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-23"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-23'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-23"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;class OutputStream, &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; T&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;OutputStream&amp;amp; operator&amp;lt;&amp;lt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;OutputStream&amp;amp; os, &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; T&amp;amp; x&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; ostream&amp;amp; base = os;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; base &amp;lt;&amp;lt;x;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; os;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;由於要能夠給各種繼承自 &lt;code&gt;ostream&lt;/code&gt; 的 output stream 使用, 因此第一個參數 - &lt;code&gt;OutputStream&lt;/code&gt; - 的 type 得是個 template parameter, 才能讓 compiler 認為這個 template 是比 base class (&lt;code&gt;ostream&lt;/code&gt;) 的 inserter 更好的選擇. 但也因此引發兩個使它無法被使用的 side effect:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;這個 template 會影響所有 user-defined type 的 &lt;code&gt;operator&amp;lt;&amp;lt;&lt;/code&gt;, 原本沒問題的 code 在有了這個 template 後可能無法 compile 或正常運作.&lt;/li&gt;
&lt;li&gt;當 template parameter &lt;code&gt;OutputStream&lt;/code&gt; 已是 &lt;code&gt;ostream&lt;/code&gt; 時, 這個 template inserter (inserter template 實例化所產生的實體) 會沒完沒了的 recursive 自己 call 自己, 會在 run time 發作直到把 stack 吃光為止.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;也考慮過換個 operator 來實現這樣的功能, 例如 &lt;code&gt;operator%&lt;/code&gt;, 但那依然無法解決上述的第一個 side effect, 只是換個 operator 出問題. 於是還是請出了 Boost:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-4"&gt;Listing 4&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-24"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-24'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-24"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;class OutputStream, &lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; T&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;typename&lt;/span&gt; boost::&lt;span style="color: #00eeff;"&gt;disable_if&lt;/span&gt;&amp;lt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; boost::&lt;span style="color: #00eeff;"&gt;mpl&lt;/span&gt;::&lt;span style="color: #00eeff;"&gt;or_&lt;/span&gt;&amp;lt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; boost::&lt;span style="color: #00eeff;"&gt;mpl&lt;/span&gt;::&lt;span style="color: #00eeff;"&gt;not_&lt;/span&gt;&amp;lt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; boost::&lt;span style="color: #00eeff;"&gt;is_base_of&lt;/span&gt;&amp;lt;ostream, OutputStream&amp;gt;&amp;gt;,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; boost::&lt;span style="color: #00eeff;"&gt;is_same&lt;/span&gt;&amp;lt;OutputStream, ostream&amp;gt;&amp;gt;,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; OutputStream&amp;gt;::&lt;span style="color: #00eeff;"&gt;type&lt;/span&gt;&amp;amp; operator&amp;lt;&amp;lt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; OutputStream&amp;amp; os, &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; T&amp;amp; x&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; ostream&amp;amp; base = os;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; base &amp;lt;&amp;lt;x;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; os;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Note: 由於不明原因, 我用的 code highlighter plugin 會把某些空格省略掉. 這通常不會造成明顯的問題, 結果依然是合法的 code. 但在 &lt;a href="#listing-4"&gt;Listing 4&lt;/a&gt; 的 line 5 and 6 的結尾, 這個 plugin 把應該是 &lt;code&gt;&amp;gt; &amp;gt;&lt;code&gt; (中間有 space) 的 code 顯示成在現行標準下不合法的連續兩個 &lt;code&gt;&amp;gt;&lt;/code&gt;. 請特別注意.&lt;/em&gt; &lt;/p&gt;
&lt;p&gt;宣告這個 inserter template 的 &lt;a href="#listing-4"&gt;Listing 4&lt;/a&gt; 可以這樣解讀; 不讓 compiler 考慮 (&lt;code&gt;boost::disable_if&lt;/code&gt;) 採用這個 template (同時也不會有具體實現這個 template 的問題), 如果下列某個條件成立 (&lt;code&gt;boost::or_&lt;/code&gt;):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;ostream&lt;/code&gt; 不是 (&lt;code&gt;boost::not_&lt;/code&gt;) &lt;code&gt;OutputStream&lt;/code&gt; 的 base class (&lt;code&gt;boost::is_base_of&lt;/code&gt;).&lt;sup&gt;&lt;a href="#footnote-2-332" id="footnote-link-2-332" class="footnote-link footnote-identifier-link" title="如果 boost::is_base_of 的兩個 template parameter 相同時 - e.g. boost::is_base_of&amp;lt;X, X&amp;gt; - result 為真."&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;OutputStream&lt;/code&gt; 就是 (&lt;code&gt;boost::is_same&lt;/code&gt;) &lt;code&gt;ostream&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;這段 code 既繞舌又不好寫. 連屁的半死的我也寫了超過十分鐘才寫對. 不過結果看起來不錯, 保留了output stream 做 insertion 之前的型別, 也在沒有明顯的 side effect 的情況之下讓類似 &lt;a href="#listing-2"&gt;Listing 2&lt;/a&gt; 的操作合法了.&lt;/p&gt;
&lt;p&gt;Full source code: &lt;a href="http://fsfoundry.org/tmp/ostream_type_preserve.cpp"&gt;ostream_type_preserve.cpp&lt;/a&gt;&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/05/04/type-preservation-on-iostream/#comments" title="Comments on &amp;quot;Output Stream Type Preservation&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?332" alt="Comments" /&gt;&lt;/a&gt;&lt;ol start="1" class="footnotes"&gt;&lt;li id="footnote-1-332" class="footnote"&gt;&lt;a href="&lt;a href="http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/"&gt;下一篇文字&lt;/a&gt; 才要進一步談寫給 Hubert 參考的解法, 以及在 C++0x 的標準下優化的方法. [&lt;a href="#footnote-link-1-332" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-2-332" class="footnote"&gt;如果 &lt;code&gt;boost::is_base_of&lt;/code&gt; 的兩個 template parameter 相同時 - e.g. &lt;code&gt;boost::is_base_of&amp;lt;X, X&amp;gt;&lt;/code&gt; - &lt;a href="http://www.boost.org/doc/libs/1_38_0/libs/type_traits/doc/html/boost_typetraits/reference/is_base_of.html"&gt;result 為真&lt;/a&gt;. [&lt;a href="#footnote-link-2-332" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;/ol&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=5puwiRfPfPY:TA-Qait231M:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=5puwiRfPfPY:TA-Qait231M:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/05/04/ostream-type-preservation/#comments" thr:count="3" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/05/04/ostream-type-preservation/feed/atom/" thr:count="3" />
		<thr:total>3</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/05/04/ostream-type-preservation/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Contemplation of Prejudice]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/ez6DanLeYlU/" />
		<id>http://fsfoundry.org/codefreak/2009/04/07/contemplation-of-a-prejudice/</id>
		<updated>2009-05-24T12:51:26Z</updated>
		<published>2009-04-07T15:09:06Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="boost" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="mortal" />		<summary type="html"><![CDATA[Not long ago, I blogged about how it was difficult to adopt Free libraries in my previous projects at work.
Just today, when I was exploring the compay repository, I see boost sits comfortably under a folder named "3rd_party" in one of my previous projects, along with a couple of tiny Free libraries I wrote.
Apparently, I [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/04/07/contemplation-of-prejudice/">&lt;p&gt;Not long ago, I blogged about how &lt;a href="http://fsfoundry.org/codefreak/2009/03/01/whats-up/#comment-4870"&gt;it was difficult to adopt Free libraries in my previous projects at work&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Just today, when I was exploring the compay repository, I see &lt;a href="http://boost.org"&gt;boost&lt;/a&gt; sits comfortably under a folder named "3rd_party" in one of my previous projects, along with a couple of tiny Free libraries I wrote.&lt;/p&gt;
&lt;p&gt;Apparently, I was wrong and didn't try hard enough. Going to find the man who made it happen and pay him my respect tomorrow.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/04/07/contemplation-of-a-prejudice/#comments" title="Comments on &amp;quot;Contemplation of Prejudice&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?331" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=ez6DanLeYlU:2j5gsabCdoY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=ez6DanLeYlU:2j5gsabCdoY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/04/07/contemplation-of-prejudice/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/04/07/contemplation-of-prejudice/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/04/07/contemplation-of-prejudice/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[DDJ Stopped Printing]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/dGO7Cdn9yMY/" />
		<id>http://fsfoundry.org/codefreak/2009/04/06/ddj-stopped-printing/</id>
		<updated>2009-04-06T13:55:22Z</updated>
		<published>2009-04-06T13:55:22Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="moody" />		<summary type="html"><![CDATA[今天感冒了, 掛病號在家裡睡覺. 一個朋友告訴我,  DDJ 停止了實體雜誌的發行.
雖然我不是每期必看 DDJ 的忠實讀者, 但這個消息依然讓我不開心了一天. 只是這事件對我來說怎麼也比不上三年前 CUJ 被併到 DDJ 的感傷.
安息吧, CUJ.
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/04/06/ddj-stopped-printing/">&lt;p&gt;今天感冒了, 掛病號在家裡睡覺. 一個朋友告訴我,  &lt;a href="http://subscribe.ddj.com/"&gt;DDJ 停止了實體雜誌的發行&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;雖然我不是每期必看 &lt;a href="http://www.ddj.com/"&gt;DDJ&lt;/a&gt; 的忠實讀者, 但這個消息依然讓我不開心了一天. 只是這事件對我來說怎麼也比不上三年前 &lt;a href="http://fsfoundry.org/codefreak/2006/04/13/cuj-merged-with-ddj/"&gt;CUJ 被併到 DDJ&lt;/a&gt; 的感傷.&lt;/p&gt;
&lt;p&gt;安息吧, &lt;a href="http://www.cuj.com/"&gt;CUJ&lt;/a&gt;.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/04/06/ddj-stopped-printing/#comments" title="Comments on &amp;quot;DDJ Stopped Printing&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?330" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=dGO7Cdn9yMY:LpQY30IBDQg:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=dGO7Cdn9yMY:LpQY30IBDQg:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/04/06/ddj-stopped-printing/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/04/06/ddj-stopped-printing/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/04/06/ddj-stopped-printing/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[[Note] Installing Review-Board on Ubuntu Linux 8.10]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/tephAA5P738/" />
		<id>http://fsfoundry.org/codefreak/2009/03/27/note-installing-review-board-on-ubuntu-linux-810/</id>
		<updated>2009-03-30T16:27:43Z</updated>
		<published>2009-03-27T15:32:12Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="foss" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[This is a note to myself just in case if I have to do it again - installing a Review Board server.
The installation process took me more than a few hours and spanned over three days. Here are the few things that caused me a grief. They are things I managed to remember and are [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/03/27/note-installing-review-board-on-ubuntu-linux-810/">&lt;p&gt;This is a note to myself just in case if I have to do it again - installing a &lt;a href="http://www.review-board.org/"&gt;Review Board&lt;/a&gt; server.&lt;/p&gt;
&lt;p&gt;The installation process took me more than a few hours and spanned over three days. Here are the few things that caused me a grief. They are things I managed to remember and are by no mean complete.&lt;br /&gt;
&lt;span id="more-328"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Read and follow the instructions &lt;a href="http://www.review-board.org/docs/manual/dev/admin/installation/linux/"&gt;here&lt;/a&gt; then &lt;a href="http://www.review-board.org/docs/manual/dev/admin/sites/creating-sites/"&gt;here&lt;/a&gt; very carefully. Don't miss a thing!&lt;/li&gt;
&lt;li&gt;Try satisfing Review Board's dependencies in advance might help. I installed all dependencies (those I could find) listed in its document before I begin installing it, i.e. &lt;code&gt;easy_install ReviewBoard&lt;/code&gt;. After all, my installation seems to be successful, at least for for the time being. Oh, BTW, its dependencies is scattered pretty much everywhere in the first link in item 1 above.&lt;/li&gt;
&lt;li&gt;Much of the installation process took place in terminal, i.e. text mode. Pay attention to the output message while building, installing and etc. I was lucky to notice an error when installing one of its dependencies, it was not obvious enough, for me anyway. This saved me from spending (only-god-knows-how-much) more time troubleshooting.&lt;/li&gt;
&lt;li&gt;On this server Review Board was installed, though I failed to recall which, there was one thing or two missing in the dependencies listing in its documentation. If things are not working, crawl through its log and figure out what's missing or miss-configured. It wasn't difficult to tell what was wrong in my case.&lt;/li&gt;
&lt;li&gt;Make sure you change the permissions for its "db" folder, i.e. &lt;code&gt;chown -R www-data /var/www/reviews.example.com/db&lt;/code&gt;, while you are &lt;a href="http://www.review-board.org/docs/manual/dev/admin/sites/creating-sites/#changing-permissions"&gt;changing the permissions for its "uploaded" folder&lt;/a&gt;. I had to do this for using sqlite, not sure if it is needed for using other DB backends.&lt;/li&gt;
&lt;li&gt;I had to remove the site configuration Review Board installed, and modify my &lt;code&gt;/etc/apache2/sites-enabled/000-default&lt;/code&gt; for things to work, in the contrary to what was suggested in its &lt;strong&gt;Note&lt;/strong&gt; section near the end of &lt;a href="http://www.review-board.org/docs/manual/dev/admin/sites/creating-sites/"&gt;this page&lt;/a&gt;. Note that, my server runs web applications in sub-folders (instead of &lt;em&gt;VirtualHost&lt;/em&gt;ing that is).&lt;/li&gt;
&lt;li&gt;If your server runs web applications in sub-folders like mine, there is a chance Review Board may break your applications. In my case, my MediaWiki installation was broken. For someone who is not proficient with Apache administration, e.g. &lt;a href="http://fsfoundry.org/codefreak/2008/05/25/i-m-a-lousy-sys-admin/"&gt;me&lt;/a&gt;, you will need a friend who is capable of, and willing to help you configuring Apache. Thanks &lt;a href="http://fsfoundry.org/codefreak/2006/10/12/best-wishes-to-newly-wedded-friends/"&gt;Tye&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/03/27/note-installing-review-board-on-ubuntu-linux-810/#comments" title="Comments on &amp;quot;[Note] Installing Review-Board on Ubuntu Linux 8.10&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?328" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=tephAA5P738:Eu6GfDMMqfo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=tephAA5P738:Eu6GfDMMqfo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/03/27/note-installing-review-board-on-ubuntu-linux-810/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/03/27/note-installing-review-board-on-ubuntu-linux-810/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/03/27/note-installing-review-board-on-ubuntu-linux-810/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Re: Domain-Specific Language (DSL)]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/ghG3cPAAFe8/" />
		<id>http://fsfoundry.org/codefreak/2009/03/26/re-domain-specific-language-dsl/</id>
		<updated>2009-04-06T14:16:42Z</updated>
		<published>2009-03-26T07:09:10Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="boost" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="C++0x" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="commentary" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[Keiko 寫了篇 討論 DSL 的文字. 這篇文字不短, 有層次. 有他說的 "學院派式" 的格調. 讓我有一種在看一份探討輪胎直徑與胎壓以及胎壁厚度之間關係的 paper 的 feel. 相形之下, 這裡寫的東西看起來更像是黑手示範如何補胎換胎的 howto.
好了, 細節不多談, 建議自己去參觀一下. 重點是, 本要在 comment 裏寫 code, 但這該死的 blogger 超難用, 讓我想起當年 從 blogger 離家出走 的原因.1 又扯遠了, 回到我想回應給 Keiko 的東西.


文字中一段談到 metaprogramming 可以把部份的 domain-specific 錯誤偵測從 run time 往前拉到 compile time. 文中以這樣的 interface 為例:

Listing 1
PLAIN TEXT
C++:




template &#60;int RowT, int ColT&#62;


class MyMatrix [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/03/26/re-domain-specific-language-dsl/">&lt;p&gt;Keiko 寫了篇 &lt;a href="http://keikoblog.blogspot.com/2009/03/domain-specific-language-dsl.html" rel="nofollow"&gt;討論 DSL 的文字&lt;/a&gt;. 這篇文字不短, 有層次. 有他說的 "學院派式" 的格調. 讓我有一種在看一份探討輪胎直徑與胎壓以及胎壁厚度之間關係的 paper 的 feel. 相形之下, 這裡寫的東西看起來更像是黑手示範如何補胎換胎的 howto.&lt;/p&gt;
&lt;p&gt;好了, 細節不多談, 建議自己去參觀一下. 重點是, 本要在 comment 裏寫 code, 但這該死的 blogger 超難用, 讓我想起當年 &lt;a href="http://fsfoundry.org/codefreak/about/"&gt;從 blogger 離家出走&lt;/a&gt; 的原因.&lt;sup&gt;&lt;a href="#footnote-1-327" id="footnote-link-1-327" class="footnote-link footnote-identifier-link" title="搬家的另一個主因是當年 blogger 沒有 tag/label."&gt;1&lt;/a&gt;&lt;/sup&gt; 又扯遠了, 回到我想回應給 Keiko 的東西.&lt;br /&gt;
&lt;span id="more-327"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;hr /&gt;
&lt;p&gt;文字中一段談到 metaprogramming 可以把部份的 domain-specific 錯誤偵測從 run time 往前拉到 compile time. 文中以這樣的 interface 為例:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-1"&gt;Listing 1&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-32"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-32'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-32"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;int RowT, &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; ColT&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; MyMatrix &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt;:&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #ff0000;"&gt;// !fr3@K! Detail omitted.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; MyMatrix&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #ff0000;"&gt;// !fr3@K! This explicit declaration is added to help &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #ff0000;"&gt;//&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;expressing my point.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;int RowT, &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; ColT&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;MyMatrix&amp;lt;RowT, ColT&amp;gt; operator +&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; MyMatrix&amp;lt;RowT, ColT&amp;gt;&amp;amp; lhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; MyMatrix&amp;lt;RowT, ColT&amp;gt;&amp;amp; rhs &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;在這樣的 use case 時:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-2"&gt;Listing 2&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-33"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-33'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-33"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;MyMatrix&amp;lt;&lt;span style="color: #0000dd;color:#800000;"&gt;10&lt;/span&gt;, &lt;span style="color: #0000dd;color:#800000;"&gt;10&lt;/span&gt;&amp;gt; m1;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;MyMatrix&amp;lt;&lt;span style="color: #0000dd;color:#800000;"&gt;2&lt;/span&gt;, &lt;span style="color: #0000dd;color:#800000;"&gt;2&lt;/span&gt;&amp;gt;&amp;nbsp; &amp;nbsp;m2;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000dd;"&gt;cout&lt;/span&gt; &amp;lt;&amp;lt;m1 + m2 &amp;lt;&amp;lt;endl; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;會產生下面的 compile time error diagnosis (in MSVC 2005):&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;
error C2679: binary '+' : no operator found which takes a right-hand operand of type 'MyMatrix' (or there is no acceptable conversion)
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;雖然 compiler 已經把錯誤抓到了, 可是錯誤訊息卻不是很友善. 你希望能有 "incompatible dimensions" (domain-specific error) 的提示. Okay, 讓我這個黑手示範一下在這個 use case 下, 怎麼樣叫 compiler 吐出 "incompatible dimensions". &lt;code&gt;operator+&lt;/code&gt; 的宣告要改成類似這樣:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-3"&gt;Listing 3&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-34"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-34'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-34"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; RowLhs, &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; ColLhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; RowRhs, &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; ColRhs&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;MyMatrix&amp;lt;RowLhs, ColLhs&amp;gt; operator +&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; MyMatrix&amp;lt;RowLhs, ColLhs&amp;gt;&amp;amp; lhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; MyMatrix&amp;lt;RowRhs, ColRhs&amp;gt;&amp;amp; rhs&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;通常, 好的 interface 會儘量設計成不允許錯誤的使用被 compile, 並且讓錯誤發生的時機愈早愈好. Keiko 舉的 use case 所引發的錯誤就是一個例子, 若用 Keiko 的 interface, 這個錯誤會發生在 &lt;code&gt;operator+&lt;/code&gt; 的 caller site, 也就是 &lt;a href="#listing-2"&gt;Listing 2&lt;/a&gt; 的 line 4. 但我為了讓 error message 能更為明確, 選擇反向操作 - 讓 &lt;code&gt;operator+&lt;/code&gt; 的 caller site &lt;strong&gt;能被 compile&lt;/strong&gt;, 將錯誤 defer 到 &lt;code&gt;operator+&lt;/code&gt; 的裏面發生. 實作為:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-4"&gt;Listing 4&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-35"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-35'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-35"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; RowLhs, &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; ColLhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; RowRhs, &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; ColRhs&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;MyMatrix&amp;lt;RowLhs, ColLhs&amp;gt; operator +&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; MyMatrix&amp;lt;RowLhs, ColLhs&amp;gt;&amp;amp; lhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; MyMatrix&amp;lt;RowRhs, ColRhs&amp;gt;&amp;amp; rhs&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; static_assert&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; RowLhs == RowRhs &amp;amp;&amp;amp; ColLhs == ColRhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #666666;"&gt;"incompatible dimensions"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; MyMatrix&amp;lt;RowLhs, ColLhs&amp;gt; tmp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; tmp += rhs;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;我手邊沒有 &lt;a href="http://fsfoundry.org/codefreak/tag/cpp0x/"&gt;C++0x&lt;/a&gt; 的 compiler, 無法確切告訴你 &lt;a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Static_assertions"&gt;&lt;code&gt;static_assert&lt;/code&gt;&lt;/a&gt; 實務上會吐出怎樣的錯誤訊息. 但沒意外的話, 應該會是 Keiko 想要的 message.&lt;sup&gt;&lt;a href="#footnote-2-327" id="footnote-link-2-327" class="footnote-link footnote-identifier-link" title="這裡 有個實例."&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;沒有 C++0x 的 compiler? 沒關係, 還有 &lt;a href="http://www.boost.org/doc/libs/1_38_0/doc/html/boost_staticassert.html"&gt;Boost.StaticAssert&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-5"&gt;Listing 5&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-36"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-36'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-36"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; RowLhs, &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; ColLhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; RowRhs, &lt;span style="color: #0000ff;"&gt;size_t&lt;/span&gt; ColRhs&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;MyMatrix&amp;lt;RowLhs, ColLhs&amp;gt; operator +&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; MyMatrix&amp;lt;RowLhs, ColLhs&amp;gt;&amp;amp; lhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; MyMatrix&amp;lt;RowRhs, ColRhs&amp;gt;&amp;amp; rhs&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; BOOST_STATIC_ASSERT&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;RowLhs == RowRhs &amp;amp;&amp;amp; ColLhs == ColRhs&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &amp;amp;&amp;amp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #666666;"&gt;"incompatible dimensions"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; MyMatrix&amp;lt;RowLhs, ColLhs&amp;gt; tmp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; tmp += rhs;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;雖然吐出的 error message 沒有包含 "incompatible dimensions", 但會 output 出包含 "boost::STATIC_ASSERTION_FAILURE" 的訊息, 並指出發生 static assertion failure 的是 line 4 of &lt;a href="#listing-2"&gt;Listing 2&lt;/a&gt; 所 instantiate 的 line 8 of &lt;a href="#listing-5"&gt;Listing 5&lt;/a&gt;. 只要 programmer 跑過去看就可以看到在 &lt;code&gt;BOOST_STATIC_ASSERT&lt;/code&gt; 這個 statement 中的 "incompatible dimensions" 字串. 有點麻煩, 有點陽春, but it still works, more or less.&lt;/p&gt;
&lt;p&gt;如果你跟我一樣覺得 Boost.StaticAssert 這樣的行為有點鳥, 可以試試另外一種 static assertion 的 implementation, 類似下面這樣:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-6"&gt;Listing 6&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-37"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-37'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-37"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;bool&amp;gt; &lt;span style="color: #0000ff;"&gt;struct&lt;/span&gt; my_static_assertion;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;template&lt;/span&gt; &amp;lt;&amp;gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style="color: #0000ff;"&gt;struct&lt;/span&gt; my_static_assertion&amp;lt;true&amp;gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #339900;"&gt;#define MY_STATIC_ASSERT(exp, message) \&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt; \&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; my_static_assertion&amp;lt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000dd;"&gt;exp&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; != &lt;span style="color: #0000dd;color:#800000;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&amp;gt; ERROR_&lt;span style="color: #339900;"&gt;##message; \&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;ERROR_&lt;span style="color: #339900;"&gt;##message; \&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;並把 &lt;a href="#listing-5"&gt;Listing 5&lt;/a&gt; 的第八行開始的 &lt;code&gt;BOOST_STATIC_ASSERT&lt;/code&gt; statement 換成:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-7"&gt;Listing 7&lt;/a&gt;&lt;/p&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-38"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-38'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-38"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;MY_STATIC_ASSERT&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; RowLhs == RowRhs &amp;amp;&amp;amp; ColLhs == ColRhs,&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; incompatible_dimensions&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;這時候的 error output 會像下面這樣 (using gcc):&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;
error: ‘ERROR_&lt;strong&gt;incompatible_dimensions&lt;/strong&gt;’ has incomplete type
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;這個 implementation 有一個很明顯的 limitation, 看 &lt;a href="#listing-6"&gt;實作&lt;/a&gt; 就會知道它要的 message 只能是一個字 (誤). No space, no symbols except underscore (底線/下滑線). 所以在 &lt;a href="#listing-7"&gt;Listing 7&lt;/a&gt; 的例子裏, 用的是 &lt;strong&gt;incompatible_dimensions&lt;/strong&gt;, 而不是更容易讀的 &lt;strong&gt;incompatible dimensions&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Have fun!&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/03/26/re-domain-specific-language-dsl/#comments" title="Comments on &amp;quot;Re: Domain-Specific Language (DSL)&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?327" alt="Comments" /&gt;&lt;/a&gt;&lt;ol start="1" class="footnotes"&gt;&lt;li id="footnote-1-327" class="footnote"&gt;搬家的另一個主因是當年 blogger 沒有 tag/label. [&lt;a href="#footnote-link-1-327" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-2-327" class="footnote"&gt;&lt;a href="http://www.cppprog.com/2009/0102/31_2.html" rel="nofollow"&gt;這裡&lt;/a&gt; 有個實例. [&lt;a href="#footnote-link-2-327" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;/ol&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=ghG3cPAAFe8:8EzQFpdoUZ0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=ghG3cPAAFe8:8EzQFpdoUZ0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/03/26/re-domain-specific-language-dsl/#comments" thr:count="1" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/03/26/re-domain-specific-language-dsl/feed/atom/" thr:count="1" />
		<thr:total>1</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/03/26/re-domain-specific-language-dsl/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Code Review &#8211; ActiveMQ-CPP]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/3QB08QaI5rs/" />
		<id>http://fsfoundry.org/codefreak/2009/03/08/code-review-activemq-cpp/</id>
		<updated>2009-03-10T12:15:22Z</updated>
		<published>2009-03-08T13:20:16Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="apache" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="code-review" /><category scheme="http://fsfoundry.org/codefreak" term="commentary" /><category scheme="http://fsfoundry.org/codefreak" term="exception" /><category scheme="http://fsfoundry.org/codefreak" term="foss" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[上一篇寫到 不識貨的我不欣賞 ActiveMQ-CPP "仿 Java" 的 design 哲學. 或許是我太機車了, 畢竟這是主觀的. 一個特定 design 哲學及其產物一定是有好也有壞, 評價的高低端看它的強項是否能被人欣賞, 缺點是否能被接受 (忍受). 今天讓我們來幫 ActiveMQ-CPP 做個 code review, 從 C++ coding 來看看能不能幫 Apache 扳回一城.

先從 ActiveMQ-CPP 的一個 example 看起. 下面是其中一個 class 的 dtor (destructor) 的 code snip (details omitted):

Listing 1
PLAIN TEXT
C++:




class HelloWorldProducer : public Runnable &#123;


public:


&#160; &#160; virtual ~HelloWorldProducer&#40;&#41;&#123;


&#160; &#160; &#160; &#160; cleanup&#40;&#41;;


&#160; [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/03/08/code-review-activemq-cpp/">&lt;p&gt;上一篇寫到 &lt;a href="http://fsfoundry.org/codefreak/2009/03/03/caffeine-intoxication/"&gt;不識貨的我不欣賞 ActiveMQ-CPP "仿 Java" 的 design 哲學&lt;/a&gt;. 或許是我太機車了, 畢竟這是主觀的. 一個特定 design 哲學及其產物一定是有好也有壞, 評價的高低端看它的強項是否能被人欣賞, 缺點是否能被接受 (忍受). 今天讓我們來幫 &lt;a href="http://activemq.apache.org/cms/index.html" rel="nofollow"&gt;ActiveMQ-CPP&lt;/a&gt; 做個 code review, 從 C++ coding 來看看能不能幫 &lt;a href="http://www.apache.org/" rel="nofollow"&gt;Apache&lt;/a&gt; 扳回一城.&lt;br /&gt;
&lt;span id="more-322"&gt;&lt;/span&gt;&lt;br /&gt;
先從 ActiveMQ-CPP 的一個 &lt;a href="http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/examples/main.cpp?revision=733921&amp;#038;view=markup" rel="nofollow"&gt;example&lt;/a&gt; 看起. 下面是其中一個 class 的 dtor (destructor) 的 code snip (details omitted):&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-1"&gt;Listing 1&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-44"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-44'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-44"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;class&lt;/span&gt; HelloWorldProducer : &lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; Runnable &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt;:&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;virtual&lt;/span&gt; ~HelloWorldProducer&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cleanup&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;private&lt;/span&gt;:&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; cleanup&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #ff0000;"&gt;// Destroy resources.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;try&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; destination != &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #0000dd;"&gt;delete&lt;/span&gt; destination;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;catch&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; CMSException&amp;amp; e &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt; e.&lt;span style="color: #00eeff;"&gt;printStackTrace&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; destination = &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;try&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; producer != &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #0000dd;"&gt;delete&lt;/span&gt; producer;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;catch&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; CMSException&amp;amp; e &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt; e.&lt;span style="color: #00eeff;"&gt;printStackTrace&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; producer = &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #ff0000;"&gt;// Close open resources.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;try&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; session != &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; session-&amp;gt;close&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; connection != &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; connection-&amp;gt;close&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;catch&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; CMSException&amp;amp; e &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt; e.&lt;span style="color: #00eeff;"&gt;printStackTrace&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;try&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; session != &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #0000dd;"&gt;delete&lt;/span&gt; session;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;catch&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; CMSException&amp;amp; e &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt; e.&lt;span style="color: #00eeff;"&gt;printStackTrace&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; session = &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;try&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; connection != &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #0000dd;"&gt;delete&lt;/span&gt; connection;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;catch&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; CMSException&amp;amp; e &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt; e.&lt;span style="color: #00eeff;"&gt;printStackTrace&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; connection = &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;&lt;a href="#listing-1"&gt;Listing 1&lt;/a&gt; 的 line 12, 17, 23, 28, 33 都在 &lt;code&gt;delete&lt;/code&gt; 之前先測試要被 &lt;code&gt;delete&lt;/code&gt; 的 pointer 是否為 NULL. 這其實是 &lt;a href="http://fsfoundry.org/codefreak/2007/09/16/a-couple-of-things-about-pointer-to-null/"&gt;完全沒有必要的&lt;/a&gt;. 至少在使用符合 Standard 規範的 Standard Library 實作或是符合 Standard 規範的 user-defined &lt;code&gt;operator delete&lt;/code&gt; 時沒有必要.&lt;/p&gt;
&lt;p&gt;接著可以注意到這些 &lt;code&gt;delete&lt;/code&gt; statement 都位於 &lt;code&gt;try&lt;/code&gt; block 裏面. 這比較奇怪, Apache 認可的 C++ contributor 還有人不知道當 object 在 destroy 的時候遇到錯誤, 寫個 log 甚至是打電話給你的阿姨都比在 dtor 裏面拋出 exception 來得好!&lt;sup&gt;&lt;a href="#footnote-1-322" id="footnote-link-1-322" class="footnote-link footnote-identifier-link" title="From [17.3] of C++ Faq Lite"&gt;1&lt;/a&gt;&lt;/sup&gt; 在這驚魂未定之際 trace 了幾個被 &lt;code&gt;delete&lt;/code&gt; 的 class (from derived to base classes) 的 dtor, 發現它們都是空的, 也就是說根本不會有 exception 發生的可能. 更別提不允許 dtor 拋出 exception 是 Standard 明白規定的.&lt;/p&gt;
&lt;p&gt;也許 ActiveMQ-CPP 的人不認同 Standard 或是其他 Guru  的意見, 寧願讓 dtor 可以拋出 exception. 而為了搞不清楚到底哪些會 throw, 就全部 &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; 起來. 在我沒看完所有的 dtor code 的狀況下, 我願意相信 Apache/ActiveMQ 的人不是出於對 C++ 的熟悉度不夠, 而是選擇這樣做的. 其實這個猜測已經與 &lt;code&gt;HelloWorldProducer&lt;/code&gt; 的 dtor 的思維背道而馳了, One allows exceptions to propagate out of dtors, the other catches and absorbs all exceptions in dtor. 撇開這些的有的沒的, &lt;code&gt;HelloWorldProducer&lt;/code&gt; 的 dtor 已是萬無一失了嗎? Well, not quite.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;HelloWorldProducer::cleanup()&lt;/code&gt; 的所有 &lt;code&gt;catch&lt;/code&gt; block 裏面都做了同樣一件事 - 把 exception 所經過的 stack trace 輸出到 &lt;code&gt;std::cerr&lt;/code&gt;, i.e. &lt;code&gt;e.printStackTrace()&lt;/code&gt;. Code snip from &lt;a href="http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/lang/Exception.cpp?revision=710096&amp;#038;view=markup" rel="nofollow"&gt;decaf/lang/Exception.cpp&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-2"&gt;Listing 2&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-45"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-45'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-45"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Exception::&lt;span style="color: #00eeff;"&gt;printStackTrace&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; printStackTrace&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; std::&lt;span style="color: #0000dd;"&gt;cerr&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #ff0000;"&gt;////////////////////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Exception::&lt;span style="color: #00eeff;"&gt;printStackTrace&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; std::&lt;span style="color: #00eeff;"&gt;ostream&lt;/span&gt;&amp;amp; stream &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; stream &amp;lt;&amp;lt;getStackTraceString&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #ff0000;"&gt;////////////////////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;std::&lt;span style="color: #00eeff;"&gt;string&lt;/span&gt; Exception::&lt;span style="color: #00eeff;"&gt;getStackTraceString&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #ff0000;"&gt;// Create the output stream.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; std::&lt;span style="color: #00eeff;"&gt;ostringstream&lt;/span&gt; stream;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #ff0000;"&gt;// Write the message and each stack entry.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; stream &amp;lt;&amp;lt;message &amp;lt;&amp;lt;std::&lt;span style="color: #00eeff;"&gt;endl&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;unsigned&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; ix=&lt;span style="color: #0000dd;color:#800000;"&gt;0&lt;/span&gt;; ix&amp;lt;stackTrace.&lt;span style="color: #00eeff;"&gt;size&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; ++ix &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; stream &amp;lt;&amp;lt;&lt;span style="color: #666666;"&gt;"&lt;span style="color: #666666; font-weight: bold;"&gt;\t&lt;/span&gt;FILE: "&lt;/span&gt; &amp;lt;&amp;lt;stackTrace&lt;span style="color: #000000;"&gt;&amp;#91;&lt;/span&gt;ix&lt;span style="color: #000000;"&gt;&amp;#93;&lt;/span&gt;.&lt;span style="color: #00eeff;"&gt;first&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; stream &amp;lt;&amp;lt;&lt;span style="color: #666666;"&gt;", LINE: "&lt;/span&gt; &amp;lt;&amp;lt;stackTrace&lt;span style="color: #000000;"&gt;&amp;#91;&lt;/span&gt;ix&lt;span style="color: #000000;"&gt;&amp;#93;&lt;/span&gt;.&lt;span style="color: #00eeff;"&gt;second&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; stream &amp;lt;&amp;lt;std::&lt;span style="color: #00eeff;"&gt;endl&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #ff0000;"&gt;// Return the string from the output stream.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; stream.&lt;span style="color: #00eeff;"&gt;str&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;Line 2 and 7, 在沒有檢查 &lt;a href="http://www.dinkumware.com/manuals/?manual=compleat&amp;#038;page=ios.html#basic_ios::exceptions" rel="nofollow"&gt;exception mask&lt;/a&gt; 的前提下, 對 &lt;code&gt;std::cerr&lt;/code&gt; 做 insertion 有可能拋出 exception. 雖然 default 是不會, i.e. &lt;code&gt;basic_ios::exceptions()&lt;/code&gt; 為 &lt;a href="http://www.dinkumware.com/manuals/?manual=compleat&amp;#038;page=ios.html#ios_base::iostate" rel="nofollow"&gt;&lt;code&gt;ios_base::goodbit&lt;/code&gt;&lt;/a&gt;, 但 &lt;code&gt;std::cerr&lt;/code&gt; 為一 global variable, 誰能保證它的 exception mask 不會被 client code (甚至是 ActiveMQ-CPP 的其他部份) 改變?&lt;/p&gt;
&lt;p&gt;說到 IOStream default 不會 throw, 因此在 &lt;code&gt;Exception::getStackTraceString()&lt;/code&gt; 內對一個 local 的 &lt;code&gt;std::ostringstream&lt;/code&gt; instance (&lt;code&gt;stream&lt;/code&gt;) 做 insertion 不會 throw. 但是 &lt;code&gt;stream.str()&lt;/code&gt; (&lt;a href="http://www.dinkumware.com/manuals/?manual=compleat&amp;#038;page=sstream.html#basic_ostringstream::str" rel="nofollow"&gt;&lt;code&gt;basic_ostringstream::str()&lt;/code&gt;&lt;/a&gt;) - 由於以 return by value 的方式回傳一個 &lt;code&gt;std::string&lt;/code&gt; - 卻有可能 throw.&lt;sup&gt;&lt;a href="#footnote-2-322" id="footnote-link-2-322" class="footnote-link footnote-identifier-link" title="Exception::getStackTraceString() 也是 return by value, 因此也一樣可能 throw."&gt;2&lt;/a&gt;&lt;/sup&gt; 結果就是 &lt;code&gt;HelloWorldProducer&lt;/code&gt; 的 dtor 不但沒有成功地將所有可能的 exception 攔住, 反而增加了在 &lt;code&gt;catch&lt;/code&gt; block 內發生沒被處理的 exception 的可能. 而比在 dtor 內拋出 exception 更糟糕的是 - 這狀況會讓程式立即 &lt;a href="http://www.dinkumware.com/manuals/?manual=compleat&amp;#038;Search=terminate&amp;#038;page=exceptio.html#terminate" rel="nofollow"&gt;terminate&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;ActiveMQ 是實作來給 enterprise grade application 使用, 做為做 message transport/routing, 該要 &lt;strong&gt;"非常非常 robust"&lt;/strong&gt; 才對. 上面談到的問題點畢竟都不是源自 production code, 或許不該被拿來太認真檢驗, 只能說它是很糟糕的 sample code. 可是往更深入的地方繼續 hack, 看到的卻是整個 ActiveMQ-CPP 的 exception handling 都不及格.&lt;/p&gt;
&lt;p&gt;隨便選個檔案挑個 function, &lt;a href="http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/core/ActiveMQConnection.cpp?revision=749440&amp;#038;view=markup" rel="nofollow"&gt;activemq/core/ActiveMQConnection.cpp&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-3"&gt;Listing 3&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-46"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-46'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-46"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;ActiveMQConnection::~ActiveMQConnection&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;try&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this-&amp;gt;close&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; AMQ_CATCH_NOTHROW&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; ActiveMQException &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; AMQ_CATCHALL_NOTHROW&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Note: 挑 &lt;code&gt;ActiveMQConnection&lt;/code&gt; 的 dtor 來說明是因為它短, 與它是個 dtor 無關.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;其中 &lt;code&gt;AMQ_CATCH_NOTHROW&lt;/code&gt; 的定義如下 (in &lt;a href="http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/exceptions/ExceptionDefines.h?revision=593614&amp;#038;view=markup" rel="nofollow"&gt;activemq/exceptions/ExceptionDefines.h&lt;/a&gt;):&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-4"&gt;Listing 4&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-47"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-47'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-47"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #339900;"&gt;#define AMQ_CATCH_NOTHROW( type ) \&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;catch&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; type&amp;amp; ex &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt; \&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ex.&lt;span style="color: #00eeff;"&gt;setMark&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; __FILE__, __LINE__ &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;; \&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;而 &lt;code&gt;ActiveMQConnection::setMark()&lt;/code&gt; (&lt;a href="http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/exceptions/ActiveMQException.h?revision=688327&amp;#038;view=markup" rel="nofollow"&gt;source&lt;/a&gt;) 為一 proxy function, 轉 call 其 base class - &lt;code&gt;decaf::lang::Exception&lt;/code&gt; (&lt;a href="http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/lang/Exception.cpp?revision=710096&amp;#038;view=markup" rel="nofollow"&gt;source&lt;/a&gt;) 的 &lt;code&gt;setMark()&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;div id="listing-5"&gt;Listing 5&lt;/div&gt;
&lt;div class="igBar"&gt;&lt;span id="lcpp-48"&gt;&lt;a href="#" onclick="javascript:showPlainTxt('cpp-48'); return false;"&gt;PLAIN TEXT&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="syntax_hilite"&gt;&lt;span class="langName"&gt;C++:&lt;/span&gt;
&lt;div id="cpp-48"&gt;
&lt;div class="cpp"&gt;
&lt;ol&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; Exception::&lt;span style="color: #00eeff;"&gt;setMark&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;char&lt;/span&gt;* &lt;span style="color: #0000ff;"&gt;file&lt;/span&gt;, &lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; lineNumber &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#123;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #ff0000;"&gt;// Add this mark to the end of the stack trace.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; stackTrace.&lt;span style="color: #00eeff;"&gt;push_back&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; std::&lt;span style="color: #00eeff;"&gt;make_pair&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;std::&lt;span style="color: #00eeff;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;file&lt;/span&gt;, &lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;lineNumber &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; std::&lt;span style="color: #00eeff;"&gt;ostringstream&lt;/span&gt; stream;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; stream &amp;lt;&amp;lt;&lt;span style="color: #666666;"&gt;"&lt;span style="color: #666666; font-weight: bold;"&gt;\t&lt;/span&gt;FILE: "&lt;/span&gt; &amp;lt;&amp;lt;stackTrace&lt;span style="color: #000000;"&gt;&amp;#91;&lt;/span&gt;stackTrace.&lt;span style="color: #00eeff;"&gt;size&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;-&lt;span style="color: #0000dd;color:#800000;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#93;&lt;/span&gt;.&lt;span style="color: #00eeff;"&gt;first&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; stream &amp;lt;&amp;lt;&lt;span style="color: #666666;"&gt;", LINE: "&lt;/span&gt; &amp;lt;&amp;lt;stackTrace&lt;span style="color: #000000;"&gt;&amp;#91;&lt;/span&gt;stackTrace.&lt;span style="color: #00eeff;"&gt;size&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#41;&lt;/span&gt;-&lt;span style="color: #0000dd;color:#800000;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;#93;&lt;/span&gt;.&lt;span style="color: #00eeff;"&gt;second&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #ff0000;"&gt;//decaf::util::logger::SimpleLogger logger(&amp;quot;com.yadda2&amp;quot;);&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: #ff0000;"&gt;//logger.log( stream.str() );&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="font-weight: bold;color:#26536A;"&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"&gt;&lt;span style="color: #000000;"&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/ul&gt;
&lt;p&gt;不讓人意外, 光看 &lt;a href="#listing-5"&gt;listing 5&lt;/a&gt; 的第四行就可以猜到 &lt;code&gt;Exception::stackTrace&lt;/code&gt; 的型別應該是 &lt;code&gt;std::vector&lt; std::pair&lt; std::string, int&gt; &gt;&lt;/code&gt;. 而單是這一行 code, 就 (至少) 有四個地方會產生 exception. (你知道是哪四個嗎?) 看到這裡, 相信你已經知道這個 function 在 &lt;code&gt;catch&lt;/code&gt; block 內被呼叫, 若引發了沒有被處理的 exception 的後果 - terminate. 而這樣的 code, 在 ActiveMQ-CPP 裏面到處都是.&lt;/p&gt;
&lt;p&gt;ActiveMQ 本身 (由於是以 Java 寫的) 是否不受這篇談的 exception handling 缺失的影響? 雖然 Java 我是門外漢, 但就我所知 C++ 與 Java 的 exception handling model 是很接近的. 因此, 若 hack 進去可能也會發現類似的問題. 那 ActiveMQ-CPP 能用嗎? 除了前面提到的問題之外, 另外還有注意到模組化, 同步最佳化等的 non-critical 問題, 或許我會另外寫一篇文字加以討論.&lt;sup&gt;&lt;a href="#footnote-3-322" id="footnote-link-3-322" class="footnote-link footnote-identifier-link" title="其實 Listing 5 就有無用的廢碼."&gt;3&lt;/a&gt;&lt;/sup&gt; 對 &lt;a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/jms/package-summary.html"&gt;JMS&lt;/a&gt;/ActiveMQ 不熟悉的我, 要用看的抓出 ActiveMQ-CPP 非語言問題相關的其他問題可說是力有未逮 (at least at the time of writing). 無論如何, 一個打著 Apache 招牌的 project, 我相信它起碼在&lt;strong&gt;理想狀態&lt;/strong&gt;下是能正確運作的, 至少大多數時候.&lt;/p&gt;
&lt;p&gt;由於工作上需要使用 ActiveMQ/ActiveMQ-CPP, 希望能在這一兩個星期之內準備個 patch (maybe a few patches) 送給 ActiveMQ-CPP. Java (ActiveMQ) 的部份就要靠另外的善心人士幫幫忙看看有沒有問題.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/03/08/code-review-activemq-cpp/#comments" title="Comments on &amp;quot;Code Review &amp;#8211; ActiveMQ-CPP&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?322" alt="Comments" /&gt;&lt;/a&gt;&lt;ol start="1" class="footnotes"&gt;&lt;li id="footnote-1-322" class="footnote"&gt;From &lt;a href="http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3" rel="nofollow"&gt;[17.3]&lt;/a&gt; of &lt;a href="http://www.parashift.com/c++-faq-lite/index.html" rel="nofollow"&gt;C++ Faq Lite&lt;/a&gt; [&lt;a href="#footnote-link-1-322" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-2-322" class="footnote"&gt;&lt;code&gt;Exception::getStackTraceString()&lt;/code&gt; 也是 return by value, 因此也一樣可能 throw. [&lt;a href="#footnote-link-2-322" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-3-322" class="footnote"&gt;其實 &lt;a href="#listing-5"&gt;Listing 5&lt;/a&gt; 就有無用的廢碼. [&lt;a href="#footnote-link-3-322" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;/ol&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=3QB08QaI5rs:hvKYAQUq7FA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=3QB08QaI5rs:hvKYAQUq7FA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/03/08/code-review-activemq-cpp/#comments" thr:count="2" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/03/08/code-review-activemq-cpp/feed/atom/" thr:count="2" />
		<thr:total>2</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/03/08/code-review-activemq-cpp/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[咖啡因中毒]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/PxrIXsu3aAM/" />
		<id>http://fsfoundry.org/codefreak/2009/03/03/caffeine-intoxication/</id>
		<updated>2009-03-09T02:53:39Z</updated>
		<published>2009-03-03T01:10:46Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="apache" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="commentary" /><category scheme="http://fsfoundry.org/codefreak" term="foss" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="java" />		<summary type="html"><![CDATA[由於工作需要, 這幾天花了點時間看了 ActiveMQ-CPP (from Apache) 的文件與 source code. 第一印象是 - 這票 Apache 的人會不會太喜歡 Java 了?

與 Apache ActiveMQ (core functions in Java) 一樣, ActiveMQ-CPP (client library in C++) 都是拿 JMS (Java Message Service) 來當範本重新實作. ActiveMQ 的部份我沒看進去就不多談了, 只知道是它是個 JMS message router 的實作. ActiveMQ-CPP 就有趣了, 它是 JMS protocol compatible, i.e. 可以與不同 JMS message router/client 的實作互通, 這很好也很正常. 可是看到它仿造 (modeled [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/03/03/caffeine-intoxication/">&lt;p&gt;由於工作需要, 這幾天花了點時間看了 &lt;a href="http://activemq.apache.org/cms/index.html" rel="nofollow"&gt;ActiveMQ-CPP&lt;/a&gt; (from &lt;a href="http://www.apache.org" rel="nofollow"&gt;Apache&lt;/a&gt;) 的文件與 source code. 第一印象是 - 這票 Apache 的人會不會太喜歡 Java 了?&lt;br /&gt;
&lt;span id="more-321"&gt;&lt;/span&gt;&lt;br /&gt;
與 Apache &lt;a href="http://activemq.apache.org/" rel="nofollow"&gt;ActiveMQ&lt;/a&gt; (core functions in Java) 一樣, ActiveMQ-CPP (client library in C++) 都是拿 &lt;a href="http://en.wikipedia.org/wiki/Java_Message_Service" rel="nofollow"&gt;JMS&lt;/a&gt; (Java Message Service) 來當範本重新實作. ActiveMQ 的部份我沒看進去就不多談了, 只知道是它是個 JMS message router 的實作. ActiveMQ-CPP 就有趣了, 它是 JMS protocol compatible, i.e. 可以與不同 JMS message router/client 的實作互通, 這很好也很正常. 可是看到它仿造 (modeled after) &lt;a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/jms/package-summary.html" rel="nofollow"&gt;javax.jms&lt;/a&gt; 的 &lt;a href="http://activemq.apache.org/cms/api_docs/cms-1.2/hierarchy.html" rel="nofollow"&gt;API&lt;/a&gt; 就另當別論了.&lt;sup&gt;&lt;a href="#footnote-1-321" id="footnote-link-1-321" class="footnote-link footnote-identifier-link" title="另一個有名的 Apache project, log4cxx 也是仿造 (modeled after) log4j 而來的."&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;不是說 Java 不好 (即便我顯然偏愛 C++). C++ 與 Java 是不同的語言, 除了 syntax 有幾分相似之外&lt;sup&gt;&lt;a href="#footnote-2-321" id="footnote-link-2-321" class="footnote-link footnote-identifier-link" title="The language derives much of its syntax from C and C++..., from Wikipedia."&gt;2&lt;/a&gt;&lt;/sup&gt;, 兩者的編程思維不盡相同, 把 Java 用的 API 近乎完整的 clone 到 C++, 連 utility 都是仿的.&lt;sup&gt;&lt;a href="#footnote-3-321" id="footnote-link-3-321" class="footnote-link footnote-identifier-link" title="One example: decaf::io vs. java.io"&gt;3&lt;/a&gt;&lt;/sup&gt; 我很想知道這裡的 rationale 到底是什麼? &lt;/p&gt;
&lt;p&gt;在多數合理的情況下以 Java 開發軟體比 C++ 容易不少, 這個說法我猜不會有太多人反對. 那為何還是有這麼多人寫 C++? 至少沒聽說過有找不到 C++ 工作的問題.&lt;sup&gt;&lt;a href="#footnote-4-321" id="footnote-link-4-321" class="footnote-link footnote-identifier-link" title="不只是 C++, 也包括 Java, 許多其他程式語言也都是能把晚餐帶回家的技能."&gt;4&lt;/a&gt;&lt;/sup&gt; 除了寫較低階的程式之外, runtime efficiency 可能也是 C++ 依然有它的位置的原因之一. 當然, 也有某些腦筋轉不過來的 programmer, i.e. me, 就只愛寫 C++. 它的迷人之處在於極少設限, 提供使用者 &lt;a href="http://fsfoundry.org/codefreak/2008/04/13/cpp-is-not-privileged-to-language-lawyers/#footnote-3-242"&gt;多種 progamming paradigm&lt;/a&gt;, 甚至有 &lt;a href="http://fsfoundry.org/codefreak/2008/04/11/five-ways-to-write-for-loops/"&gt;多種功能相似的 programming construct&lt;/a&gt; 的怪現象.&lt;/p&gt;
&lt;p&gt;It's the 21th century! 現在的我們已經知道 &lt;a href="http://fsfoundry.org/codefreak/2007/05/22/avoid-pointer-parameters-and-inheritance/#avoid-inheritance"&gt;許多時候不用 inheritance 能夠架構出更 reusable, 更 low coupling 的 C++ code&lt;/a&gt;, 我不懂為什麼 Apache 大神還在跟著 Java &lt;a href="http://activemq.apache.org/cms/api_docs/activemqcpp-2.2.1/classdecaf_1_1lang_1_1_runnable.html" rel="nofollow" rel="nofollow"&gt;走老路&lt;/a&gt;? 要把 code 架構到近 STL/Boost 等 library 的水準的確很不容易, 只是我對掛著 Apache 招牌的 code 的 expectation 不僅如此.&lt;/p&gt;
&lt;p&gt;有句話印象中是這樣說的, "別用 C++ 的語法寫 C 的程式". 目的是要提醒從 C 轉到 C++ 的 programmer 要以 C++ 的方式寫 C++ 程式. 例如避免把 class 的 member data 放到 &lt;code&gt;public&lt;/code&gt; section, 要以 IOStream 取代 &lt;code&gt;printf()&lt;/code&gt; 家族等等. 我想把這句話修改一下也同樣適用, "別用 C++ 的語法寫 Java 的程式". 送給 Apache.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/03/03/caffeine-intoxication/#comments" title="Comments on &amp;quot;咖啡因中毒&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?321" alt="Comments" /&gt;&lt;/a&gt;&lt;ol start="1" class="footnotes"&gt;&lt;li id="footnote-1-321" class="footnote"&gt;另一個有名的 Apache project, &lt;a href="http://logging.apache.org/log4cxx/index.html" rel="nofollow"&gt;log4cxx&lt;/a&gt; 也是仿造 (modeled after) &lt;a href="http://logging.apache.org/log4j/" rel="nofollow"&gt;log4j&lt;/a&gt; 而來的. [&lt;a href="#footnote-link-1-321" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-2-321" class="footnote"&gt;&lt;a href="http://en.wikipedia.org/wiki/Java_(programming_language)" rel="nofollow"&gt;The language derives much of its syntax from C and C++...&lt;/a&gt;, from Wikipedia. [&lt;a href="#footnote-link-2-321" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-3-321" class="footnote"&gt;One example: &lt;a href="http://activemq.apache.org/cms/api_docs/activemqcpp-2.2.1/namespacedecaf_1_1io.html" rel="nofollow"&gt;decaf::io&lt;/a&gt; vs. &lt;a href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/package-summary.html" rel="nofollow"&gt;java.io&lt;/a&gt; [&lt;a href="#footnote-link-3-321" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;li id="footnote-4-321" class="footnote"&gt;不只是 C++, 也包括 Java, 許多其他程式語言也都是能把晚餐帶回家的技能. [&lt;a href="#footnote-link-4-321" class="footnote-link footnote-back-link"&gt;↩&lt;/a&gt;]&lt;/li&gt;&lt;/ol&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=PxrIXsu3aAM:5zHrge79-JA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=PxrIXsu3aAM:5zHrge79-JA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/03/03/caffeine-intoxication/#comments" thr:count="2" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/03/03/caffeine-intoxication/feed/atom/" thr:count="2" />
		<thr:total>2</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/03/03/caffeine-intoxication/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[好狗運]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codefreak/~3/_ryUASjx0Vk/" />
		<id>http://fsfoundry.org/codefreak/2009/03/01/considered-lucky/</id>
		<updated>2009-03-02T05:51:36Z</updated>
		<published>2009-03-01T13:52:54Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="gossip" /><category scheme="http://fsfoundry.org/codefreak" term="mortal" />		<summary type="html"><![CDATA[過了個年, 世界看來已經變了. 去年十月我還說:
只要有兩把刷子的人願意跟著公司的產品或服務的方向玩, 多的是有人願意付錢讓他們每天玩耍.
不到五個月後的今天, 公司的現金夠多竟是讓我依舊睡的安穩的原因之一.
去年春天在找工作的時候景況還一片大好, 也承蒙面試的長官抬愛有三間大公司願意僱用小弟. 由於種種原因, 我放棄了 M 跟 Y 選擇了加入現在服務的公司. 只是當時的我怎麼也不會想到 M 跟 Y 居然會在這麼短的時間內先後砍掉為數不小的 head count. 我真是好狗運.
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2009/03/01/considered-lucky/">&lt;p&gt;過了個年, 世界看來已經變了. 去年十月我還說:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;a href="http://fsfoundry.org/codefreak/2008/10/13/foss-contrib-for-what/"&gt;只要有兩把刷子的人願意跟著公司的產品或服務的方向玩, 多的是有人願意付錢讓他們每天玩耍&lt;/a&gt;.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;不到五個月後的今天, 公司的現金夠多竟是讓我依舊睡的安穩的原因之一.&lt;/p&gt;
&lt;p&gt;去年春天在找工作的時候景況還一片大好, 也承蒙面試的長官抬愛有三間大公司願意僱用小弟. 由於種種原因, 我放棄了 M 跟 Y 選擇了加入現在服務的公司. 只是當時的我怎麼也不會想到 M 跟 Y 居然會在這麼短的時間內先後砍掉為數不小的 head count. 我真是好狗運.&lt;/p&gt;
&lt;br /&gt;&lt;a href="http://fsfoundry.org/codefreak/2009/03/01/considered-lucky/#comments" title="Comments on &amp;quot;好狗運&amp;quot;"&gt;&lt;img src="http://fsfoundry.org/codefreak/wp-content/plugins/feed-comments-number/image.php?320" alt="Comments" /&gt;&lt;/a&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codefreak?a=_ryUASjx0Vk:dvCFdm9ilaE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codefreak?i=_ryUASjx0Vk:dvCFdm9ilaE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
		<link rel="replies" type="text/html" href="http://fsfoundry.org/codefreak/2009/03/01/considered-lucky/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://fsfoundry.org/codefreak/2009/03/01/considered-lucky/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2009/03/01/considered-lucky/</feedburner:origLink></entry>
	</feed>
