<?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>Fri, 27 Jan 2012 17:36:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Robert Ramey</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1894</link>
		<dc:creator>Robert Ramey</dc:creator>
		<pubDate>Fri, 27 Jan 2012 17:36:04 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1894</guid>
		<description>&lt;p&gt;This is a great comment.  It well captures the ying/yang of C++ development.&lt;/p&gt;

&lt;p&gt;I think the solution is building applications in layers of abstraction.  This can permit all/most of the issues related to low level optimization to be addressed in lower layers while upper layers and ignore most of this.  This is why I&#039;m a fan of C++.  Unfortunately, I think a lot of programing shops miss and just start coding rather than building applications layer by layer.  Using C++ in this way causes lots of frustration and confusion.&lt;/p&gt;

&lt;p&gt;C++ is a toolbox for making other toolboxes.&lt;/p&gt;

&lt;p&gt;Robert Ramey&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This is a great comment.  It well captures the ying/yang of C++ development.</p>

<p>I think the solution is building applications in layers of abstraction.  This can permit all/most of the issues related to low level optimization to be addressed in lower layers while upper layers and ignore most of this.  This is why I&#8217;m a fan of C++.  Unfortunately, I think a lot of programing shops miss and just start coding rather than building applications layer by layer.  Using C++ in this way causes lots of frustration and confusion.</p>

<p>C++ is a toolbox for making other toolboxes.</p>

<p>Robert Ramey</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-1890</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Fri, 13 Jan 2012 19:33:22 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1890</guid>
		<description>&lt;p&gt;1000% agreed&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>1000% agreed</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Howard Hinnant</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1889</link>
		<dc:creator>Howard Hinnant</dc:creator>
		<pubDate>Fri, 13 Jan 2012 18:10:11 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1889</guid>
		<description>&lt;p&gt;I think this article should be updated for C++11.  There are two things wrong with it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;
 It leaves the impression that one should &lt;strong&gt;always&lt;/strong&gt; write your assignment operator like so:
&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;
T&amp; operator=(T x)    // x is a copy of the source; hard work already done
{
    swap(*this, x);  // trade our resources for x&#039;s
    return *this;    // our (old) resources get destroyed with x
}
&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;
But in some important cases, this is a large performance penalty.  Vector-like classes where heap memory can be reused during the copy assignment is a classic example.  I&#039;ve just written a short example showing as high as a 7X performance penalty.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
 In C++11 the correct way to write sorted is:
&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;
std::vector&lt;std::string&gt;
sorted(std::vector&lt;std::string&gt; names)
{
    std::sort(names.begin(), names.end());
    return names;
}
&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;
Implicit return-by-move from by-value parameters is now required.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The basic point of the article is sound:  Passing by value is an important tool in the tool box.  But I&#039;ve seen too many references to this article that mistakenly throw design and testing out the window on this issue, and translate this article into &quot;&lt;strong&gt;always&lt;/strong&gt; pass by value&quot;.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I think this article should be updated for C++11.  There are two things wrong with it:</p>

<ol>
<li>
<p>
 It leaves the impression that one should <strong>always</strong> write your assignment operator like so:
</p>

<blockquote><pre>
T&amp; operator=(T x)    // x is a copy of the source; hard work already done
{
    swap(*this, x);  // trade our resources for x's
    return *this;    // our (old) resources get destroyed with x
}
</pre></blockquote>

<p>
But in some important cases, this is a large performance penalty.  Vector-like classes where heap memory can be reused during the copy assignment is a classic example.  I&#8217;ve just written a short example showing as high as a 7X performance penalty.
</p>
</li>
<li>
<p>
 In C++11 the correct way to write sorted is:
</p>

<blockquote><pre>
std::vector&lt;std::string&gt;
sorted(std::vector&lt;std::string&gt; names)
{
    std::sort(names.begin(), names.end());
    return names;
}
</pre></blockquote>

<p>
Implicit return-by-move from by-value parameters is now required.
</p>
</li>
</ol>

