<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/atom10full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><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>2008-11-18T08:12:20Z</updated>
	<generator uri="http://wordpress.org/" version="2.3.3">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><entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[C++0x: More on Rvalue References]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/454958855/" />
		<id>http://fsfoundry.org/codefreak/2008/11/16/cpp0x-more-on-rvalue-references/</id>
		<updated>2008-11-17T05:53:33Z</updated>
		<published>2008-11-16T14:57:39Z</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" />		<summary type="html"><![CDATA[上一篇 的主題是 Rvalue References 的語意/語法與功能, 這篇要補完同樣是關於 Rvalue References, 但較為瑣碎的細節.

Binding
A quote from n1377:
Remember: you really do not want to bind a temporary to a non-const reference &#8230; except when you do!
讓我們先把場景設定一下: (借用 n1377 的範例)

// listing-1

struct A {};
A source();
const A const_source();

void foo(const A&#038; t);  // #1
void foo(A&#038;&#038; t);       // #2


在 [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-more-on-rvalue-references/"><![CDATA[<p><a href="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/">上一篇</a> 的主題是 Rvalue References 的語意/語法與功能, 這篇要補完同樣是關於 Rvalue References, 但較為瑣碎的細節.<br />
<span id="more-306"></span></p>
<h3>Binding</h3>
<p>A quote from <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm" rel="nofollow">n1377</a>:</p>
<blockquote><p>Remember: you really do not want to bind a temporary to a non-const reference &#8230; except when you do!</p></blockquote>
<p>讓我們先把場景設定一下: (借用 n1377 的範例)</p>
<div id="listing-1">
<pre>// <a href="#listing-1">listing-1</a>

struct A {};
A source();
const A const_source();

void foo(const A&#038; t);  // #1
void foo(A&#038;&#038; t);       // #2
</pre>
</div>
<p>在 binding 選定時, 一個 rvalue 會優先與 rvalue reference 產生 binding; lvalue 則偏好 lvalue reference. 其 CV qualification<sup><a href="#footnote-1-306" id="footnote-link-1-306" class="footnote-link footnote-identifier-link" title="The const-ness and/or volatile-ness of a type.">1</a></sup> 在此時為次要考量:</p>
<div id="listing-2">
<pre>// <a href="#listing-2">listing-2</a>

void f()
{
  A a;
  const A ca;
  foo(a);               // call I:   binds to #1
  foo(ca);              // call II:  binds to #1
  foo(source());        // call III: binds to #2
  foo(const_source());  // call IV:  binds to #1
}</pre>
</div>
<p>一如往常, 一個更多 CV qualification 的 instance 不能與較少 CV qualification 的 lvalue/rvalue reference 發生綁定. 除非在候選人 (overload set) 中沒有更適合的 rvalue reference, 一個 rvalue 總是會與 rvalue reference 做綁定. 反之, lvalue 的狀況也是一樣.</p>
<h3>Named Rvalue References</h3>
<p>上一篇的開場就說了 &#8212; 一個 rvalue reference 其實就是 reference to an un-named instance (temporary). 現在又生出一種叫作 Named Rvalue References 的新玩意 &#8212; <strong>有名字的無名物件之參考</strong>!? 這乍聽之下似乎有點匪夷所思, 可是實際上卻沒任何詭異之處. 因為拿 rvalue reference 來當參數的時候, 通常是有名字的. 見上一篇 <a href="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/#listing-11"><code>String</code> class 的 move-constructor</a> 與 <a href="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/#listing-15">listing-15 的 <code>compute3()</code></a>.</p>
<p>扯遠了&#8230; 重點是, 當與一個 named rvalue reference (下例中的 <code>x</code>) 發生綁定後, 在 callee 的 function body 內, 該 binding (<code>x</code>) 卻是被當作 lvalue 的:</p>
<div id="listing-3">
<pre>// <a href="#listing-3">listing-3</a>

void bar(A&#038;&#038; t);       // #3

void g(A&#038;&#038; x)
{
  foo(x);       // call V:   binds to #1
  foo(move(x)); // call VI:  binds to #2
  bar(x);       // call VII: binds to #3
}</pre>
</div>
<p>想當然, 這是為了避免使用者無意間觸發了 move 機制, 而得到非預期的結果. 只有 rvalue (i.e. temporary), 才會優先選擇與 rvalue reference 產生 binding. (如 <a href="#listing-2">listing-2</a> 的 call III)</p>
<ol start="1" class="footnotes"><li id="footnote-1-306" class="footnote">The <code>const</code>-ness and/or <code>volatile</code>-ness of a type. [<a href="#footnote-link-1-306" class="footnote-link footnote-back-link">↩</a>]</li></ol>]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/11/16/cpp0x-more-on-rvalue-references/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[C++0x: Rvalue References]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/454827439/" />
		<id>http://fsfoundry.org/codefreak/2008/11/16/c0x-rvalue-references/</id>
		<updated>2008-11-18T08:12:20Z</updated>
		<published>2008-11-16T11:00:51Z</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" />		<summary type="html"><![CDATA[Intro
Rvalue-References 是 C++0x 規範的新 language feature, 也是繼 Templates 後, IMO, C++ 語言最重要的 language feature. 其目的是為了支持 Move Semantics &#8212; 將 resource 從一個 instance 轉移到另一個 instance.
Rvalue &#8212; 這個聽起來饒舌的新名詞 &#8212; 說穿了其實就是 un-named instance (i.e. temporary), 反之, 有名字的 (named) 則為 lvalue. 而 const/volatile 與否則是額外的修飾. 也就是說, 一個 rvalue reference 其實就是 reference to a temporary.











The Scenario
以一個簡單的 string class 為例:

// listing-1

class String
{
public:
 [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/"><![CDATA[<h3>Intro</h3>
<p>Rvalue-References 是 C++0x 規範的新 language feature, 也是繼 Templates 後, IMO, C++ 語言最重要的 language feature. 其目的是為了支持 Move Semantics &#8212; 將 resource 從一個 instance 轉移到另一個 instance.</p>
<p>Rvalue &#8212; 這個聽起來饒舌的新名詞 &#8212; 說穿了其實就是 un-named instance (i.e. temporary), 反之, 有名字的 (named) 則為 lvalue. 而 <code>const</code>/<code>volatile</code> 與否則是額外的修飾. 也就是說, 一個 rvalue reference 其實就是 reference to a temporary.<br />
<span id="more-304"></span><table align="center">
<tr>
<td>
<script type="text/javascript"><!--
google_ad_client = "pub-9597535005741556";
/* 468x60, 2008/6/13 */
google_ad_slot = "6320497412";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>
</table>
<br /></p>
<h3>The Scenario</h3>
<p>以一個簡單的 string class 為例:</p>
<div id="listing-1">
<pre>// <a href="#listing-1">listing-1</a>

class String
{
public:
  String(const String&#038; other)
    : data_(new char[other.length_]),
      length_(other.length_)
  {
    <a href="http://www.dinkumware.com/manuals/?manual=compleat&#038;page=algorith.html#copy" rel="nofollow">copy</a>(
      other.data_,
      other.data_ + other.length_,
      data_);
  }
  // Details omitted&#8230;

private:
  char* data_;
  size_t length_;
};</pre>
</div>
<p>這個例子可說是 C++ RAII 的典型範例. <code>String</code> class 的 instance 在 copy-construct 的時候, 會先 allocate 一塊 memory, 然後將被複製的對象的內容 <code>copy</code> 過來. 雖然我們可以很明顯的看出複製 <code>String</code> instance 的 cost, 但大多時候這個 cost 並不會造成 C++ programmer 的困擾. 需要複製的時候, 該付出的 cost 本來就是省不掉的, 而絕大多數 C++ programmer 早已知道傳遞 function call parameter 的時候可以用 call-by-reference 以避開不必要的複製:</p>
<div id="listing-2">
<pre>// <a href="#listing-2">listing-2</a>

int compute(const String&#038; arg)
{
  // Evaluate `arg&#8217; and do some computation&#8230;
  return hash;
}

String produce()
{
  // Produce then return a String instance&#8230;
}</pre>
</div>
<div id="listing-3">
<pre>// <a href="#listing-3">listing-3</a>

void client()
{
  String arg(produce());
  int hash = compute(arg);
}</pre>
</div>
<p>讓我們把狀況稍微搞複雜一點, 如果餵給 <code>compute()</code> 的參數不是一個 lvalue, 而是一個如 return value 的 rvalue 呢:</p>
<div id="listing-4">
<pre>// <a href="#listing-4">listing-4</a>

void client()
{
  int hash = compute(produce());
}</pre>
</div>
<p>這也沒問題, 一個 temporary 的生命周期跟指向它的 <code>const reference</code> 一樣長, 與下面的狀況是相同的:</p>
<div id="listing-5">
<pre>// <a href="#listing-5">listing-5</a>

void client()
{
  int hash = compute(&#8221;blah&#8221;);
}</pre>
</div>
<h3>The Problem</h3>
<p>目前為止, 即便跨了數個 stack frame, 在我們的 code stack 上只有一份 <code>String</code> instance, 沒有發生複製, 一切都很理想. 現在假設 <code>compute()</code> 這個 function 需要對客戶端給的 parameter 作 local 的修改以利運算, 譬如把 double quote (<code>"</code>) remove 掉:</p>
<div id="listing-6">
<pre>// <a href="#listing-6">listing-6</a>

int compute2(const String&#038; arg)
{
  String var(arg);
  var.Remove(&#8217;\&#8221;&#8216;);
  // Evaluate `var&#8217; and do some computation&#8230;
  return hash;
}</pre>
</div>
<p>我們甚至可以針對像是 <a href="#listing-3">listing-3</a> &#8212; 以 lvalue 為參數的客戶端進行 overload:</p>
<div id="listing-7">
<pre>// <a href="#listing-7">listing-7</a>

int compute2(String&#038; arg)
{
  arg.Remove(&#8217;\&#8221;&#8216;);
  // Evaluate `arg&#8217; and do some computation&#8230;
  return hash;
}</pre>
</div>
<p>然後再改寫 <a href="#listing-6">listing-6</a> 的版本, 以利 code reuse:</p>
<div id="listing-8">
<pre>// <a href="#listing-8">listing-8</a>

int compute2(const String&#038; arg)
{
  String var(arg);
  return compute2(var);
}</pre>
</div>
<p>先不論 <a href="#listing-7">listing-7</a> 的 <code>compute2()</code> 對客戶端參數做修改的恰當與否, <code>compute2()</code> 的確在<strong>特定狀況</strong>下避開了複製. <em>Note: <a href="#listing-7">listing-7</a> 與 <a href="#listing-8">listing-8</a> 的 <code>compute2()</code> 為同時存在的兩個 overload</em>. 但這方法好嗎? 一點也不.</p>
<p>當參數 為 rvalue 時 (如 <a href="#listing-4">listing-4</a> and <a href="#listing-5">listing-5</a>), 客戶端絕對是沒有可能繼續參考該 instance. 反之, 在參數為 lvalue 時 (如 <a href="#listing-3">listing-3</a>), 客戶端很可能打算接下來繼續使用該 instance, 但卻會被 <code>compute2()</code> 給修改了. 這樣的 overload 對用戶端可能需要繼續使用的 instance 進行 mutable 操作, <strong>卻放過完全可以安全重複使用的狀況</strong>.</p>
<h3>One Option</h3>
<p>若要能正確接收能放心重複使用的參數, 以現行 C++ Standard, 唯一的方法是在 <code>String</code> class 內 implement 類似 <a href="http://www.dinkumware.com/manuals/?manual=compleat&#038;page=memory.html#auto_ptr" rel="nofollow"><code>auto_ptr</code></a>/<a href="http://www.dinkumware.com/manuals/?manual=compleat&#038;page=memory.html#auto_ptr_ref" rel="nofollow"><code>auto_ptr_ref</code></a> 的機制.<sup><a href="#footnote-1-304" id="footnote-link-1-304" class="footnote-link footnote-identifier-link" title="有興趣的朋友可以參考 gion 的實作 &#8212; auto_handle 與 auto_handle_ref 之間的互動.">1</a></sup></p>
<p>這方法不但非語言標準, 除了要針對每個支持這語意的 user-defined class 多寫不少 code, 也無法在 compile 階段阻止使用者做出錯誤的操作, 如:</p>
<div id="listing-9">
<pre>// <a href="#listing-9">listing-9</a>

template &lt;class T&gt;
void leak(auto_ptr&lt;T&gt; foo)
{
  auto_ptr_ref&lt;T&gt; leaked(foo);
}</pre>
</div>
<p><em>Note: <a href="#listing-9">listing-9</a> 會把 <code>foo</code> 管理的指標 leak 掉.</em></p>
<p>更糟糕的是, 光是類似 <code>auto_ptr</code>/<code>auto_ptr_ref</code> 的機制是不夠的, 這樣的機制只能達成 always-move 的功能, 無法放過 lvalue 的參數. 要達到全面的支持 &#8212; 連同放過 lvalue &#8212; 還需要寫更多 code&#8230;</p>
<h3>The Solution &#8212; Rvalue References</h3>
<p>C++0x 的 Rvalue References, 這個新的 language feature 就是為了解決這難題而誕生的.</p>
<p>要完整支持 Rvalue References, 首先我們得替 <code>String</code> class 加一個新的建構子 &#8212; <strong>Move-Constructor</strong>:<sup><a href="#footnote-2-304" id="footnote-link-2-304" class="footnote-link footnote-identifier-link" title="別忘了還有 Move-Assignment operator.">2</a></sup></p>
<div id="listing-11">
<pre>// <a href="#listing-11">listing-11</a>

class String
{
public:
  String(const String&#038; other);
  <strong>String(String<u>&#038;&#038;</u> other)
    : data_(other.data_),
      length_(other.length_)
  {
    other.data_ = nullptr;
    other.length_ = 0;
  }</strong>
  // Details omitted&#8230;
};</pre>
</div>
<p>為了避免無意間下觸發 move 機制, 在 C++0x 的規範中 lvalue 被自動轉為 rvalue reference 的優先順序較低. 在 lvalue 與 rvalue 都可行的狀況下,<sup><a href="#footnote-3-304" id="footnote-link-3-304" class="footnote-link footnote-identifier-link" title="lvalue 與 rvalue 都可行的意思是 String 的兩個 ctor 都被選入 overload set. 詳見 C++0x, More on Rvalue References.">3</a></sup> 使用者必須顯性轉換 (explicit cast). 這時候 <code>String</code> 的 client code 可以以 resource 轉移的方式建構新的 <code>String</code> instance:</p>
<div id="listing-12">
<pre>// <a href="#listing-12">listing-12</a>

void bar()
{
  String src(&#8221;blah&#8221;);

  // This triggers String&#8217;s move-ctor.
  String dst(<strong>static_cast&lt;String&#038;&&gt;</strong>(src));
}</pre>
</div>
<p>為了給使用者容易表達的語法以及清楚的語意, C++0x 定義了叫 <code>move()</code> 的 helper function template:</p>
<div id="listing-13">
<pre>// <a href="#listing-13">listing-13</a>

template &lt;class T&gt;
typename remove_reference&lt;T&gt;::type&#038;&#038;
  move(T&#038;&#038; x)
{
  return x;
}</pre>
</div>
<p>特別注意, as a special case, 在 template funciton 內, 如上例, 如果 caller 給的參數為 lvalue, 則 <code>T</code> 為 reference type.<sup><a href="#footnote-4-304" id="footnote-link-4-304" class="footnote-link footnote-identifier-link" title="Note that T&#038; &#038;&#038; 會被推導為 T&#038;, 稱為 reference collapsing.">4</a></sup> 若參數為 rvalue, 則 <code>T</code> 為 plain type.<sup><a href="#footnote-5-304" id="footnote-link-5-304" class="footnote-link footnote-identifier-link" title="Template Argument Deduction with A&#038;&#038;.">5</a></sup> 於是 <a href="#listing-13"><code>move()</code></a> 的功能就只是把參數 <code>x</code> 轉為一個 rvalue reference, 不論 <code>x</code> 原來究竟是 lvalue/rvalue 還是本來就是 rvalue reference. 讓使用者可以以明白的顯性方式使用 move semantics, <a href="#listing-12"><code>bar()</code></a> 的 code 就可以改成:</p>
<div id="listing-14">
<pre>// <a href="#listing-14">listing-14</a>

void bar2()
{
  String src(&#8221;blah&#8221;);

  // This also triggers String&#8217;s move-ctor.
  String dst(<strong>move</strong>(src));
}</pre>
</div>
<p>到了這裡, 我們應該已經知道 &#8212; 在使用有 Rvalue References 支持的 compiler 時 &#8212; 該如何正確地寫出理想的 <code>compute2()</code> 的 overload, 就叫 <code>compute3()</code> 吧:</p>
<div id="listing-15">
<pre>// <a href="#listing-15">listing-15</a>

int compute3(<strong>String&#038;&#038;</strong> arg)
{
  arg.Remove(&#8217;\&#8221;&#8216;);
  // Evaluate `arg&#8217; and do some computation&#8230;
  return hash;
}</pre>
</div>
<div id="listing-16">
<pre>// <a href="#listing-16">listing-16</a>

int compute3(const String&#038; arg)
{
  String var(arg);
  return compute3(<strong>move</strong>(var));
}</pre>
</div>
<p>在 client code 不需要繼續使用餵給 <code>compute3()</code> 的參數時可以這樣寫:</p>
<div id="listing-17">
<pre>// <a href="#listing-17">listing-17</a>

void client3a()
{
  // Call to compute3() as of <a href="#listing-15">listing-15</a>.
  int hash = compute3(<strong>move</strong>(produce()));
}</pre>
</div>
<p>或是畫蛇添足一下:</p>
<div id="listing-18">
<pre>// <a href="#listing-18">listing-18</a>

void client3b()
{
  String arg(<strong>move</strong>(produce()));

  // Call to compute3() as of <a href="#listing-15">listing-15</a>.
  int hash = compute3(<strong>move</strong>(arg));

  // Note that `arg&#8217; is now in an user-defined <strong>empty</strong> state.
  // Similar to a <a href="http://www.dinkumware.com/manuals/?manual=compleat&#038;page=memory.html#auto_ptr::release" rel="nofollow">released</a> auto_ptr</a>.
}</pre>
</div>
<p>若還需要繼續參考該參數時:</p>
<div id="listing-19">
<pre>// <a href="#listing-19">listing-19</a>

void client3c()
{
  String arg(<strong>move</strong>(produce()));

  // Call to compute3() as of <a href="#listing-16">listing-16</a>.
  int hash = compute3(arg);

  // Continue doing more computation with possible references to `arg&#8217;.
}</pre>
</div>
<h3>References</h3>
<ul>
<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html" rel="nofollow">A Brief Introduction to Rvalue References</a></li>
<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm#Cast%20to%20rvalue" rel="nofollow">A Proposal to Add Move Semantics Support to the C++ Language</a></li>
</ul>
<ol start="1" class="footnotes"><li id="footnote-1-304" class="footnote">有興趣的朋友可以參考 <a href="http://code.google.com/p/gion/">gion</a> 的實作 &#8212; <a href="http://code.google.com/p/gion/source/browse/branches/0.2/include/gion/auto_handle.hpp#51"><code>auto_handle</code></a> 與 <a href="http://code.google.com/p/gion/source/browse/branches/0.2/include/gion/auto_handle.hpp#33"><code>auto_handle_ref</code></a> 之間的互動. [<a href="#footnote-link-1-304" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-2-304" class="footnote">別忘了還有 Move-Assignment operator. [<a href="#footnote-link-2-304" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-3-304" class="footnote">lvalue 與 rvalue 都可行的意思是 <code>String</code> 的兩個 ctor 都被選入 overload set. 詳見 <a href="">C++0x, More on Rvalue References</a>. [<a href="#footnote-link-3-304" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-4-304" class="footnote">Note that <u><code>T&#038; &#038;&#038;</code></u> 會被推導為 <u><code>T&#038;</code></u>, 稱為 <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm#References%20to%20References" rel="nofollow">reference collapsing</a>. [<a href="#footnote-link-4-304" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-5-304" class="footnote"><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm#Template%20Argument%20Deduction%20with%20A%26%26" rel="nofollow">Template Argument Deduction with A&#038;&#038;</a>. [<a href="#footnote-link-5-304" class="footnote-link footnote-back-link">↩</a>]</li></ol>]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[C++0x 風暴, 即將來襲]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/435584049/" />
		<id>http://fsfoundry.org/codefreak/2008/10/29/incoming-strom-cpp0x/</id>
		<updated>2008-11-12T03:37:52Z</updated>
		<published>2008-10-29T07:49:23Z</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="geeky" />		<summary type="html"><![CDATA[今天從 Sutter&#8217;s Mill 知道  最新的 C++0x draft (n2798) 已經正式 publish 以被 review 與 comment. Sutter 稱這 draft 為 feature complete.
也是今天, 在 Visual C++ Team Blog 看到 Visual Studio 2010 的 Community Technology Preview (CTP) 版本也 release 了 (按此前往下載頁面. Warning: 11 個 split 過的 rar, 約 7350 MB). 其中最令人興奮的正是對 C++0x 的支持:

Lambda Expressions (functional programming 來了, [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/10/29/incoming-strom-cpp0x/"><![CDATA[<p>今天從 Sutter&#8217;s Mill 知道 <a href="http://herbsutter.wordpress.com/2008/10/28/september-2008-iso-c-standards-meeting-the-draft-has-landed-and-a-new-convener/" rel="nofollow"> 最新的 C++0x draft</a> (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf" rel="nofollow">n2798</a>) 已經正式 publish 以被 review 與 comment. Sutter 稱這 draft 為 feature complete.</p>
<p>也是今天, 在 Visual C++ Team Blog 看到 <a href="http://blogs.msdn.com/vcblog/archive/2008/10/28/visual-studio-2010-ctp-released.aspx" ref="nofollow">Visual Studio 2010 的 Community Technology Preview (CTP) 版本也 release 了</a> (<a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=922B4655-93D0-4476-BDA4-94CF5F8D4814&#038;displaylang=en" rel="nofollow">按此前往下載頁面</a>. Warning: 11 個 split 過的 rar, 約 7350 MB). 其中最令人興奮的正是對 C++0x 的支持:</p>
<ul>
<li>Lambda Expressions (functional programming 來了, C++ 請接招)</li>
<li>Rvalue References (opportunity for major performance boost)</li>
<pre>
void foo()
{
  list&lt;string&gt; strings;
  // strings.push_back("bar");
  strings.push_back(<strong>string(&#8221;bar&#8221;)</strong>);
}
</pre>
<p>這段 code 將只需要兩次 memory allocation, 一次在 <code>list</code>, 一次在 <code>string</code>. 因 <del><code>"bar"</code></del> <ins><code>string("bar")</code></ins> (note: see <a href="http://fsfoundry.org/codefreak/2008/10/29/incoming-strom-cpp0x/#comment-4728">comment from icek</a>) 而產生的 <code>string</code> <strong>暫時物件</strong>的 resource 將直接轉移到 <code>list</code> 內部新創建的 element 身上 (move-construct). 不再是把該<strong>暫時物件</strong> copy 到新創建的 element 身上 (copy-construct).</p>
<p><em>Note: 雖然 VS10 的 C++ compiler 已經支援 Rvalue-References, 但由於 VS10 所 ship 的 STL (by Dinkumware) 尚未實現 Rvalue-References 的部份, 因此, 在 VC10 的 STL 改善之前, 上面的 code snip 仍然會觸發 <code>string</code> 的 copy-construction, 換句話說會引發三次 allocation.</em></p>
<li>static_assert (compile time 即可 assert 的不再需要等到 run time)</li>
<li>auto Keyword (懶人好幫手)</li>
<pre>
template &lt;class Container&gt;
void bar(const Container&#038; cont)
{
  for(<strong>auto first</strong> = cont.begin(); first != cont.end(); ++first)
  {
    // do meaningful things here&#8230;
  }
}
</pre>
<p>別把用在此處的 <code>auto</code> 與 variant 混為一談, 兩者完全不同. 後者 (variant) 的型別 (或者說意義) 是 runtime 決定的 (可參考 <a href="http://www.boost.org/doc/libs/1_36_0/doc/html/variant.html" rel="nofollow">Boost.Variant</a> 與 <a href="http://msdn.microsoft.com/en-us/library/ms221627.aspx" rel="nofollow">COM 的 variant</a>). 前者 (<code>auto</code>) 則是在 compile time 就決定了. 簡單的說, 是 compiler 在 compile 時藉由後面的敘述 (例中的 <code>cont.begin()</code>) 推導出 <code>auto</code> 定義的變數 (<code>first</code> ) 的 type.
</ul>
<p>另外, FOSS 陣營的王道 compiler, GCC, 則是一直有在 <a href="http://gcc.gnu.org/projects/cxx0x.html" rel="nofollow">C++0x 的支持方面持續加強</a> (GCC 4.3/4.4). 不過 (就我所知) 還沒進入主流的 GNU/Linux distro.</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/10/29/incoming-strom-cpp0x/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Protected: 今天, 我沒有臉承認我是 - 台灣人 (passwd: shameless)]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/427684508/" />
		<id>http://fsfoundry.org/codefreak/2008/10/22/%e4%bb%8a%e5%a4%a9-%e6%88%91%e6%b2%92%e6%9c%89%e8%87%89%e6%89%bf%e8%aa%8d%e6%88%91%e6%98%af-%e5%8f%b0%e7%81%a3%e4%ba%ba/</id>
		<updated>2008-10-21T17:38:38Z</updated>
		<published>2008-10-21T17:30:52Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="mortal" /><category scheme="http://fsfoundry.org/codefreak" term="shameless" />		<summary type="html"><![CDATA[There is no excerpt because this is a protected post.]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/10/22/shame-on-us-taiwanese/"><![CDATA[<form action="http://fsfoundry.org/codefreak/wp-pass.php" method="post">
<p>This post is password protected. To view it please enter your password below:</p>
<p><label>Password:<br />
<input name="post_password" type="password" size="20" /></label><br />
<input type="submit" name="Submit" value="Submit" /></p></form>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/10/22/shame-on-us-taiwanese/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[搞 FOSS 為哪樁?]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/418707196/" />
		<id>http://fsfoundry.org/codefreak/2008/10/13/foss-contrib-for-what/</id>
		<updated>2008-10-19T13:29:15Z</updated>
		<published>2008-10-12T16:55:36Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="foss" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="mortal" />		<summary type="html"><![CDATA[
看了一篇 Slashdot 上有趣的文字 - Getting Paid To Abandon an Open Source Project?. 有感而發, 寫了下面的廢話.


除了因為工作而熟識的朋友之外, 現實世界中我的朋友大部份是正常人 (A.K.A. 不靠寫軟體生活的人. 不過, 飆車族 夜店咖 倒是還不少. :-P) 雖然在我幾乎不在他們面前提工作/技術上的事情,1 他們都知道我的工作是搞軟體技術的. 也大都清楚我的工作對我來說是玩耍遠遠大於討生活.
我很想說我自認還算是個硬派技術人, 不是世俗的物質可以打動我.2 只是現實並不總是如此簡單, 結了婚有了家, 對於用以滿足物質需要的金錢需求也比以前來得高. 但仍舊只要是能讓我把帳單付清, 滿足我與老婆能擁有些許舒適生活的穩定收入就可以接受. 這當然不會是個跳樓大拍賣的價位, (IMO) 卻也絕對不會是獅子大開口的數字.3 一直到現在, 我更在乎的是 - how much fun am I gonna get? 我能不能盡情發揮我的長處? 與我一起工作的人是不是也很有趣? 會不會比我更猛讓我學習?
好幾次我被朋友問到 - 為什麼這麼好心, 花自己的時間無償地寫自由軟體?  是想出名? 是希望有一天可以藉此以某種方式賺到錢?
呵呵~ 搞軟體技術的人中有很一群特別有意思的人. 他們跟我一樣, [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/10/13/foss-contrib-for-what/"><![CDATA[<p><a href="" rel="nofollow"></a></p>
<p>看了一篇 <a href="http://slashdot.org/" rel="nofollow">Slashdot</a> 上有趣的文字 - <a href="http://ask.slashdot.org/askslashdot/08/10/05/1317252.shtml" rel="nofollow">Getting Paid To Abandon an Open Source Project?</a>. 有感而發, 寫了下面的廢話.<br />
<span id="more-300"></span><br />
<hr/>
<p>除了因為工作而熟識的朋友之外, 現實世界中我的朋友大部份是正常人 (A.K.A. 不靠寫軟體生活的人. 不過, <a href="http://fsfoundry.org/codefreak/2006/06/09/the-ghost-of-my-past/">飆車族</a> <a href="http://fsfoundry.org/gallery/main.php?g2_itemId=4088">夜店咖</a> 倒是還不少. :-P) 雖然在我幾乎不在他們面前提工作/技術上的事情,<sup><a href="#footnote-1-300" id="footnote-link-1-300" class="footnote-link footnote-identifier-link" title="拜託喔~ 這種 geek 才聽的下去的東西誰有興趣啊!?">1</a></sup> 他們都知道我的工作是搞軟體技術的. 也大都清楚我的工作對我來說是玩耍遠遠大於討生活.</p>
<p>我很想說我自認還算是個硬派技術人, 不是世俗的物質可以打動我.<sup><a href="#footnote-2-300" id="footnote-link-2-300" class="footnote-link footnote-identifier-link" title="我就曾經幹過為了選擇我熱愛的差事而放棄薪水高三分之一的工作這種白痴的事">2</a></sup> 只是現實並不總是如此簡單, 結了婚有了家, 對於用以滿足物質需要的金錢需求也比以前來得高. 但仍舊只要是能讓我把帳單付清, 滿足我與老婆能擁有些許舒適生活的穩定收入就可以接受. 這當然不會是個跳樓大拍賣的價位, (IMO) 卻也絕對不會是獅子大開口的數字.<sup><a href="#footnote-3-300" id="footnote-link-3-300" class="footnote-link footnote-identifier-link" title="不過, 我是不會嫌老闆給的錢多啦~">3</a></sup> 一直到現在, 我更在乎的是 - how much fun am I gonna get? 我能不能盡情發揮我的長處? 與我一起工作的人是不是也很有趣? 會不會比我更猛讓我學習?</p>
<p>好幾次我被朋友問到 - 為什麼這麼好心, 花自己的時間無償地寫自由軟體?  是想出名? 是希望有一天可以藉此以某種方式賺到錢?</p>
<p>呵呵~ 搞軟體技術的人中有很一群特別有意思的人. 他們跟我一樣, 不是為了工作而是因為興趣幹了這行. 這些人是受到祝福的一群人. 對他們來說每天上班等於是在玩樂, 只要他們願意玩的方向儘量靠近公司的產品或服務, 多的是有人願意付錢讓他們每天玩耍.<sup><a href="#footnote-4-300" id="footnote-link-4-300" class="footnote-link footnote-identifier-link" title="當然除了興趣還要有一定實力才有這麼爽的待遇.">4</a></sup></p>
<p>既然已經過著有得玩又有得拿的生活了, 又在下班時間搞自由軟體是怎樣的心態呢? 答案很簡單, 看了下面這個比方你就懂了:</p>
<ul>
小明的興趣是開飛機, 他很幸運的得到個民航機師的工作. 可是其實教他熱血沸騰的是戰鬥機的貼背感, dog fight 時的 G force. 這些都是沒有辦法透過工作上駕駛民航機的任務得到滿足. 於是, 他希望可以在工作之餘, 即便需要花錢也願意, 能有機會可以駕馭戰鬥機親身體驗. 就算無緣駕駛真正的戰鬥機, 有大型模擬器機台, 甚至是 flight simulation games, 能過過乾癮也好.
</ul>
<p>雖說我的工作就是我的興趣, 但還是有某些特定領域的題目是我更想玩的. 既然工作上沒機會玩到, 其中一個解癮的方法就是自己或是跟志同道合的朋友一起搞個 FOSS project 一起玩耍吧!</p>
<p>若玩耍的產出物能 somehow 讓我們得到某種額外的回饋當然是很好. 即便沒有, (我們也不期望會有) 我們已經得到追求的那樣東西了 - 這個玩耍的過程.</p>
<ol start="1" class="footnotes"><li id="footnote-1-300" class="footnote">拜託喔~ 這種 geek 才聽的下去的東西誰有興趣啊!? [<a href="#footnote-link-1-300" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-2-300" class="footnote">我就曾經幹過為了選擇我熱愛的差事而放棄薪水高三分之一的工作這種白痴的事 [<a href="#footnote-link-2-300" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-3-300" class="footnote">不過, 我是不會嫌老闆給的錢多啦~ [<a href="#footnote-link-3-300" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-4-300" class="footnote">當然除了興趣還要有一定實力才有這麼爽的待遇. [<a href="#footnote-link-4-300" class="footnote-link footnote-back-link">↩</a>]</li></ol>]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/10/13/foss-contrib-for-what/</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://feeds.feedburner.com/~r/codefreak/~3/411774390/" />
		<id>http://fsfoundry.org/codefreak/2008/10/05/retro-former-job/</id>
		<updated>2008-10-12T14:16:06Z</updated>
		<published>2008-10-05T09:35:36Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="跳跳" /><category scheme="http://fsfoundry.org/codefreak" term="mortal" />		<summary type="html"><![CDATA[周六的午夜 (其實是今天凌晨零點多), 跟 Michael 一起回 (前) 公司帶仍然在加班的可憐 Polo 去打撞球. 見到了 Tye, 意外的還看到了幾位北京過來支援 Polo 的同事以及督軍兼在現場打雜的我的老長官 阿國. 熟悉的場景, 親切的臉孔, 還有我的回憶&#8230;


我在 2004 的下半年再次加入了一間 startup. 加入該公司的最大原因是想會一會一位傳說 Taiwan No. 1 的 C++ guy, 一位比我先加入卻比我晚報到的同事. 雖然說單就這點而言我沒得到滿足,1 這份工作依然讓我見識到包含這位 No.1 在內的許多強者.
一開始, 我的上司是一位有趣的白人, 他是一位積極參與 SIP 相關的 IETF 標準制定, 以及 Open Source project 的加拿大人. 後來有段時間我的上司是一位 (在我眼中至少一度是) 整個華人世界最 重要的 FOSS project founder/leader and Linux kernel contributor [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/10/05/retro-former-job/"><![CDATA[<p>周六的午夜 (其實是今天凌晨零點多), 跟 <a href="http://fsfoundry.org/gallery/main.php?g2_itemId=4290">Michael</a> 一起回 (前) 公司帶仍然在加班的可憐 <a href="http://fsfoundry.org/codefreak/2006/09/29/to-polo-an-email-not-sent/">Polo</a> 去打撞球. 見到了 <a href="http://fsfoundry.org/gallery/main.php?g2_itemId=826">Tye</a>, 意外的還看到了幾位北京過來支援 Polo 的同事以及督軍兼在現場打雜的我的老長官 <a href="http://fsfoundry.org/gallery/main.php?g2_itemId=89">阿國</a>. 熟悉的場景, 親切的臉孔, 還有我的回憶&#8230;<br />
<span id="more-301"></span><br />
<hr />
<p>我在 2004 的下半年再次加入了<a href="http://fsfoundry.org/codefreak/tag/teltel/">一間 startup</a>. 加入該公司的最大原因是想會一會一位傳說 Taiwan No. 1 的 C++ guy, 一位比我先加入卻比我晚報到的同事. 雖然說單就這點而言我沒得到滿足,<sup><a href="#footnote-1-301" id="footnote-link-1-301" class="footnote-link footnote-identifier-link" title="沒到 No. 1 其實也挺厲害的了">1</a></sup> 這份工作依然讓我見識到包含這位 No.1 在內的許多強者.</p>
<p>一開始, 我的上司是一位有趣的白人, 他是一位積極參與 SIP 相關的 IETF 標準制定, 以及 <a href="http://www.resiprocate.org/Main_Page" rel="nofollow">Open Source project</a> 的加拿大人. 後來有段時間我的上司是一位 (在我眼中至少一度是) 整個華人世界最 <a href="http://fsfoundry.org/gallery/main.php?g2_itemId=3090">重要的 FOSS project founder/leader and Linux kernel contributor</a> 之一.</p>
<p>這份工作不但使我得到機會參加了辦在美國 Cisco 辦公室的 Open Source hackathon.<sup><a href="#footnote-2-301" id="footnote-link-2-301" class="footnote-link footnote-identifier-link" title="還記得當時的主要 agenda 之一是完成全球第一個使用 DTLS 的 project.">2</a></sup><sup><a href="#footnote-3-301" id="footnote-link-3-301" class="footnote-link footnote-identifier-link" title="Cisco 非該 hackathon 主辦單位, 僅在週末提供場地">3</a></sup> 更有幸能與幾位 RFC 作者甚至是 working group 的 chair 面對面一起畫白板討論 protocol 一起寫 code, 以致我後來有可以在 MSN 上直接請益特定 RFC 上沒寫清楚或我看不懂的部份的特權. 這段期間我玩得很開心.<sup><a href="#footnote-4-301" id="footnote-link-4-301" class="footnote-link footnote-identifier-link" title="很可惜後來才開始 blog, 否則應該會更清楚紀錄這些有趣的事情.">4</a></sup></p>
<p>直到 2006 年底公司的 road map/direction 發生第二次重大的改變. 接下來半年左右的時間, 我的工作內容轉為我一直提不起興趣的題目, <a href="http://fsfoundry.org/codefreak/2007/11/03/time-to-say-goodbye/">心情跟 productivity 都陷入低潮</a>. 即便長官對我的表現是儘量忍耐, (現在回頭想想, 還真是辛苦你們了) 儘管我是多麼喜歡這票 <a href="http://fsfoundry.org/gallery/main.php?g2_itemId=1646">讓我每天上班都會開心的同事</a>, 雖然我也很肖想享受到未來有機會值錢的 stock option. 離開對我來說已是無可避免的 <a href="http://fsfoundry.org/codefreak/2007/07/25/next-stop/">決定</a>.</p>
<p>離開後經過短暫 (半年多) 的上海體驗營, 我又回到了台北, 也很慎重的選擇了我喜歡的新工作. 雖然不能繼續站在你們身旁, 並肩作戰過的伙伴們啊, 請連我的份也一起加油吧!</p>
<p>PS. 不過你們加班也加得太瘋狂了吧!? 畢竟健康還是比較重要啊!</p>
<ol start="1" class="footnotes"><li id="footnote-1-301" class="footnote">沒到 No. 1 其實也挺厲害的了 [<a href="#footnote-link-1-301" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-2-301" class="footnote">還記得當時的主要 agenda 之一是完成全球第一個使用 <a href="http://crypto.stanford.edu/~nagendra/projects/dtls/dtls.html" rel="nofollow">DTLS</a> 的 project. [<a href="#footnote-link-2-301" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-3-301" class="footnote">Cisco 非該 hackathon 主辦單位, 僅在週末提供場地 [<a href="#footnote-link-3-301" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-4-301" class="footnote">很可惜後來才開始 blog, 否則應該會更清楚紀錄這些有趣的事情. [<a href="#footnote-link-4-301" class="footnote-link footnote-back-link">↩</a>]</li></ol>]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/10/05/retro-former-job/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Stroustrup &#038; Sutter on C++]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/410926355/" />
		<id>http://fsfoundry.org/codefreak/2008/10/03/stroustrup-sutter-on-c/</id>
		<updated>2008-10-04T07:13:02Z</updated>
		<published>2008-10-03T04:02:24Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[超想去聽的啦 - Stroustrup &#038; Sutter on C++ 2008, Second Showing: October 30-31, 2008, in Boston, MA, USA:
Wednesday, October 29, 2008
C++0x Overview (Bjarne Stroustrup)
What Not to Code: Avoiding Bad Design Choices and Worse Implementations (Herb Sutter)
How to Design Good Interfaces (Bjarne Stroustrup)
How to Migrate C++ Code to the Manycore “Free Lunch” (Herb Sutter)
Grill the Experts: Ask [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/10/03/stroustrup-and-sutter-on-cpp/"><![CDATA[<p>超想去聽的啦 - <a href="http://herbsutter.wordpress.com/2008/10/01/stroustrup-sutter-on-c-2008-second-showing-october-30-31-2008-in-boston-ma-usa/" rel="nofollow">Stroustrup &#038; Sutter on C++ 2008, Second Showing: October 30-31, 2008, in Boston, MA, USA</a>:</p>
<blockquote><p>Wednesday, October 29, 2008</p>
<p>C++0x Overview (Bjarne Stroustrup)</p>
<p>What Not to Code: Avoiding Bad Design Choices and Worse Implementations (Herb Sutter)</p>
<p>How to Design Good Interfaces (Bjarne Stroustrup)</p>
<p>How to Migrate C++ Code to the Manycore “Free Lunch” (Herb Sutter)</p>
<p>Grill the Experts: Ask Us Anything! (Bjarne Stroustrup &#038; Herb Sutter)</p>
<p>Thursday, October 30, 2008</p>
<p>[&#8221;Best of Stroustrup &#038; Sutter&#8221;] Update of talk voted “Most Informative” at S&#038;S 2007: Concepts and Generic Programming in C++0x (Bjarne Stroustrup)</p>
<p>Safe Locking: Best Practices to Eliminate Race Conditions (Herb Sutter)</p>
<p>C++ in Safety-Critical Systems (Bjarne Stroustrup)</p>
<p>Lock-Free Programming in C++—or How to Juggle Razor Blades (Herb Sutter)</p>
<p>Discussion on Questions Raised During the Seminar (Herb Sutter &#038; Bjarne Stroustrup)</p></blockquote>
<p><a href="http://sdbestpractices.com/index.php?option=com_content&#038;task=view&#038;id=45&#038;Itemid=114" rel="nofollow">Software Development Best Practice</a> 跟 <a href="http://techinsightsevents.com/web/escb/registration" rel="nofollow">Embedded Systems Conference Boston</a> 的套票好像說好似的都一樣要價 US$2795. </p>
<p>尤其是 <a href="https://www.cmpevents.com/SDe8/a.asp?option=C&#038;V=1&#038;SL=2&#038;CP1=155&#038;scTKs=1228" rel="nofollow">Software Development Best Practice 的 C++ Track</a>, 看起來非常值得期待. 只是這 US$2795&#8230; 可是會讓老婆餓肚子的呢.</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/10/03/stroustrup-and-sutter-on-cpp/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[A Productive Day]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/404678474/" />
		<id>http://fsfoundry.org/codefreak/2008/09/27/a-productive-day/</id>
		<updated>2008-09-28T04:04:06Z</updated>
		<published>2008-09-27T14:02:39Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="security" />		<summary type="html"><![CDATA[今天真是多產的一天啊, 連這篇也算的話今天已經寫了四篇文字. (文字的內容與品質則是另外一回事) 這該是個人最高紀錄了吧! 這代表了一件事情 - 我實在是太不務正業了!
昨晚開始拜讀 Rootkits, 加上今天上廁所的時候看了幾頁, 目前進度只有二十來頁. 怎麼說這都是我現階段要拿來吃飯的傢伙, 不能擺爛啊. 只是手邊積了幾篇寫了一半的文字, 不把它們寫一寫, get them out of the system, 老是覺得不是很自在.
這本書作者的寫作功力跟技術實力都很強, 雖然主題與我傳統上的興趣沒什麼相干, 但目前為止讀起來很好玩. 看來我很可能會上鉤, 以後又多個題目玩耍了.
拼一下看睡前能寫多少算多少, 明天繼續 root.
]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/27/a-productive-day/"><![CDATA[<p>今天真是多產的一天啊, 連這篇也算的話今天已經寫了四篇文字. (文字的內容與品質則是另外一回事) 這該是個人最高紀錄了吧! 這代表了一件事情 - 我實在是太不務正業了!</p>
<p>昨晚開始拜讀 <a href="http://ppolis.tw/spec_item.action?sid=127401300010162205&#038;userItemSid=82378&#038;added%3A1&#038;editUserItem%3A1" rel="nofollow">Rootkits</a>, 加上今天上廁所的時候看了幾頁, 目前進度只有二十來頁. 怎麼說這都是我現階段要拿來吃飯的傢伙, 不能擺爛啊. 只是手邊積了幾篇寫了一半的文字, 不把它們寫一寫, get them out of the system, 老是覺得不是很自在.</p>
<p>這本書作者的寫作功力跟技術實力都很強, 雖然主題與我傳統上的興趣沒什麼相干, 但目前為止讀起來很好玩. 看來我很可能會上鉤, 以後又多個題目玩耍了.</p>
<p>拼一下看睡前能寫多少算多少, 明天繼續 root.</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/27/a-productive-day/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[XP 改. 中文版變英文版]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/404631314/" />
		<id>http://fsfoundry.org/codefreak/2008/09/27/xp-chinese-to-english/</id>
		<updated>2008-09-27T13:01:18Z</updated>
		<published>2008-09-27T12:29:44Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[不是我中文真那麼糟, 也不是我英文超流利. 實在是很多中文化的技術辭彙讓我不敢恭維, 排序的時候也不像英文那般一目了然. 即便買的 NB 上總有 不要也不行的 隨機器附的 Win XP 中文版, 我這幾年都是當錢丟到水裡, 用個 GNU/Linux 的 distro (通常是 Ubuntu) 直接蓋過去, 自然是以個人偏好的英文為主要介面.
還記得幾年前的我還會先灌個英文版的 XP 然後做成 XP/Linux dual boot. (現在都是用 VM) 可是這個時候機車的事就來了, 雖然是 不要也不行的 隨機器附的 中文版 XP, 但畢竟我錢也沒少付. 偏偏這隨機器附的序號就是不能用在自己灌的英文版 XP 上. 於是我做了法律所不允許的事情, 把錢匯到了國外. Oh 不&#8230; 是不顧 Microsoft 再賺我一次的權力.
剛看了 阿駕零零壹 © 學習筆記 才知道原來還有 這招 啊, 直接把中文版的 XP 介面改為英文. 這樣我就可以合法 [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/27/xp-chinese-to-english/"><![CDATA[<p>不是我中文真那麼糟, 也不是我英文超流利. 實在是很多中文化的技術辭彙讓我不敢恭維, 排序的時候也不像英文那般一目了然. 即便買的 NB 上總有 <del>不要也不行的</del> <ins>隨機器附的</ins> Win XP 中文版, 我這幾年都是當錢丟到水裡, 用個 GNU/Linux 的 distro (通常是 Ubuntu) 直接蓋過去, 自然是以個人偏好的英文為主要介面.</p>
<p>還記得幾年前的我還會先灌個英文版的 XP 然後做成 XP/Linux dual boot. (現在都是用 VM) 可是這個時候機車的事就來了, 雖然是 <del>不要也不行的</del> <ins>隨機器附的</ins> 中文版 XP, 但畢竟我錢也沒少付. 偏偏這隨機器附的序號就是不能用在自己灌的英文版 XP 上. 於是我做了法律所不允許的事情, 把錢匯到了國外. Oh 不&#8230; 是不顧 Microsoft 再賺我一次的權力.</p>
<p>剛看了 <a href="http://twntwn.info/blog/ajer001" rel="nofollow">阿駕零零壹 © 學習筆記</a> 才知道原來還有 <a href="http://twntwn.info/blog/ajer001/archives/2691">這招</a> 啊, 直接把中文版的 XP 介面改為英文. 這樣我就可以合法 (?) 地 dual boot 了. Dual boot 雖然沒 VM 好用, 但至少可以讓我心裡舒服點.</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/27/xp-chinese-to-english/</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://feeds.feedburner.com/~r/codefreak/~3/404556723/" />
		<id>http://fsfoundry.org/codefreak/2008/09/27/where-are-you/</id>
		<updated>2008-09-29T14:43:45Z</updated>
		<published>2008-09-27T09:54:49Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="view" /><category scheme="http://fsfoundry.org/codefreak" term="whining" />		<summary type="html"><![CDATA[在 Better Blog Search 的過程中讓我看到一個 interesting (但一點也不有趣) 的現象. 雖然 Sutter&#8217;s Mill 是今年四月才從 Windows Live 的舊址搬到 Wordpress.1 至今五個多月以來, 竟然只有一個部落格以中文寫過相關文字. 而教我不知道該高興還是苦笑的是, 這個部落格不是別人, 正是這裡 - 小弟的 COdE fr3@K.

從網路上的相關 activity (各大 mailing lists, FOSS projects, Usenet Groups and etc.) 看來, C++ 還是活得很好, 不但一直在往前走 (C++0x, boost and etc.) 也有健康的使用者社群. I can&#8217;t help wondering - 台灣的 (or Chinese in general) C++ [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/27/where-are-you/"><![CDATA[<p>在 <a href="http://fsfoundry.org/codefreak/2008/09/27/better-blog-search/">Better Blog Search</a> 的過程中讓我看到一個 interesting (但一點也不有趣) 的現象. 雖然 <a href="http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!917.entry" rel="nofollow">Sutter&#8217;s Mill 是今年四月才從 Windows Live 的舊址搬到 Wordpress</a>.<sup><a href="#footnote-1-296" id="footnote-link-1-296" class="footnote-link footnote-identifier-link" title="Sutter&#8217;s Mill 新址">1</a></sup> 至今五個多月以來, 竟然只有一個部落格以中文寫過相關文字. 而教我不知道該高興還是苦笑的是, 這個部落格不是別人, 正是這裡 - 小弟的 COdE fr3@K.<br />
<span id="more-296"></span><br />
從網路上的相關 activity (各大 mailing lists, FOSS projects, Usenet Groups and etc.) 看來, C++ 還是活得很好, 不但一直在往前走 (C++0x, boost and etc.) 也有健康的使用者社群. I can&#8217;t help wondering - 台灣的 (or Chinese in general) C++ 社群怎麼了?</p>
<p>不要跟我說台灣有這種<del>幾乎沒有專業品質可言</del><ins>疑似初學者社群</ins>的 <a href="http://www.programmer-club.com/pc2020v5/forum/forumN.asp?board_pc2020=c" rel="nofollow">程式設計愛好者的天堂</a>, 更別提那種<ins>少部份</ins><del>不長進</del>學生跪求作業的討論群. 熱愛 C++ 的台灣人, 你們在哪裡?</p>
<hr/>
用這唯一的指標來論斷台灣的 C++ 社群的狀況絕對是不夠客觀的, 但我相信很多人心裡明瞭, 現狀並沒有很好. 反觀 <a href="http://fsfoundry.org/codefreak/2007/11/03/multiple-delete/">大陸的社群狀況</a> (IMHO) 倒是比寶島熱絡多了.</p>
<p>[Update Sep 29th, 2008 at 22:30]<br />
我想我該再說明白些. 撇開因為工作認識的與其他的不說, 至少藉著這個 blog 我也多知道了好幾位 C++ 的能人, 自然也知道台灣不是沒人了. (人夠不夠又是另一回事了) 只是若以 community 的角度來看, 台灣的狀況似乎真的不樂觀.</p>
<p>感謝 jeffhung 的回應, 我會去 cpp-tw 坐坐的.</p>
<ol start="1" class="footnotes"><li id="footnote-1-296" class="footnote"><a href="http://herbsutter.wordpress.com/">Sutter&#8217;s Mill 新址</a> [<a href="#footnote-link-1-296" class="footnote-link footnote-back-link">↩</a>]</li></ol>]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/27/where-are-you/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Better Blog Search]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/404540313/" />
		<id>http://fsfoundry.org/codefreak/2008/09/27/better-blog-search/</id>
		<updated>2008-09-27T10:01:23Z</updated>
		<published>2008-09-27T09:23:31Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="web" />		<summary type="html"><![CDATA[以往, 我會每隔幾周用一次 google 的 blog search 找與自己興趣相關的部落格. 雖然這個方法是可以找到不錯的部落格, 但效率不是很好. 找到的東西中, 有很多五四三的垃圾. 昨晚意外發現一個更好更有效的方法; Technorati.
首先鎖定一個自己喜歡的部落格, 用 Technorati 來查哪些其他的部落格引用/連結到這個部落格. 舉個例子, 我最感興趣的題目是 C++, 在幾位 C++ 仙級人物 (Guru) 中,1 我最推崇的是 Herb Sutter. 用 Technorati 一查, 果然發現 文中寫到/連結到他的 Sutter&#8217;s Mill 的部落格 不少都是我很感興趣的.2 簡單的方法挖到了許多好文章, 一股腦的又多 subscribe 了五六個部落格.
在我心目中, 唯一神級的 C++ 人物自然是 Bjarne Stroustrup. [↩]Sutter&#8217;s Mill, http://herbsutter.wordpress.com/ [↩]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/27/better-blog-search/"><![CDATA[<p>以往, 我會每隔幾周用一次 <a href="http://www.google.com/blogsearch" rel="nofollow">google 的 blog search</a> 找與自己興趣相關的部落格. 雖然這個方法是可以找到不錯的部落格, 但效率不是很好. 找到的東西中, 有很多五四三的垃圾. 昨晚意外發現一個更好更有效的方法; <a href="http://technorati.com/" rel="nofollow">Technorati</a>.</p>
<p>首先鎖定一個自己喜歡的部落格, 用 Technorati 來查哪些其他的部落格引用/連結到這個部落格. 舉個例子, 我最感興趣的題目是 C++, 在幾位 C++ 仙級人物 (Guru) 中,<sup><a href="#footnote-1-295" id="footnote-link-1-295" class="footnote-link footnote-identifier-link" title="在我心目中, 唯一神級的 C++ 人物自然是 Bjarne Stroustrup.">1</a></sup> 我最推崇的是 <a href="http://www.gotw.ca/">Herb Sutter</a>. 用 Technorati 一查, 果然發現 <a href="http://technorati.com/blogs/herbsutter.wordpress.com?reactions" rel="nofollow">文中寫到/連結到他的 Sutter&#8217;s Mill 的部落格</a> 不少都是我很感興趣的.<sup><a href="#footnote-2-295" id="footnote-link-2-295" class="footnote-link footnote-identifier-link" title="Sutter&#8217;s Mill, http://herbsutter.wordpress.com/">2</a></sup> 簡單的方法挖到了許多好文章, 一股腦的又多 subscribe 了五六個部落格.</p>
<ol start="1" class="footnotes"><li id="footnote-1-295" class="footnote">在我心目中, 唯一神級的 C++ 人物自然是 <a href="http://public.research.att.com/~bs/">Bjarne Stroustrup</a>. [<a href="#footnote-link-1-295" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-2-295" class="footnote"><a href="http://herbsutter.wordpress.com/">Sutter&#8217;s Mill</a>, http://herbsutter.wordpress.com/ [<a href="#footnote-link-2-295" class="footnote-link footnote-back-link">↩</a>]</li></ol>]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/27/better-blog-search/</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://feeds.feedburner.com/~r/codefreak/~3/402978574/" />
		<id>http://fsfoundry.org/codefreak/2008/09/26/importance-of-error-definition-in-document/</id>
		<updated>2008-09-27T07:32:40Z</updated>
		<published>2008-09-25T17:21:49Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="commentary" /><category scheme="http://fsfoundry.org/codefreak" term="error-handling" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="gnu-linux" /><category scheme="http://fsfoundry.org/codefreak" term="microsoft" />		<summary type="html"><![CDATA[這是一個我想寫很久了的主題, 由於我也搞不清楚的原因 delay 到這個時候. Anyway, here it goes&#8230;


因為 半路的留言, 我比平常仔細地看了 MSDN 對 fopen 的說明. 下面節錄其中與錯誤相關的部份:
&#8230; A null pointer value indicates an error&#8230;
See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.
&#8230; If an error occurs, the global variableerrno is set and may be used to get specific error information. For [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/26/importance-of-error-definition-in-document/"><![CDATA[<p>這是一個我想寫很久了的主題, 由於我也搞不清楚的原因 delay 到這個時候. Anyway, here it goes&#8230;<br />
<span id="more-294"></span><br />
<hr/>
因為 <a href="http://fsfoundry.org/codefreak/2008/09/15/security-crt-safer-than-standard-library/#comment-4656">半路的留言</a>, 我比平常仔細地看了 MSDN 對 <a href="http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx" rel="nofollow"><code>fopen</code></a> 的說明. 下面節錄其中與錯誤相關的部份:</p>
<blockquote><p>&#8230; A null pointer value indicates an error&#8230;</p></blockquote>
<blockquote><p>See <a href="http://msdn.microsoft.com/en-us/library/t3ayayh1.aspx" rel="nofollow">_doserrno, errno, _sys_errlist, and _sys_nerr</a> for more information on these, and other, error codes.</p></blockquote>
<blockquote><p>&#8230; If an error occurs, the global variableerrno is set and may be used to get specific error information. For further information, see <a href="http://msdn.microsoft.com/en-us/library/t3ayayh1.aspx" rel="nofollow">errno</a>&#8230;</p></blockquote>
<p>上面兩個 link 都是連到同一頁, 一個叫作 &#8220;errno, _doserrno, _sys_errlist, and _sys_nerr&#8221; 的頁面. 我想有點 sense 的 developer 這時候應該已經猜到了, 這頁面不過就是一個包含了一串標準 error code 列表的頁面, 但沒有 (標明哪些才是) <code>fopen</code> specific 的錯誤碼. 這是 Microsoft 給 developer 的文件.</p>
<p>我們再來看看 GNU/Linux 的說明文件, man page, 對同一個 function, <a href="http://linux.die.net/man/3/fopen" rel="nofollow"><code>fopen</code></a> 的錯誤說明:</p>
<blockquote><h3>Errors</h3>
<ul>
<strong><code>EINVAL</code></strong> The mode provided to fopen(), fdopen(), or freopen() was invalid.</p>
<p>The fopen(), fdopen() and freopen() functions may also fail and set errno for any of the errors specified for the routine malloc(3).</p>
<p>The fopen() function may also fail and set errno for any of the errors specified for the routine open(2).</ul>
</blockquote>
<p>它清楚的告訴 developer (即便是間接的), 當開檔的模式有錯誤時 <a href="http://linux.die.net/man/3/errno" rel="nofollow"><code>errno</code></a> 為 <code>EINVAL</code>. 記憶體請求失敗時 <code>errno</code> 為 <code>ENOMEM</code>.<sup><a href="#footnote-1-294" id="footnote-link-1-294" class="footnote-link footnote-identifier-link" title="這是有條件的, 詳見 malloc (3) 的 Notes section.">1</a></sup> 在 (<code>fopen</code> 底層的) <a href="http://linux.die.net/man/2/open" rel="nofollow"><code>open</code> (2)</a> 失敗時&#8230; 可能發生的錯誤還真不少, 有興趣的請自己看看.</p>
<p>總之, GNU/Linux 的 man page 把可能發生的錯誤以及其原因交代的清清楚楚. 可別小看這個說明, 它不但開了一扇窗給 developer 以利給使用者友善的提示訊息的機會, 更能在錯誤為非硬性錯誤的時候讓程式更有機會繼續 (自動) 走下一步. 例如當給 <code>fopen</code> 的 <code>mode</code> 為 <code>"r"</code>, 發生錯誤而 <code>errno</code> 為 <code>ENOENT</code>, 程式可以知道該檔不存在, 而嘗試讀取備用的檔案. 仔細看看 GNU/Linux 的 man page, 我相信大部份 developer 都能找到在特定應用下能有效被 handle 的錯誤.</p>
<p>不論 <a href="http://fsfoundry.org/codefreak/2008/07/06/google-forbids-use-of-exception-in-cpp/">選擇哪種 error reporting 機制</a>, 知道什麼能出錯是上層能正確 error handling 的最大要素. 很可惜, Microsoft 的 MSDN 顯然沒做好這工作.</p>
<p>晚安&#8230;</p>
<ol start="1" class="footnotes"><li id="footnote-1-294" class="footnote">這是有條件的, 詳見 <a href="http://linux.die.net/man/3/malloc" rel="nofollow"><code>malloc</code> (3)</a> 的 Notes section. [<a href="#footnote-link-1-294" class="footnote-link footnote-back-link">↩</a>]</li></ol>]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/26/importance-of-error-definition-in-document/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[What Are You Smoking?]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/394965351/" />
		<id>http://fsfoundry.org/codefreak/2008/09/17/what-are-you-smoking/</id>
		<updated>2008-09-17T16:50:06Z</updated>
		<published>2008-09-17T07:33:09Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="funny" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" />		<summary type="html"><![CDATA[A geeky friend of mine IM&#8217;ed me URL of an interesting blog post just now.
Though I haven&#8217;t had the chance to read through this post, I can&#8217;t help quoting his/her words on STL, so they could be shared by more people out there:
&#8230; when you first encounter it (ed: STL), makes you wonder what the [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/17/what-are-you-smoking/"><![CDATA[<p>A geeky friend of mine IM&#8217;ed me URL of an interesting blog post just now.</p>
<p>Though I haven&#8217;t had the chance to read through this post, I can&#8217;t help quoting his/her <a href="http://jalf.dk/blog/?p=83">words on STL</a>, so they could be shared by more people out there:</p>
<blockquote><p>&#8230; when you first encounter it (ed: STL), makes you wonder what the hell the designer was smoking. And once you&#8217;ve gotten used to it, you start wondering why other library designers don&#8217;t start smoking the same thing.</p></blockquote>
<p>Get me some of those. Somebody! Please!</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/17/what-are-you-smoking/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Microsoft Search SPAM is back]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/394147626/" />
		<id>http://fsfoundry.org/codefreak/2008/09/16/microsoft-search-spam-is-back/</id>
		<updated>2008-09-16T14:19:45Z</updated>
		<published>2008-09-16T11:56:32Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="FSfoundry" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="microsoft" /><category scheme="http://fsfoundry.org/codefreak" term="spam" /><category scheme="http://fsfoundry.org/codefreak" term="web" />		<summary type="html"><![CDATA[Here it comes again, referer spam from Microsoft&#8217;s live search engine is back. It managed to bypass the mod_rewrite rules I got from The Art of Web the last time I was spammed.
New rules (again, from The Art of Web) were installed last night. So far, the log spamming seems to be stopped.
Huge thanks to [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/16/microsoft-search-spam-is-back/"><![CDATA[<p>Here it comes again, <a href="http://en.wikipedia.org/wiki/Referer_spam" rel="nofollow">referer spam</a> from Microsoft&#8217;s live search engine is back. It managed to bypass the mod_rewrite rules I got from <a href="http://www.the-art-of-web.com/system/logs-qbhp/">The Art of Web</a> <a href="http://fsfoundry.org/codefreak/2008/06/10/live-ms-search-spam/">the last time I was spammed</a>.</p>
<p>New rules (again, from The Art of Web) were installed last night. So far, the log spamming seems to be stopped.</p>
<p>Huge thanks to people from <a href="http://www.the-art-of-web.com/system/logs-qbhp/" rel="nofollow">The Art of Web</a> for documenting this instance and updating their blocking method.</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/16/microsoft-search-spam-is-back/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Security Enhanced CRT, Safer Than Standard Library?]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/393007305/" />
		<id>http://fsfoundry.org/codefreak/2008/09/15/security-crt-a-false-sense-of-security/</id>
		<updated>2008-09-16T13:52:08Z</updated>
		<published>2008-09-14T16:48:41Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="commentary" /><category scheme="http://fsfoundry.org/codefreak" term="english" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="microsoft" /><category scheme="http://fsfoundry.org/codefreak" term="security" />		<summary type="html"><![CDATA[In a blog post Danny Kalev published earlier this year on InformIT, an example was presented demonstrating how one could write valid but insecure code involving vector and auto_ptr which compiles without any warning, despite other warnings Micorsoft&#8217;s recent compilers would&#8217;ve issued against standard compliant code. Together with other points he made in the post, [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/15/security-crt-safer-than-standard-library/"><![CDATA[<p>In a <a href="http://www.informit.com/blogs/blog.aspx?uk=Theyre-at-it-again" rel="nofollow">blog post</a> Danny Kalev published earlier this year on InformIT, an example was presented demonstrating how one could write valid but insecure code involving <code>vector</code> and <code>auto_ptr</code> which compiles without any warning, despite other warnings Micorsoft&#8217;s recent compilers would&#8217;ve issued against standard compliant code. Together with other points he made in the post, Danny suggests Micorsoft doesn&#8217;t really care about your code safety. I couldn&#8217;t have agreed with him more, and would like to contribute my own analysis in support of Danny&#8217;s finding.<br />
<span id="more-287"></span><table align="center">
<tr>
<td>
<script type="text/javascript"><!--
google_ad_client = "pub-9597535005741556";
/* 468x60, 2008/6/13 */
google_ad_slot = "6320497412";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>
</table>
<br />Most of us are very well aware of the often-exploited security vulnerability in <a href="http://msdn.microsoft.com/en-us/library/kk6xf663.aspx" rel="nofollow"><code>strcpy</code></a>, and Microsoft&#8217;s answer to it is <a href="http://msdn.microsoft.com/en-us/library/td1esda9(VS.80).aspx" rel="nofollow"><code>strcpy_s</code></a>, which is said to be a more secure alternative that takes an additional parameter, <code>numberOfElements</code> as they called it:</p>
<pre>char *strcpy(
   char *strDestination,
   const char *strSource
);
errno_t strcpy_s(
   char *strDestination,
   size_t <strong>numberOfElements</strong>,
   const char *strSource
);</pre>
<p>But wait, doesn&#8217;t <code>strcpy_s</code> look awfully similar to another <a href="http://msdn.microsoft.com/en-us/library/xdsywd25.aspx" rel="nofollow">standard function</a>?</p>
<pre>char *strncpy(
   char *strDest,
   const char *strSource,
   size_t <strong>count</strong>
);</pre>
<p>Put aside the subtle differences in types of return value and parameter naming/ordering, and the insignificant behavioral difference (in regard to <a href="http://en.wikipedia.org/wiki/Buffer_overflow" rel="nofollow">buffer overruns</a>) when <code>numberOfElements</code>/<code>count</code> is too small. They are <em>effectively the same</em>.</p>
<p>Yes, I get the semantical differentiation Micorsoft has been trying to sell us programmers. But I failed to understand how is <code>strcpy_s</code> more secure than <code>strncpy</code> <em>syntactically</em>.</p>
<p>Wouldn&#8217;t it make more sense if compiler vendors would educate programmers (a.k.a. their customers) the habit of using <code>strncpy</code> instead of <code>strcpy</code>, and use <code>count</code> as the size of the memory buffer <code>strDest</code> points to, for security reasons. But, no, Microsoft had to tell programmers to embrace their proprietary <a href="http://msdn.microsoft.com/en-us/library/wd3wzwts.aspx" rel="nofollow">Security Enhanced CRT functions</a> and hint, in a <a href="http://msdn.microsoft.com/en-us/library/xdsywd25.aspx" rel="nofollow">security note</a>, that we can&#8217;t use <code>strncpy</code> safely:</p>
<blockquote><p>strncpy does not check for sufficient space in strDest; it is therefore a potential cause of buffer overruns. Keep in mind that count limits the number of characters copied; it is not a limit on the size of strDest.</p></blockquote>
<p>If the parameter <code>count</code> couldn&#8217;t buy <code>strncpy</code> security, I just don&#8217;t see how <code>strcpy_s</code> could have made the cut with <code>numberOfElements</code>.</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/15/security-crt-safer-than-standard-library/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[How to Do It Right? Wide and Multi-Byte String Conversion]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/392183392/" />
		<id>http://fsfoundry.org/codefreak/2008/09/14/how-to-do-it-right-wide-and-multi-byte-string-conversion/</id>
		<updated>2008-09-14T09:24:44Z</updated>
		<published>2008-09-14T09:17:47Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="unicode" />		<summary type="html"><![CDATA[一方面是受到 jeffhung 與  augustinus 在 我家 與 clsung 那邊留下的迴響所啟發, 令一方面也是為了讓自己更清楚了解正確 wide string and multi-byte string 間轉換的 issue. 我寫了兩個 Wide string and Multi-Bytes string conversion functions 以及方便性的 wrapper (source code hosted by Google Code).
希望能做為一個範例提供給想進一步了解這題目的朋友 (this includes me) 把玩. 也希望能請對這題目有研究的朋友來 code review 以及留下意見, 幫助讓我完善這對 conversion functions 一起造福有這需求的使用者.
Disclaimer: 小弟對實作所使用的 API (wcrtomb 與 mbrtowc) 的行為不是非常肯定, 不敢宣稱其功能上的正確. 歡迎自行取用 (MIT-licensed), [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/14/how-to-do-it-right-wide-and-multi-byte-string-conversion/"><![CDATA[<p>一方面是受到 jeffhung 與  augustinus 在 <a href="http://fsfoundry.org/codefreak/2008/09/03/re-string-tofrom-wstring-conversion/#comment-4614">我家</a> 與 <a href="http://blog.dragon2.net/2008/08/29/551.php" rel="nofollow">clsung</a> 那邊留下的迴響所啟發, 令一方面也是為了讓自己更清楚了解正確 wide string and multi-byte string 間轉換的 issue. 我寫了兩個 <strong>W</strong>ide string and <strong>M</strong>ulti-<strong>B</strong>ytes string conversion functions 以及方便性的 wrapper (<a href="http://code.google.com/p/codefreak/source/browse/trunk/wmb/" rel="nofollow">source code</a> hosted by <a href="http://code.google.com/" rel="nofollow">Google Code</a>).</p>
<p>希望能做為一個範例提供給想進一步了解這題目的朋友 (this includes me) 把玩. 也希望能請對這題目有研究的朋友來 code review 以及留下意見, 幫助讓我完善這對 conversion functions 一起造福有這需求的使用者.</p>
<p>Disclaimer: 小弟對實作所使用的 API (<code>wcrtomb</code> 與 <code>mbrtowc</code>) 的行為不是非常肯定, 不敢宣稱其功能上的正確. 歡迎自行取用 (MIT-licensed), 但不提供任何保證.</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/14/how-to-do-it-right-wide-and-multi-byte-string-conversion/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[re: string to/from wstring conversion]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/382134125/" />
		<id>http://fsfoundry.org/codefreak/2008/09/03/re-string-tofrom-wstring-conversion/</id>
		<updated>2008-09-03T11:58:32Z</updated>
		<published>2008-09-03T08:22:55Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><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="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="unicode" />		<summary type="html"><![CDATA[本篇文字的內容本來是我寫給 clsung 的一篇文字 的第二個回應. 但不幸的是我的回應被 anti-spam 機制吃掉了, 並提示我 email 給站長處理. 那時 submit 的文字我是撈不回來了, 於是決定重寫並貼在自己家裡.


撇開 一個 wchar_t 不能一對一對應到一個 Unicode code point 的問題 不談. 這個回應要 focus 的是 STL 的正確使用.
文中的第一個 function, narrow to wide 的 string conversion, 是這樣寫的:

std::wstring s2ws(const std::string&#038; s)
{
  std::wstring temp(s.length(),L‘ ‘);
  std::copy(s.begin(), s.end(), temp.begin());
  return temp;
}
上面的版本從執行結果來說並沒有錯, 但更正確的應該要這樣寫:

std::wstring s2ws(const std::string&#038; s)
{
  return [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/03/re-string-tofrom-wstring-conversion/"><![CDATA[<p>本篇文字的內容本來是我寫給 <a href="http://blog.dragon2.net/2008/08/29/551.php" rel="nofollow">clsung 的一篇文字</a> 的第二個回應. 但不幸的是我的回應被 anti-spam 機制吃掉了, 並提示我 email 給站長處理. 那時 submit 的文字我是撈不回來了, 於是決定重寫並貼在自己家裡.<br />
<span id="more-284"></span><br />
<hr />
<p>撇開 <a href="http://fsfoundry.org/codefreak/2008/09/03/more-on-win32-unicoding/" title="{nolb}">一個 wchar_t 不能一對一對應到一個 Unicode code point 的問題</a> 不談. 這個回應要 focus 的是 STL 的正確使用.</p>
<p>文中的第一個 function, narrow to wide 的 string conversion, 是這樣寫的:</p>
<pre>
std::wstring s2ws(const std::string&#038; s)
{
  std::wstring temp(s.length(),L‘ ‘);
  std::copy(s.begin(), s.end(), temp.begin());
  return temp;
}</pre>
<p>上面的版本從執行結果來說並沒有錯, 但更正確的應該要這樣寫:</p>
<pre>
std::wstring s2ws(const std::string&#038; s)
{
  return std::wstring(s.begin(), s.end());
}</pre>
<p>因為:</p>
<ol>
<li>更簡潔的代碼</li>
<li>一樣沒有多次 allocation 的問題</li>
<p>雖然我沒注意過 Standard 是否有相關規範, 任何自重的 STL implementation 都會在 iterator 為 <a href="http://www.sgi.com/tech/stl/ForwardIterator.html" rel="nofollow">Forward Iterator</a> (含 <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html" rel="nofollow">Random Access Iterator</a>) 時, 先算 first 到 last 的 distance 然後一次 allocate 足.</p>
<li>更快速</li>
<p>原版本的 function body 內先是做 N 次 copy-construct (第一行), 然後再做 N 次 assignment (第二行). 第二個的版本只做 N 次 copy-construct.</p>
<li>更加適合 compiler 進行最佳化</li>
<p>請參考 <a href="http://www.cs.cmu.edu/~gilpin/c++/performance.html#returnvalue" rel="nofollow">Return</a> <a href="http://www.informit.com/guides/content.aspx?g=cplusplus&#038;seqNum=199" rel="nofollow">Value</a> <a href="http://www.tantalon.com/pete/cppopt/final.htm#AvoidTemporaries" rel="nofollow">Optimization</a> (RVO)</p>
<p><em>Note: 對最佳化有興趣的朋友請自行 google 另一個用途更廣泛但對 compiler 而言難度較高的最佳化 - <a href="http://www.google.com/search?q=named+return+value+optimization">Named Return Value Optimization (NRVO)</a>.</em>
</ol>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/03/re-string-tofrom-wstring-conversion/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[More on WIN32 Unicoding]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/382105582/" />
		<id>http://fsfoundry.org/codefreak/2008/09/03/more-on-win32-unicoding/</id>
		<updated>2008-09-03T14:48:05Z</updated>
		<published>2008-09-03T05:33:27Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="cited" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="unicode" />		<summary type="html"><![CDATA[Follow up 一下去年底沒給出結論的 WIN32 的 _TCHAR 與 std::wstring 的問題. From MSDN:
Microsoft’s C/C++ compiler defines a built-in data type, wchar_t, which represents a 16-bit Unicode (UTF-16) character.
雖然我沒在 MSDN 上找到, 據一位 MS International Fundamentals team 的 developer, Michael Kaplan, 所 blog - 從 XP 開始, WIN32 的 wchar_t 用的 encoding 是 UTF-16. 之前用的則是&#8230; 一種不理會 surrogate (原文為 surrogate [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/09/03/more-on-win32-unicoding/"><![CDATA[<p>Follow up 一下去年底沒給出結論的 <a href="http://fsfoundry.org/codefreak/2007/11/20/problem-with-win32-tchar-and-std-wstring/">WIN32 的 _TCHAR 與 std::wstring 的問題</a>. From <a href="http://msdn.microsoft.com/en-us/library/cc500319.aspx" rel="nofollow">MSDN</a>:</p>
<blockquote><p>Microsoft’s C/C++ compiler defines a built-in data type, wchar_t, which represents a 16-bit Unicode (UTF-16) character.</p></blockquote>
<p>雖然我沒在 MSDN 上找到, 據一位 MS International Fundamentals team 的 developer, <a href="http://blogs.msdn.com/michkap/default.aspx" rel="nofollow">Michael Kaplan</a>, 所 <a href="http://blogs.msdn.com/michkap/archive/2005/05/11/416552.aspx" rel="nofollow">blog</a> - 從 XP 開始, WIN32 的 <code>wchar_t</code> 用的 encoding 是 UTF-16. 之前用的則是&#8230; 一種不理會 surrogate (原文為 surrogate neutral) 的 UTF-16 (也因此有人說它是 UCS-2).</p>
<p>結論, 從 XP (含) 以後, WIN32 的 <code>wchar_t</code> 用的 encoding 是標準的 UTF-16. 假設每一個 <code>wchar_t</code> 元素都能一對一地 mapping 到一個 Unicode code point 是錯誤的. 另一個說法是 <code>std::wstring::size()</code> 只能告訴你這個寬字串有幾個 <code>wchar_t</code>, 而不能正確的告訴你到底有幾個 code point.</p>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/09/03/more-on-win32-unicoding/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Exception Handling 新思維, Using Guards]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/379625346/" />
		<id>http://fsfoundry.org/codefreak/2008/08/31/exception-handling-%e6%96%b0%e6%80%9d%e7%b6%ad-using-gaurds/</id>
		<updated>2008-10-02T09:39:28Z</updated>
		<published>2008-08-31T12:41:50Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><category scheme="http://fsfoundry.org/codefreak" term="error-handling" /><category scheme="http://fsfoundry.org/codefreak" term="exception" /><category scheme="http://fsfoundry.org/codefreak" term="geeky" /><category scheme="http://fsfoundry.org/codefreak" term="tutorial" />		<summary type="html"><![CDATA[續自 Exception Handling 新思維.
Preface
取材來說明如何以某種技巧寫出俱備特定目的的 code 有時不是一件容易的事. 我讀過好幾本這樣的 programming 書籍, 即便書中用了不小的篇幅來說明範例相關的 domain/business logic, 可是我依然無法感同身受 (當然也有可能是我太笨). 但當要說明的目的是 exception safety, 那就簡單多了. 以最多 C++ programmer 熟悉的 STL 為例, 應該不會錯到哪裡去.
本文選的題目是 STL 的 uninitialized_fill_n. 雖然這個題目可能同時也是不錯的 template programming 教材, 但是為了避免失焦, 這篇文字將假設讀者對文中使用到的 template programming 技巧以及 STL 有足夠的熟悉度, 並略過部份與 exception safety 無關的細節. 另外, 為方便排版, 文中 code snippet (代碼片斷) 可能不同於引用的來源, 兩者在文中的 context 應為等效.
這篇文字要從 library implementor [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/08/31/a-new-mind-set-for-exception-handling-using-guards/"><![CDATA[<p><em>續自 <a href="http://fsfoundry.org/codefreak/2008/08/18/a-new-mind-set-for-exception-handling/">Exception Handling 新思維</a>.</em></p>
<h3>Preface</h3>
<p>取材來說明如何以某種技巧寫出俱備特定目的的 code 有時不是一件容易的事. 我讀過好幾本這樣的 programming 書籍, 即便書中用了不小的篇幅來說明範例相關的 domain/business logic, 可是我依然無法感同身受 (當然也有可能是我太笨). 但當要說明的目的是 exception safety, 那就簡單多了. 以最多 C++ programmer 熟悉的 STL 為例, 應該不會錯到哪裡去.</p>
<p>本文選的題目是 STL 的 <code><a href="http://www.kuzbass.ru:8086/docs/isocpp/lib-utilities.html#lib.uninitialized.fill.n" rel="nowfollow">uninitialized_fill_n</a></code>. 雖然這個題目可能同時也是不錯的 template programming 教材, 但是為了避免失焦, 這篇文字將假設讀者對文中使用到的 template programming 技巧以及 STL 有足夠的熟悉度, 並略過部份與 exception safety 無關的細節. 另外, 為方便排版, 文中 code snippet (代碼片斷) 可能不同於引用的來源, 兩者在文中的 context 應為等效.</p>
<p>這篇文字要從 library implementor 的角度出發, 拿 STL 當題材, 探討不同手法的 implementation 俱備的特性.<br />
<span id="more-280"></span><!--adsense--><br />
<h3>lib.specialized.fill.n</h3>
<p>根據 <a href="http://www.kuzbass.ru:8086/docs/isocpp/" rel="nofollow">C++98 Final Draft</a> 表示, 它的效果 (effects) 等同以下 code snippet:</p>
<table>
<tr>
<td id="listing-1">
<pre>
<span style="color: #66cdaa;">// </span><span style="color: #66cdaa;"><a href="#listing-1">Listing 1</a>, effects</span>
<span style="color: #6495ed; font-weight: bold;">template</span> &lt;<span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">ForwardIterator</span>, <span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">Size</span>, <span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">T</span>&gt;
<span style="color: #9290ff;">void</span>
  <span style="color: #87ceeb;">uninitialized_fill_n</span>(<span style="color: #9290ff;">ForwardIterator</span> <span style="color: #98fb98;">first</span>,
                       <span style="color: #9290ff;">Size</span> <span style="color: #98fb98;">n</span>,
                       <span style="color: #6495ed; font-weight: bold;">const</span> <span style="color: #9290ff;">T</span>&amp; <span style="color: #98fb98;">x</span>)
{
  <span style="color: #6495ed; font-weight: bold;">typedef</span>
    <span style="color: #6495ed; font-weight: bold;">typename</span> <span style="color: #9932cc; font-weight: bold;">iterator_traits</span>&lt;<span style="color: #9290ff;">ForwardIterator</span>&gt;::<span style="color: #9290ff;">value_type</span>
      <span style="color: #9290ff;">value_type</span>;

  <span style="color: #6495ed; font-weight: bold;">for</span> (; <code>--n</code>; ++first)
    <span style="color: #6495ed; font-weight: bold;">new</span> (&amp;*first) <span style="color: #9290ff;">value_type</span>(x);
}</pre>
</td>
</tr>
</table>
<p>用白話來說, 這段 code 做的就是在 <code>(first, first + n]</code> 這個 range 迭代, 順序對 <code>n</code> 個 <strong>還沒被 construct 的 element</strong> 在記憶體的位置做 <a href="http://www.informit.com/guides/content.aspx?g=cplusplus&#038;seqNum=304" rel="nofollow">placement new</a> (又稱為 in-place construction).<sup><a href="#footnote-1-280" id="footnote-link-1-280" class="footnote-link footnote-identifier-link" title="不同於其他的 new expression 會先動態請求 instance 所需要的 memory footprint 然後再調用 class specific constructor. Placement new 是 C++ 支援的一種就地創建 instance 的方法. 在使用者指定的位置直接調用 constructor">1</a></sup></p>
<p>這段 code 只能用來表示其效果, 不能拿來當作一個 reference implementation. 因為 <code>uninitialized_fill_n</code> 被 <a href="http://www.kuzbass.ru:8086/docs/isocpp/lib-utilities.html#lib.specialized.algorithms" rel="nofollow">lib.specialized.algorithms</a> 所規範, 其中一個 requirement 是要支援 commit-or-rollback 的語意:</p>
<blockquote><p>&#8230; if an exception is thrown there are no effects.</p></blockquote>
<p>這個 requirement 的緣由很重要. 當建構使用者指定 range 中的某 element 拋出 exception 時, 使用者無法知道到底是那一個 element 的 construction 失敗了. 沒有辦法正確的 destroy 已經成功建構的 element.</p>
<h3>Commit-or-rollback via Try-Catch</h3>
<p>前面用來表示 effects 的 code 沒有實現 rollback 的部份, 因此並不符合規範. 一個常見的實現 commit-or-rollback 的手法是以 try-catch 來實現:</p>
<table>
<tr>
<td id="listing-2">
<pre>
<span style="color: #66cdaa;">// </span><span style="color: #66cdaa;"><a href="#listing-2">Listing 2</a>, using try-catch</span>
<span style="color: #6495ed; font-weight: bold;">template</span> &lt;<span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">ForwardIterator</span>, <span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">Size</span>, <span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">T</span>&gt;
<span style="color: #9290ff;">void</span>
  <span style="color: #87ceeb;">uninitialized_fill_n</span>(<span style="color: #9290ff;">ForwardIterator</span> <span style="color: #98fb98;">first</span>,
                       <span style="color: #9290ff;">Size</span> <span style="color: #98fb98;">n</span>,
                       <span style="color: #6495ed; font-weight: bold;">const</span> <span style="color: #9290ff;">T</span>&amp; <span style="color: #98fb98;">x</span>)
{
  <span style="color: #6495ed; font-weight: bold;">typedef</span>
    <span style="color: #6495ed; font-weight: bold;">typename</span> <span style="color: #9932cc; font-weight: bold;">iterator_traits</span>&lt;<span style="color: #9290ff;">ForwardIterator</span>&gt;::<span style="color: #9290ff;">value_type</span>
      <span style="color: #9290ff;">value_type</span>;

  <span style="color: #9290ff;">ForwardIterator</span> <span style="color: #98fb98;">iter</span> = first;
  <span style="color: #6495ed; font-weight: bold;">try</span>
  {
    <span style="color: #6495ed; font-weight: bold;">for</span> (; <code>--n</code>; ++iter)
      <span style="color: #6495ed; font-weight: bold;">new</span> (&amp;*iter) <span style="color: #9290ff;">value_type</span>(x);
  }
  <span style="color: #6495ed; font-weight: bold;">catch</span>(<code>...</code>)
  {
    <span style="color: #6495ed; font-weight: bold;">for</span> (; first != iter; ++first)
      (&amp;*first)-&gt;~value_type();
  }
}</pre>
</td>
</tr>
</table>
<p>In practice, 我所知道的 STL implementation 都用這個方法實現. 可是這個方法是最好的嗎? 我認為不是.</p>
<h3>Issues with Try-Catch</h3>
<p>即便在沒有 exception 發生的 code path, try-catch 在運行時期, 在速度與記憶體使用方面, 可能會引起對某些特殊應用而言無法接受的 overhead. (depending on the compiler implementation used to build the application)</p>
<p>撇開 overhead 不談.<sup><a href="#footnote-2-280" id="footnote-link-2-280" class="footnote-link footnote-identifier-link" title="其實我很不想談 C++ exception 所引發的 overhead. 在我的認知那多是 C++ runtime 實現的問題. 這部份可以有很大的進步空間. 見 C++ in the Linux Kernel. 不幸的是這個 project (的網頁) 似乎已經下線了, 請參考 Wayback Machine 上的 2007 年底的頁面">2</a></sup> Try-catch 不能在最大範圍有效應付除了 C++ exception 之外的其他 vendor-specific exception 機制.<sup><a href="#footnote-3-280" id="footnote-link-3-280" class="footnote-link footnote-identifier-link" title="如在 MSVC 上用了 /EHsc 這個 option 時, catch(...) clause 抓不到 SEH">3</a></sup> 在 Windows 平台上, C++ 的 exception 是建構在另一種更低階, low overhead 的非標準 exception 機制之上 - <a href="http://msdn.microsoft.com/en-us/library/ms680657(VS.85).aspx" rel="nofollow">SEH</a>. 雖然我自己很可能不會這樣幹, 但我完全能夠想像某些 performance critical 的 C++ project 無法接受 C++ exception 在 runtime 時的 overhead (尤其是跑在 kernel mode 的 code. e.g. drivers), 而希望全面或部份採用 SEH, 以取代 C++ exception 做為 exceptional condition 的回報機制.<sup><a href="#footnote-4-280" id="footnote-link-4-280" class="footnote-link footnote-identifier-link" title="可能有人會問, 如果無法接受 C++ exception 的 overhead 為何不乾脆 像 Google 一樣完全禁止 exception? 因為他們可能跟我一樣, 認為 寫沒有 exception 的 C++ 實在是太折磨人了">4</a></sup> 我認為這是或許是 C++ 語言在 kernel 應用的最大阻礙之一 (另一個明顯的大石頭是沒有 STL). 讓我好奇的是這問題與沒有任何較完整的 STL implementation (宣稱) 支持 Windows kernel 這個現象之間的關係.<sup><a href="#footnote-5-280" id="footnote-link-5-280" class="footnote-link footnote-identifier-link" title="就我所知, 目前只有 Open NTL (disclaimer: 筆者參與的 Free Software project 之一), 擁有一份尚未成熟, 正邁向符合 C++0x 標準規範 支持 Windows kernel 的 STL implementation. 本文的 range_construction_guard 與 uninitialized_fill_n 的實現皆取材自 Open NTL">5</a></sup></p>
<p>最後一個讓人困擾的是在稍微複雜一點的狀況下 - 由於 try-catch 是以巢狀的方式表現 - <a href="http://fsfoundry.org/codefreak/2008/04/29/strong-guarantee-using-transaction/#listing-7">rollback 的 code 常不會與錯誤發生的 code 相鄰</a>, 造成 source code 的不容易閱讀與維護上的困難.</p>
<h3>Commit-or-rollback via Guards</h3>
<p>把 try-catch 丟開卻還要滿足 commit-or-rollback, 剩下的選擇只有 <a href="http://en.wikibooks.org/wiki/C%2B%2B_Programming/Exception_Handling#Guards" rel="nofollow">guard 手法</a>. 簡單的說, guard 手法是一種在 guard 的 destructor 替使用者 rollback (例如釋放資源) 的手法. STL 的 <code>auto_ptr</code>, <code><a href="http://www.boost.org/doc/libs/1_36_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_types.mutex" rel="nofollow">boost::mutex</a></code> 的 <code>scoped_lock</code> 與 <a href="http://code.google.com/p/gion/">GION</a> 都是這手法的應用.</p>
<p>與單純的 guard 不同的是 - 我們不只需要 rollback, commit 也同樣重要. 也就是說同一個 guard 要能 rollback, 也必須能視情況而被 dismiss (不要 rollback). 這個 guard class 要能支持這篇文字的題目 <code>uninitialized_fill_n</code>, 以及 <a href="http://www.kuzbass.ru:8086/docs/isocpp/lib-utilities.html#lib.specialized.algorithms" rel="nofollow">lib.specialized.algorithms</a> 所定義的其他 algorithm - <code>uninitialized_fill</code> 與 <code>uninitialized_copy</code> (還有 C++0x 的 <code>uninitialized_copy_n</code>) 的使用:</p>
<table>
<tr>
<td id="listing-3">
<pre>
<span style="color: #66cdaa;">// </span><span style="color: #66cdaa;"><a href="#listing-3">Listing 3</a>, guards for lib.specialized.algorithms</span>
<span style="color: #6495ed; font-weight: bold;">template</span> &lt;<span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">ForwardIterator</span>&gt;
<span style="color: #6495ed; font-weight: bold;">struct</span> <span style="color: #9290ff;">guarded_range_constructor</span>
{
  <span style="color: #6495ed; font-weight: bold;">typedef</span> <span style="color: #9290ff;">ForwardIterator</span> <span style="color: #9290ff;">iterator</span>;
  <span style="color: #6495ed; font-weight: bold;">typedef</span> <span style="color: #6495ed; font-weight: bold;">typename</span> <span style="color: #9932cc; font-weight: bold;">iterator_traits</span>&lt;<span style="color: #9290ff;">ForwardIterator</span>&gt;::<span style="color: #9290ff;">value_type</span> <span style="color: #9290ff;">value_type</span>;

  <span style="color: #87ceeb;">guarded_range_constructor</span>(<span style="color: #9290ff;">iterator</span> <span style="color: #98fb98;">first</span>) <span style="color: #6495ed; font-weight: bold;">throw</span> ()
  : first_(first),
    <span style="color: #87ceeb;">current_</span>(first),
    <span style="color: #87ceeb;">dismissed_</span>(<span style="color: #9932cc; font-weight: bold;">false</span>)
  {
  }
  ~guarded_range_constructor() <span style="color: #6495ed; font-weight: bold;">throw</span> ()
  {
    <span style="color: #6495ed; font-weight: bold;">if</span>(dismissed_)
      <span style="color: #6495ed; font-weight: bold;">return</span>;

    <span style="color: #6495ed; font-weight: bold;">for</span>(; first_ != current_; ++first_)
      (&amp;*first_)-&gt;~value_type();
  }

  <span style="color: #9290ff;">void</span> <span style="color: #6495ed; font-weight: bold;">operator</span><span style="color: #87ceeb;">()</span>(<span style="color: #6495ed; font-weight: bold;">const</span> <span style="color: #9290ff;">value_type</span>&amp; <span style="color: #98fb98;">value</span>)
  {
    <span style="color: #6495ed; font-weight: bold;">new</span> (&#038;*current_) <span style="color: #9290ff;">value_type</span>(value);
    ++current_;
  }

  <span style="color: #9290ff;">iterator</span> <span style="color: #87ceeb;">dismiss</span>() <span style="color: #6495ed; font-weight: bold;">throw</span> ()
  {
    dismissed_ = <span style="color: #9932cc; font-weight: bold;">true</span>;
    <span style="color: #6495ed; font-weight: bold;">return</span> current_;
  }

<span style="color: #6495ed; font-weight: bold;">private</span>:
  <span style="color: #66cdaa;">// </span><span style="color: #66cdaa;">Points to the first element where
</span>  <span style="color: #66cdaa;">// </span><span style="color: #66cdaa;">range construction started
</span>  <span style="color: #9290ff;">iterator</span> <span style="color: #98fb98;">first_</span>;

  <span style="color: #66cdaa;">// </span><span style="color: #66cdaa;">Points to the NEXT element which
</span>  <span style="color: #66cdaa;">// </span><span style="color: #66cdaa;">is to be in-place constructed.
</span>  <span style="color: #9290ff;">iterator</span> <span style="color: #98fb98;">current_</span>;

  <span style="color: #9290ff;">bool</span> <span style="color: #98fb98;">dismissed_</span>;
};</pre>
</td>
</tr>
</table>
<p>有了這個 guard class (template), <code>uninitialized_fill_n</code> 就可以這樣子實現:</p>
<table>
<tr>
<td id="listing-4">
<pre>
<span style="color: #66cdaa;">// </span><span style="color: #66cdaa;"><a href="#listing-4">Listing 4</a>, using guards</span>
<span style="color: #6495ed; font-weight: bold;">template</span> &lt;<span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">ForwardIterator</span>, <span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">Size</span>, <span style="color: #6495ed; font-weight: bold;">class</span> <span style="color: #9290ff;">T</span>&gt;
<span style="color: #9290ff;">void</span>
  uninitialized_fill_n(<span style="color: #9290ff;">ForwardIterator</span> <span style="color: #98fb98;">first</span>, <span style="color: #9290ff;">Size</span> <span style="color: #98fb98;">n</span>, <span style="color: #6495ed; font-weight: bold;">const</span> <span style="color: #9290ff;">T</span>&amp; <span style="color: #98fb98;">x</span>)
{
  <span style="color: #9290ff;">guarded_range_constructor</span>&lt;<span style="color: #9290ff;">ForwardIterator</span>&gt; <span style="color: #98fb98;">guarded_ctor</span>(first);
  <span style="color: #6495ed; font-weight: bold;">for</span> ( ; n; <code>--n</code>)
  {
    guarded_ctor(x);
  }
  guarded_ctor.dismiss();
}</pre>
</td>
</tr>
</table>
<p>與上一篇的 <a href="http://fsfoundry.org/codefreak/2008/08/18/a-new-mind-set-for-exception-handling/#modifying-functions-those-wont-throw">這個列表</a> 對照可以看出, 這個版本只有在呼叫 <code>guarded_ctor</code> 的 functional operator 時有可能會 throw. 這裡拋出的 exception 會跳過 <code>guarded_ctor.dismiss()</code> 直接觸發 <code>guarded_ctor</code> 的解構, 進而達到 rollback/unwind 的結果. 比起以 <a href="#listing-2">try-catch 手法的實現</a> 這個版本有下列優點:</p>
<ol>
<li>Low overhead</li>
<li>符合標準的作法, 並且適用於更大的 (非標準規範的) 應用範圍</li>
<li>雖然在這個例子中沒有明顯效益. IMO, 在複雜的狀況下較容易閱讀也較不容易出錯</li>
</ol>
<p><a href="#listing-4">使用了 guards 的版本</a> 有個值得注意的地方. 除了需主動 dismiss, 其餘的部份幾乎可以 line-by-line 的對應到 <a href="#listing-1">沒有 rollback 的版本</a> (也就是標準中 effects 的示意 code snip). 如果我們選擇的範例更複雜, 這現象會愈加明顯.</p>
<h3>雜記</h3>
<p>這篇文字的部份 code snippet 取自作者參與的一個 Free Software project - <a href="http://code.google.com/p/ontl/">Open NTL</a>. 寫這篇文字的過程幫助我找到了 <a href="http://code.google.com/p/ontl/source/detail?r=301" rel="nofollow">一個之前沒被注意到的 bug</a> 以及 <a href="http://code.google.com/p/ontl/source/detail?r=300" rel="nofollow">一個多餘的但無害的 iterator increment</a>.</p>
<ol start="1" class="footnotes"><li id="footnote-1-280" class="footnote">不同於其他的 <code>new</code> expression 會先動態請求 instance 所需要的 memory footprint 然後再調用 class specific constructor. Placement new 是 C++ 支援的一種就地創建 instance 的方法. 在使用者指定的位置直接調用 constructor [<a href="#footnote-link-1-280" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-2-280" class="footnote">其實我很不想談 C++ exception 所引發的 overhead. 在我的認知那多是 C++ runtime 實現的問題. 這部份可以有很大的進步空間. 見 <a href="http://netlab.ru.is/exception/LinuxCXX.shtml" rel="nofollow">C++ in the Linux Kernel</a>. 不幸的是這個 project (的網頁) 似乎已經下線了, 請參考 Wayback Machine 上的 <a href="http://web.archive.org/web/20071222161357/http://netlab.ru.is/exception/LinuxCXX.shtml" rel="nofollow">2007 年底的頁面</a> [<a href="#footnote-link-2-280" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-3-280" class="footnote">如在 MSVC 上用了 <a href="http://msdn.microsoft.com/en-us/library/1deeycx5(VS.80).aspx" rel="nofollow">/EHsc</a> 這個 option 時, <code>catch(...)</code> clause 抓不到 SEH [<a href="#footnote-link-3-280" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-4-280" class="footnote">可能有人會問, 如果無法接受 C++ exception 的 overhead 為何不乾脆 <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Exceptions#Exceptions" rel="nofollow">像 Google 一樣完全禁止 exception</a>? 因為他們可能跟我一樣, 認為 <a href="http://fsfoundry.org/codefreak/2008/07/06/google-forbids-use-of-exception-in-cpp/">寫沒有 exception 的 C++ 實在是太折磨人了</a> [<a href="#footnote-link-4-280" class="footnote-link footnote-back-link">↩</a>]</li><li id="footnote-5-280" class="footnote">就我所知, 目前只有 <a href="http://code.google.com/p/ontl/">Open NTL</a> (disclaimer: 筆者參與的 Free Software project 之一), 擁有一份尚未成熟, 正邁向符合 C++0x 標準規範 <a href="http://code.google.com/p/ontl/source/browse/#svn/branches/stl-exception-safety/ntl/stlx">支持 Windows kernel 的 STL implementation</a>. 本文的 <a href="http://code.google.com/p/ontl/source/browse/branches/stl-exception-safety/ntl/stlx/esafety.hxx?spec=svn301&#038;r=301#99" rel="nofollow"><code>range_construction_guard</code></a> 與 <a href="http://code.google.com/p/ontl/source/browse/branches/stl-exception-safety/ntl/stlx/memory.hxx?spec=svn301&#038;r=300#289" rel="nofollow"><code>uninitialized_fill_n</code></a> 的實現皆取材自 Open NTL [<a href="#footnote-link-5-280" class="footnote-link footnote-back-link">↩</a>]</li></ol>]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/08/31/a-new-mind-set-for-exception-handling-using-guards/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>fr3@K</name>
						<uri>http://fsfoundry.org/codefreak/</uri>
					</author>
		<title type="html"><![CDATA[Sutter on Hungarian Notation]]></title>
		<link rel="alternate" type="text/html" href="http://feeds.feedburner.com/~r/codefreak/~3/367903172/" />
		<id>http://fsfoundry.org/codefreak/2008/08/18/sutter-on-hungarian-notation/</id>
		<updated>2008-08-19T12:02:15Z</updated>
		<published>2008-08-18T08:10:43Z</published>
		<category scheme="http://fsfoundry.org/codefreak" term="~" /><category scheme="http://fsfoundry.org/codefreak" term="C++" /><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[From Sutter&#8217;s Mill:
&#8230; The main trouble with Systems Hungarian comes from trying to embed information about a variable’s type into the variable’s name by prepending an encoded wart like the venerable sz, pach, ul, and their ilk. Although potentially helpful in a weakly-typed language like C, that’s known to be brittle and the prefixes tend [...]]]></summary>
		<content type="html" xml:base="http://fsfoundry.org/codefreak/2008/08/18/sutter-on-hungarian-notation/"><![CDATA[<p>From <a href="http://herbsutter.wordpress.com/2008/07/15/hungarian-notation-is-clearly-goodbad/" rel="nofollow">Sutter&#8217;s Mill</a>:</p>
<blockquote><p>&#8230; The main trouble with Systems Hungarian comes from trying to embed information about a variable’s type into the variable’s name by prepending an encoded wart like the venerable sz, pach, ul, and their ilk. Although potentially helpful in a weakly-typed language like C, that’s known to be brittle and the prefixes tend to turn into lies as variable types morph during maintenance. The warting systems also don’t extend well to user-defined types and templates.</p></blockquote>
<p>I am glad to learn that <a href="http://www.gotw.ca/" rel="nofollow">the Guru</a> and I share <a href="http://fsfoundry.org/codefreak/2006/01/22/the-hungarian-notation/">similar views on this matter</a>.</p>
<hr />
<p>Further readings:</p>
<ul>
<li><a href="http://blogs.msdn.com/larryosterman/archive/2004/06/22/162629.aspx" rel="nofollow">Hugarian notation - it&#8217;s my turn now <img src='http://fsfoundry.org/codefreak/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </a> from Larry Osterman&#8217;s WebLog.</li>
<li><a href="http://www.neopoleon.com/home/blogs/neo/archive/2004/06/01/11255.aspx" rel="nofollow">Die, Hungarian notation&#8230; Just *die*</a> from Neopoleon.</li>
<li><a href="http://www.joelonsoftware.com/articles/Wrong.html" rel="nofollow">Making Wrong Code Look Wrong</a> from Joel on Software.</li>
</ul>
]]></content>
	<feedburner:origLink>http://fsfoundry.org/codefreak/2008/08/18/sutter-on-hungarian-notation/</feedburner:origLink></entry>
	</feed>
