<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: I Like to Move It</title>
	<atom:link href="http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/feed/" rel="self" type="application/rss+xml" />
	<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/</link>
	<description>Weblog of a lively geek.</description>
	<lastBuildDate>Thu, 04 Mar 2010 10:35:11 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: nono</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-8789</link>
		<dc:creator>nono</dc:creator>
		<pubDate>Sat, 08 Aug 2009 13:51:36 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-8789</guid>
		<description>感激! 發現誤解的來源了...
我是用 VC2005 測試的, 而 VC 偷偷對 str(123) 做了 rvalue-to-lvalue 轉換
因此下面的代碼可在 VC 下編譯通過
&lt;code&gt;
template &lt;class T&gt;
string&amp; operator%(string &amp;s, const T&amp; x) {
    return s += string(x);
}

int main() {
    string output = string(&quot;123&quot;) % &quot;456&quot;;
}
&lt;/code&gt;
而 g++(3.4.6)不允許, 所以的確不是個通用的好方法
&lt;ul&gt;&lt;em&gt;ed: minor correction in code snip - fr3@K.&lt;/em&gt;&lt;/ul&gt;</description>
		<content:encoded><![CDATA[<p>感激! 發現誤解的來源了&#8230;<br />
我是用 VC2005 測試的, 而 VC 偷偷對 str(123) 做了 rvalue-to-lvalue 轉換<br />
因此下面的代碼可在 VC 下編譯通過<br />
<code><br />
template &lt;class T&gt;<br />
string&amp; operator%(string &amp;s, const T&amp; x) {<br />
    return s += string(x);<br />
}</p>
<p>int main() {<br />
    string output = string("123") % "456";<br />
}<br />
</code><br />
而 g++(3.4.6)不允許, 所以的確不是個通用的好方法</p>
<ul><em>ed: minor correction in code snip &#8211; fr3@K.</em></ul>
]]></content:encoded>
	</item>
	<item>
		<title>By: fr3@K</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-8643</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Mon, 03 Aug 2009 03:19:04 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-8643</guid>
		<description>Hi nono,