<p>The basic point of the article is sound:  Passing by value is an important tool in the tool box.  But I&#8217;ve seen too many references to this article that mistakenly throw design and testing out the window on this issue, and translate this article into &#8220;<strong>always</strong> pass by value&#8221;.</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-1877</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Wed, 28 Dec 2011 22:30:11 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1877</guid>
		<description>&lt;p&gt;Hi Andrzej,&lt;/p&gt;

&lt;p&gt;I wish this were crisper, but I would say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;operator=&lt;/code&gt; is unconditionally &lt;code&gt;noexcept&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;assignment from an lvalue is &lt;code&gt;noexcept&lt;/code&gt; if &lt;code&gt;T&lt;/code&gt;’s copy constructor is &lt;code&gt;noexcept&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;assignment from an rvalue is &lt;code&gt;noexcept&lt;/code&gt; if move-constructing a &lt;code&gt;T&lt;/code&gt; is &lt;code&gt;noexcept&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Andrzej,</p>

<p>I wish this were crisper, but I would say:</p>

<ul>
<li><code>operator=</code> is unconditionally <code>noexcept</code></li>
<li>assignment from an lvalue is <code>noexcept</code> if <code>T</code>’s copy constructor is <code>noexcept</code></li>
<li>assignment from an rvalue is <code>noexcept</code> if move-constructing a <code>T</code> is <code>noexcept</code></li>
</ul>]]></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-1845</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Fri, 23 Dec 2011 13:18:19 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1845</guid>
		<description>&lt;p&gt;Note: you&#039;ll have to bring the pass-by-reference signature back when you start writing move constructors.  The pass-by-value idiom creates an ambiguity otherwise&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Note: you&#8217;ll have to bring the pass-by-reference signature back when you start writing move constructors.  The pass-by-value idiom creates an ambiguity otherwise</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Andrzej Krzemieński</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1785</link>
		<dc:creator>Andrzej Krzemieński</dc:creator>
		<pubDate>Mon, 21 Nov 2011 13:21:10 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1785</guid>
		<description>&lt;p&gt;Hi Dave,
I tried to apply the idiom for copy assignment you describe, but I encountered one suspicious nuance when trying to specify exception specification for my copy assignment. I have a &quot;gut feeling&quot; that something is wrong, although I cannot clearly specify it. Here is my problem. Without passing by value I would specify my assigment like this:&lt;/p&gt;

&lt;pre&gt;T&amp; T::operator=&#040; T const&amp; x &#041;
&#123; 
  T tmp&#040;x&#041;;          // can throw
  swap&#040;*this, tmp&#041;;  // no-throw (let&#039;s assume)
  return *this; 
&#125;&lt;/pre&gt;

&lt;p&gt;This really says what I need to do to assign the value from one object represented by reference &#039;x&#039;. I need to make a copy first, and swap it with my value. Can this operation throw? Surely: a copy constructor is a typical place where one would expect a throw.&lt;/p&gt;

&lt;p&gt;Now, is the answer the same for the &quot;pass-by-value&quot; idiom?&lt;/p&gt;

&lt;pre&gt;T&amp; operator=&#040;T x&#041;    // this copying is not inside the assignment
&#123;
    swap&#040;*this, x&#041;;  // no-throw
    return *this;  
&#125;&lt;/pre&gt;

