<?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: Want Speed? Pass by&#160;Value.</title>
	<atom:link href="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/feed/" rel="self" type="application/rss+xml" />
	<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/</link>
	<description>The next generation of C++</description>
	<lastBuildDate>Tue, 09 Mar 2010 21:18:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: litb</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-115</link>
		<dc:creator>litb</dc:creator>
		<pubDate>Wed, 02 Sep 2009 13:36:44 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-115</guid>
		<description>&lt;p&gt;Hi there. A similar example where we would mutate a temporary that has got a name:&lt;/p&gt;

&lt;pre&gt;void f&#40;&#41; &#123;
  try &#123;
    dangerous&#40;&#41;;
  &#125; catch&#40;Exception &amp;e&#41; &#123;
    // (seemingly) copy it then (seemingly) modify 
    // only the copy
    Exception copy&#40;e&#41;;
    copy.enjoy&#40;&#41;;
    throw; 
  &#125;
&#125;&lt;/pre&gt;

&lt;p&gt;We would accidentally modify the exception object. I think the restriction of the reference binding will generally make it so we can&#039;t refer to the temporary object a second time.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi there. A similar example where we would mutate a temporary that has got a name:</p>

<pre>void f&#40;&#41; &#123;
  try &#123;
    dangerous&#40;&#41;;
  &#125; catch&#40;Exception &amp;e&#41; &#123;
    // (seemingly) copy it then (seemingly) modify 
    // only the copy
    Exception copy&#40;e&#41;;
    copy.enjoy&#40;&#41;;
    throw; 
  &#125;
&#125;</pre>

<p>We would accidentally modify the exception object. I think the restriction of the reference binding will generally make it so we can&#8217;t refer to the temporary object a second time.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: litb</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-113</link>
		<dc:creator>litb</dc:creator>
		<pubDate>Tue, 01 Sep 2009 21:52:32 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-113</guid>
		<description>&lt;p&gt;I think there is another version of that. Consider:&lt;/p&gt;

&lt;pre&gt;void f&#40;&#41; &#123;
  try &#123;
    prolly_failing&#40;&#41;;
  &#125; catch&#40;Exception &amp;e&#41; &#123;
    Exception e1&#40;e&#41;;
    e1.modify&#40;&#41;;
      // oops, e is modified potentially
  &#125;
&#125;&lt;/pre&gt;

&lt;p&gt;Since exception objects are temporaries too, the restriction about reference binding makes the irritating case of above not possible.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I think there is another version of that. Consider:</p>

<pre>void f&#40;&#41; &#123;
  try &#123;
    prolly_failing&#40;&#41;;
  &#125; catch&#40;Exception &amp;e&#41; &#123;
    Exception e1&#40;e&#41;;
    e1.modify&#40;&#41;;
      // oops, e is modified potentially
  &#125;
&#125;</pre>

<p>Since exception objects are temporaries too, the restriction about reference binding makes the irritating case of above not possible.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Abrahams</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-106</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Sun, 30 Aug 2009 05:06:47 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-106</guid>
		<description>&lt;blockquote cite=&quot;comment-65&quot;&gt;

&lt;strong&gt;&lt;a href=&quot;#comment-65&quot; rel=&quot;nofollow&quot;&gt;Andrzej Krzemienski&lt;/a&gt;&lt;/strong&gt;: The copy-and-swap idiom. It implements a copy in terms of swap. Then, the default swap function is implemented in terms of three copies. It is all fine if you always implement customized swap for your classes, but if you don’t you are risking infinite recursion. Wouldn’t it be safer if the idiom was always accompained by the note saying that you need to implement swap too?
&lt;/blockquote&gt;

&lt;p&gt;Maybe, but it&#039;s probably not as scary as you think.  Outside &lt;code&gt;namespace std&lt;/code&gt;, an unqualified call to &lt;code&gt;swap&lt;/code&gt; won&#039;t find &lt;code&gt;std::swap&lt;/code&gt; unless you&#039;ve explicitly brought it into scope with a &lt;em&gt;using-declaration&lt;/em&gt;.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<blockquote cite="comment-65">

<strong><a href="#comment-65" rel="nofollow">Andrzej Krzemienski</a></strong>: The copy-and-swap idiom. It implements a copy in terms of swap. Then, the default swap function is implemented in terms of three copies. It is all fine if you always implement customized swap for your classes, but if you don’t you are risking infinite recursion. Wouldn’t it be safer if the idiom was always accompained by the note saying that you need to implement swap too?
</blockquote>