無論 &lt;code&gt;const&lt;/code&gt; 與否, 以 &lt;code&gt;string&amp;&lt;/code&gt; 只能 bind 到 Lvalue (named instance). 而 &lt;code&gt;str()&lt;/code&gt; 回傳的是 &lt;a href=&quot;http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/&quot; rel=&quot;nofollow&quot;&gt;Rvalue&lt;/a&gt;, 因此你的提議不 work.</description>
		<content:encoded><![CDATA[<p>Hi nono,</p>
<p>無論 <code>const</code> 與否, 以 <code>string&amp;</code> 只能 bind 到 Lvalue (named instance). 而 <code>str()</code> 回傳的是 <a href="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/" rel="nofollow">Rvalue</a>, 因此你的提議不 work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nono</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-8633</link>
		<dc:creator>nono</dc:creator>
		<pubDate>Sun, 02 Aug 2009 17:13:20 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-8633</guid>
		<description>前輩...冒昧請教一下

後兩次的 construction 應該可以改寫 operator% 如下, 就能省下來
string&amp; operator%(string &amp;s, const T&amp; x) {...}

而無中生有的那三次的確似乎就非靠 move 不可了, 不知是否正確?</description>
		<content:encoded><![CDATA[<p>前輩&#8230;冒昧請教一下</p>
<p>後兩次的 construction 應該可以改寫 operator% 如下, 就能省下來<br />
string&amp; operator%(string &amp;s, const T&amp; x) {&#8230;}</p>
<p>而無中生有的那三次的確似乎就非靠 move 不可了, 不知是否正確?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fr3@K</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-6577</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Sat, 23 May 2009 06:11:39 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-6577</guid>
		<description>Thanks, sam.</description>
		<content:encoded><![CDATA[<p>Thanks, sam.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sam</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-6550</link>
		<dc:creator>sam</dc:creator>
		<pubDate>Fri, 22 May 2009 10:49:19 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-6550</guid>
		<description>And here is a Channel 9 video, which have some discuss C++0x new features. (http://tinyurl.com/r6l4m8)</description>
		<content:encoded><![CDATA[<p>And here is a Channel 9 video, which have some discuss C++0x new features. (<a href="http://tinyurl.com/r6l4m8)" rel="nofollow">http://tinyurl.com/r6l4m8)</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sam</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-6538</link>
		<dc:creator>sam</dc:creator>
		<pubDate>Fri, 22 May 2009 04:36:57 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-6538</guid>
		<description>FYI. Here is a Rvalue Reference information from Microsoft Visual C++ Team.

http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx</description>
		<content:encoded><![CDATA[<p>FYI. Here is a Rvalue Reference information from Microsoft Visual C++ Team.</p>
<p><a href="http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx" rel="nofollow">http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fr3@K</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-6511</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Thu, 21 May 2009 12:17:06 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-6511</guid>
		<description>Hi Miki,

事實上我第一次知道到 Rvalue References 這玩意, 也是因為你提到的那篇 Danny Kalev 的文章. 可惜的是 &lt;a href=&quot;http://fsfoundry.org/codefreak/2007/04/28/mistake-in-cplusplus-reference-guide/&quot; rel=&quot;nofollow&quot;&gt;該文的 sample code 有錯誤&lt;/a&gt;, 也讓當時剛開始嘗試了解 Rvalue References 的我遇到了挫折. 兩年前曾嘗試過留言與 email, 很遺憾, 那個錯誤依然不動如山.

回到你的問題. 在 (語意上的) return 時會不會發生 copy 與 inline 不 inline 沒有關係. 換個說法, &lt;strong&gt;function 的語意不該因為 inline 而改變&lt;/strong&gt;. 撇開 move 不談, 原本在 return 時該有的 copy 會不會&lt;strong&gt;略去&lt;/strong&gt;與 compiler optimization 有關, or more specifically, RVO. 這方面建議可以參考 Lippman 的 &quot;Inside the C++ Object Model&quot;, Chapter 2.3 裡的 &quot;Return value initialization&quot;, &quot;Optimization at the user level&quot; 以及 &quot;Optimization at the compiler level&quot; 等章節. 如果你手邊沒有的話, 網路上也能找到其解釋, 如 &lt;a href=&quot;http://stupefydeveloper.blogspot.com/2008/10/c-rvo-and-nrvo.html&quot; rel=&quot;nofollow&quot;&gt;這裡&lt;/a&gt;. Just google for &quot;RVO C++&quot;.

副帶一提, 你提到了「兩個&amp;」(&amp;&amp;) 這個符號. 其實若不是 code 中的 &lt;code&gt;operator%&lt;/code&gt; template 的目的只是要串接繼續要被字串化的的 object, 因而總是可以享受 RVO, 否則這個 template 可能該寫成下列兩個 overload:
&lt;ul&gt;&lt;pre&gt;
template &lt;class T&gt;
string operator%(string&amp;&amp; s, const T&amp; x)
{
  return move(s += str(x));
}
template &lt;class T&gt;
string operator%(const string&amp; s, const T&amp; x)
{
  string tmp(s);
  return move(tmp) % x;
}
&lt;/pre&gt;&lt;/ul&gt;

我曾在另外一篇文字嘗試以一個 &lt;a href=&quot;http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/#listing-11&quot; rel=&quot;nofollow&quot;&gt;sample string class 來解釋 &amp;&amp; 的使用時機與方法&lt;/a&gt;, 提供你參考.</description>
		<content:encoded><![CDATA[<p>Hi Miki,</p>
<p>事實上我第一次知道到 Rvalue References 這玩意, 也是因為你提到的那篇 Danny Kalev 的文章. 可惜的是 <a href="http://fsfoundry.org/codefreak/2007/04/28/mistake-in-cplusplus-reference-guide/" rel="nofollow">該文的 sample code 有錯誤</a>, 也讓當時剛開始嘗試了解 Rvalue References 的我遇到了挫折. 兩年前曾嘗試過留言與 email, 很遺憾, 那個錯誤依然不動如山.</p>
<p>回到你的問題. 在 (語意上的) return 時會不會發生 copy 與 inline 不 inline 沒有關係. 換個說法, <strong>function 的語意不該因為 inline 而改變</strong>. 撇開 move 不談, 原本在 return 時該有的 copy 會不會<strong>略去</strong>與 compiler optimization 有關, or more specifically, RVO. 這方面建議可以參考 Lippman 的 &#8220;Inside the C++ Object Model&#8221;, Chapter 2.3 裡的 &#8220;Return value initialization&#8221;, &#8220;Optimization at the user level&#8221; 以及 &#8220;Optimization at the compiler level&#8221; 等章節. 如果你手邊沒有的話, 網路上也能找到其解釋, 如 <a href="http://stupefydeveloper.blogspot.com/2008/10/c-rvo-and-nrvo.html" rel="nofollow">這裡</a>. Just google for &#8220;RVO C++&#8221;.</p>
<p>副帶一提, 你提到了「兩個&amp;」(&amp;&amp;) 這個符號. 其實若不是 code 中的 <code>operator%</code> template 的目的只是要串接繼續要被字串化的的 object, 因而總是可以享受 RVO, 否則這個 template 可能該寫成下列兩個 overload:</p>
<ul>
<pre>
template &lt;class T&gt;
string operator%(string&amp;&amp; s, const T&amp; x)
{
  return move(s += str(x));
}
template &lt;class T&gt;
string operator%(const string&amp; s, const T&amp; x)
{
  string tmp(s);
  return move(tmp) % x;
}
</pre>
</ul>
<p>我曾在另外一篇文字嘗試以一個 <a href="http://fsfoundry.org/codefreak/2008/11/16/cpp0x-rvalue-references/#listing-11" rel="nofollow">sample string class 來解釋 &amp;&amp; 的使用時機與方法</a>, 提供你參考.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Miki</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-6502</link>
		<dc:creator>Miki</dc:creator>
		<pubDate>Thu, 21 May 2009 06:59:05 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-6502</guid>
		<description>看你網誌的介紹，覺得比較易了解RValue Reference了。可是我之前看其他的網頁中，有用到「兩個&amp;」這個符號。如以下:
http://www.informit.com/guides/content.aspx?g=cplusplus&amp;seqNum=310
那又是什麼呢？

另外，這以下的function 
string operator%(string s, const T&amp; x)
如果有用inline 的話，照理來講compiler應該會是做和macro 代入展開一樣，照理來講根本應該不會有return，也不會有copy ctor發生吧？</description>
		<content:encoded><![CDATA[<p>看你網誌的介紹，覺得比較易了解RValue Reference了。可是我之前看其他的網頁中，有用到「兩個&amp;」這個符號。如以下:<br />
<a href="http://www.informit.com/guides/content.aspx?g=cplusplus&amp;seqNum=310" rel="nofollow">http://www.informit.com/guides/content.aspx?g=cplusplus&amp;seqNum=310</a><br />
那又是什麼呢？</p>
<p>另外，這以下的function<br />
string operator%(string s, const T&amp; x)<br />
如果有用inline 的話，照理來講compiler應該會是做和macro 代入展開一樣，照理來講根本應該不會有return，也不會有copy ctor發生吧？</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fr3@K</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-6470</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Wed, 20 May 2009 08:28:27 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-6470</guid>
		<description>好個望穿秋水, 形容得很貼切. :)</description>
		<content:encoded><![CDATA[<p>好個望穿秋水, 形容得很貼切. <img src='http://fsfoundry.org/codefreak/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: av</title>
		<link>http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/comment-page-1/#comment-6451</link>
		<dc:creator>av</dc:creator>
		<pubDate>Tue, 19 May 2009 12:48:58 +0000</pubDate>
		<guid isPermaLink="false">http://fsfoundry.org/codefreak/2009/05/19/i-like-to-move-it/#comment-6451</guid>
		<description>從語言層面支援的 move 終於快要能用了，真是讓人望穿秋水啊 XD</description>
		<content:encoded><![CDATA[<p>從語言層面支援的 move 終於快要能用了，真是讓人望穿秋水啊 XD</p>
]]></content:encoded>
	</item>
</channel>
</rss>