&lt;p&gt;Technically we are doing the same thing, but copying is somehow ejected outside of the function. The function only does a no-fail (let&#039;s assume that) swap. So in fact, I can write:&lt;/p&gt;

&lt;pre&gt;T&amp; operator=&#040;T x&#041; noexcept
&#123;
    swap&#040;*this, x&#041;;  // no-throw
    return *this;  
&#125;&lt;/pre&gt;

&lt;p&gt;I am telling the truth: there is nothing in the copy assignment that could cause a throw. But anyone who tries to use the assignment may throw, beacuse our function even thouh it does not copy itself, forces you to copy T, even though you are not (or may be not) aware of it. That is, by declaring the function like this (with noexcept), while technically being correct, I confuse everyone by implying that using this assignment operator does not raise exceptions. I would be more honest if I wrote:&lt;/p&gt;

&lt;pre&gt;T&amp; operator=&#040;T x&#041; noexcept&#040; std::is_nothrow_copy_constructible&lt;T&gt;::value &#041;
&#123;
    swap&#040;*this, x&#041;;  // no-throw
    return *this;  
&#125;&lt;/pre&gt;

&lt;p&gt;But this also looks strange: why would I base the condition on the properties of the constructor that I never call?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Dave,
I tried to apply the idiom for copy assignment you describe, but I encountered one suspicious nuance when trying to specify exception specification for my copy assignment. I have a &#8220;gut feeling&#8221; that something is wrong, although I cannot clearly specify it. Here is my problem. Without passing by value I would specify my assigment like this:</p>

<pre>T&amp; T::operator=&#040; T const&amp; x &#041;
&#123; 
  T tmp&#040;x&#041;;          // can throw
  swap&#040;*this, tmp&#041;;  // no-throw (let's assume)
  return *this; 
&#125;</pre>

<p>This really says what I need to do to assign the value from one object represented by reference &#8216;x&#8217;. I need to make a copy first, and swap it with my value. Can this operation throw? Surely: a copy constructor is a typical place where one would expect a throw.</p>

<p>Now, is the answer the same for the &#8220;pass-by-value&#8221; idiom?</p>

<pre>T&amp; operator=&#040;T x&#041;    // this copying is not inside the assignment
&#123;
    swap&#040;*this, x&#041;;  // no-throw
    return *this;  
&#125;</pre>

<p>Technically we are doing the same thing, but copying is somehow ejected outside of the function. The function only does a no-fail (let&#8217;s assume that) swap. So in fact, I can write:</p>

<pre>T&amp; operator=&#040;T x&#041; noexcept
&#123;
    swap&#040;*this, x&#041;;  // no-throw
    return *this;  
&#125;</pre>

<p>I am telling the truth: there is nothing in the copy assignment that could cause a throw. But anyone who tries to use the assignment may throw, beacuse our function even thouh it does not copy itself, forces you to copy T, even though you are not (or may be not) aware of it. That is, by declaring the function like this (with noexcept), while technically being correct, I confuse everyone by implying that using this assignment operator does not raise exceptions. I would be more honest if I wrote:</p>

<pre>T&amp; operator=&#040;T x&#041; noexcept&#040; std::is_nothrow_copy_constructible&lt;T&gt;::value &#041;
&#123;
    swap&#040;*this, x&#041;;  // no-throw
    return *this;  
&#125;</pre>

<p>But this also looks strange: why would I base the condition on the properties of the constructor that I never call?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Knowing me, knowing you, a-ha</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1684</link>
		<dc:creator>Knowing me, knowing you, a-ha</dc:creator>
		<pubDate>Tue, 11 Oct 2011 07:45:27 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1684</guid>
		<description>&lt;p&gt;Thanks Dave, missed the part about template params&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks Dave, missed the part about template params</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Knowing me, knowing you, a-ha</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1683</link>
		<dc:creator>Knowing me, knowing you, a-ha</dc:creator>
		<pubDate>Tue, 11 Oct 2011 06:35:15 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1683</guid>
		<description>&lt;p&gt;Yes, he talks about template parameters.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Yes, he talks about template parameters.</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-1682</link>
		<dc:creator>Dave Abrahams</dc:creator>
		<pubDate>Tue, 11 Oct 2011 06:13:50 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1682</guid>
		<description>&lt;p&gt;No, unless that integer is a non-type template parameter, they&#039;re both lvalues.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>No, unless that integer is a non-type template parameter, they&#8217;re both lvalues.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Knowing me, knowing you, a-ha</title>
		<link>http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1681</link>
		<dc:creator>Knowing me, knowing you, a-ha</dc:creator>
		<pubDate>Tue, 11 Oct 2011 05:59:21 +0000</pubDate>
		<guid isPermaLink="false">http://cpp-next.com/?p=188#comment-1681</guid>
		<description>&lt;p&gt;So let me get this straight, named integer isn&#039;t lvalue but named float is?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>So let me get this straight, named integer isn&#8217;t lvalue but named float is?</p>]]></content:encoded>
	</item>
</channel>
</rss>