<p>Maybe, but it&#8217;s probably not as scary as you think.  Outside <code>namespace std</code>, an unqualified call to <code>swap</code> won&#8217;t find <code>std::swap</code> unless you&#8217;ve explicitly brought it into scope with a <em>using-declaration</em>.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Abrahams</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-105</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Sun, 30 Aug 2009 04:57:23 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-105</guid>
		<description>&lt;blockquote cite=&quot;comment-83&quot;&gt;

&lt;strong&gt;&lt;a href=&quot;#comment-83&quot; rel=&quot;nofollow&quot;&gt;Niels Dekker&lt;/a&gt;&lt;/strong&gt;: Fast self-assignment could be achieved by adding an extra check to the “canonical” version
&lt;/blockquote&gt;

&lt;p&gt;...which would penalize the usual case and complicate the code in order to optimize a rare case, which is almost always a bad idea.&lt;/p&gt;

&lt;blockquote cite=&quot;comment-83&quot;&gt;

&lt;strong&gt;&lt;a href=&quot;#comment-83&quot; rel=&quot;nofollow&quot;&gt;Niels Dekker&lt;/a&gt;&lt;/strong&gt;:  I find it slightly counter-intuitive, having a self-assignment that might fail!
&lt;/blockquote&gt;

&lt;p&gt;These self-assignments never have the form &lt;code&gt;x = x&lt;/code&gt; anyway (nobody knowingly does a self-assignment except in test suites).  They&#039;re almost always &lt;code&gt;x = y&lt;/code&gt; cases, where x and y may or may not refer to the same object.  That means we&#039;re in code that has to cope with an exception anyhow.  There&#039;s really zero advantage in making self-assignment a no-throw operation.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<blockquote cite="comment-83">

<strong><a href="#comment-83" rel="nofollow">Niels Dekker</a></strong>: Fast self-assignment could be achieved by adding an extra check to the “canonical” version
</blockquote>

<p>&#8230;which would penalize the usual case and complicate the code in order to optimize a rare case, which is almost always a bad idea.</p>

<blockquote cite="comment-83">

<strong><a href="#comment-83" rel="nofollow">Niels Dekker</a></strong>:  I find it slightly counter-intuitive, having a self-assignment that might fail!
</blockquote>

<p>These self-assignments never have the form <code>x = x</code> anyway (nobody knowingly does a self-assignment except in test suites).  They&#8217;re almost always <code>x = y</code> cases, where x and y may or may not refer to the same object.  That means we&#8217;re in code that has to cope with an exception anyhow.  There&#8217;s really zero advantage in making self-assignment a no-throw operation.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Abrahams</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-104</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Sun, 30 Aug 2009 04:51:58 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-104</guid>
		<description>&lt;p&gt;Trick question? It&#039;s a syntax error: you can&#039;t cv-qualify the reference itself.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Trick question? It&#8217;s a syntax error: you can&#8217;t cv-qualify the reference itself.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Abrahams</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-102</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Sun, 30 Aug 2009 04:31:48 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-102</guid>
		<description>&lt;p&gt;See my reply &lt;a href=&quot;#comment-99&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>See my reply <a href="#comment-99" rel="nofollow">here</a>.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: SG</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-101</link>
		<dc:creator>SG</dc:creator>
		<pubDate>Sat, 29 Aug 2009 14:43:06 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-101</guid>
		<description>&lt;p&gt;I think you meant&lt;/p&gt;

&lt;pre&gt;vector&lt;string&gt; const  names = get_names&#40;&#41;; // #1
vector&lt;string&gt; const&amp; names = get_names&#40;&#41;; // #2&lt;/pre&gt;

&lt;p&gt;Assuming get_names returns by value (and not a reference) and you&#039;re dealing with a &quot;good&quot; compiler there should not be a difference between the two. In the first case (#1) the compiler can make the function construct its return value directly in that space that will be referred to by the name &quot;names&quot;. In #2 the return value that lives on the stack gets an extension of its life-time and a reference to it is created. A good compiler doesn&#039;t even need to allocate space for the reference but I&#039;m not sure if most compilers are that smart (even though they could be).&lt;/p&gt;

&lt;p&gt;If get_names returns a reference it makes a big difference, of course. Is the following code safe?&lt;/p&gt;

&lt;pre&gt;string const&amp; blah = string&#40;&quot;123&quot;) + &quot;456&quot;;&lt;/pre&gt;

&lt;p&gt;Currently, it is safe. It&#039;s not safe C++0x (according to the current draft) because operator+(string&amp;&amp;,char const*) returns an rvalue reference --&gt; You get a dangling reference.&lt;/p&gt;

&lt;p&gt;In my opinion, people should not write #2 instead of #1 just because they think they can safe a copy. Many current compilers successfully elide the copy in #1. Also, they should not declare functions that return rvalue references (I really hope operator+(string&amp;&amp;,char const*) and others will be fixed) for &quot;temporary recycling&quot; because it opens up the possibility of dangling references.&lt;/p&gt;

&lt;p&gt;Cheers!
SG&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I think you meant</p>

<pre>vector&lt;string&gt; const  names = get_names&#40;&#41;; // #1
vector&lt;string&gt; const&amp; names = get_names&#40;&#41;; // #2</pre>

<p>Assuming get_names returns by value (and not a reference) and you&#8217;re dealing with a &#8220;good&#8221; compiler there should not be a difference between the two. In the first case (#1) the compiler can make the function construct its return value directly in that space that will be referred to by the name &#8220;names&#8221;. In #2 the return value that lives on the stack gets an extension of its life-time and a reference to it is created. A good compiler doesn&#8217;t even need to allocate space for the reference but I&#8217;m not sure if most compilers are that smart (even though they could be).</p>

<p>If get_names returns a reference it makes a big difference, of course. Is the following code safe?</p>

<pre>string const&amp; blah = string&#40;&quot;123&quot;) + &quot;456&quot;;</pre>

<p>Currently, it is safe. It&#8217;s not safe C++0x (according to the current draft) because operator+(string&amp;&amp;,char const*) returns an rvalue reference &#8211;&gt; You get a dangling reference.</p>

<p>In my opinion, people should not write #2 instead of #1 just because they think they can safe a copy. Many current compilers successfully elide the copy in #1. Also, they should not declare functions that return rvalue references (I really hope operator+(string&amp;&amp;,char const*) and others will be fixed) for &#8220;temporary recycling&#8221; because it opens up the possibility of dangling references.</p>

<p>Cheers!
SG</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Abrahams</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-99</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Fri, 28 Aug 2009 05:33:15 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-99</guid>
		<description>&lt;p&gt;It&#039;s not entirely useless if you know something about the type being passed, like how to move-construct it or that it can be emulated cheaply with default construct and swap&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>It&#8217;s not entirely useless if you know something about the type being passed, like how to move-construct it or that it can be emulated cheaply with default construct and swap</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Mathias Gaunard</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-98</link>
		<dc:creator>Mathias Gaunard</dc:creator>
		<pubDate>Fri, 28 Aug 2009 03:44:39 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-98</guid>
		<description>&lt;p&gt;Or rather, pass by value any arguments that you would otherwise copy explicitly and which you don&#039;t want to control the storage of the copy of.&lt;/p&gt;

&lt;p&gt;Indeed, for something like vector::push_back, passing by value is useless, even though you want to explicitly copy the argument.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Or rather, pass by value any arguments that you would otherwise copy explicitly and which you don&#8217;t want to control the storage of the copy of.</p>

<p>Indeed, for something like vector::push_back, passing by value is useless, even though you want to explicitly copy the argument.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Mathias Gaunard</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-95</link>
		<dc:creator>Mathias Gaunard</dc:creator>
		<pubDate>Wed, 26 Aug 2009 15:09:14 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-95</guid>
		<description>&lt;p&gt;That guideline appears a bit wrong.
For example, with that kind of code&lt;/p&gt;

&lt;pre&gt;void vector::push_back&#40;const T&amp; t&#41;
&#123;
    ...
    new&#40;buffer&#41; T&#40;t&#41;;
    ...
&#125;&lt;/pre&gt;

&lt;p&gt;It would be silly to pass t by value.&lt;/p&gt;

&lt;p&gt;So I believe it should be something like:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Pass by value any arguments that you would otherwise copy explicitly, and for which you do not need to control the storage of the copy.&lt;/p&gt;
&lt;/blockquote&gt;
</description>
		<content:encoded><![CDATA[<p>That guideline appears a bit wrong.
For example, with that kind of code</p>

<pre>void vector::push_back&#40;const T&amp; t&#41;
&#123;
    ...
    new&#40;buffer&#41; T&#40;t&#41;;
    ...
&#125;</pre>

<p>It would be silly to pass t by value.</p>

<p>So I believe it should be something like:</p>

<blockquote>
  <p>Pass by value any arguments that you would otherwise copy explicitly, and for which you do not need to control the storage of the copy.</p>
</blockquote>]]></content:encoded>
	</item>
</channel>
</rss>
