<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments for Sutter’s Mill</title>
	<atom:link href="http://herbsutter.com/comments/feed/" rel="self" type="application/rss+xml" />
	<link>http://herbsutter.com</link>
	<description>Herb Sutter on software, hardware, and concurrency</description>
	<lastBuildDate>Sat, 18 May 2013 00:29:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by David Thomas</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10206</link>
		<dc:creator><![CDATA[David Thomas]]></dc:creator>
		<pubDate>Sat, 18 May 2013 00:29:16 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10206</guid>
		<description><![CDATA[Since employee supports find(..., name), instead of e.name() == name the lambda could contain e == name.  If name() returns a reference to const string, then I suspect it won&#039;t matter.  If name() returns a string, then I suspect we are paying price greater than e == name (on each iteration).  Writing the lambda to avoid the potential temporaries yeidlds:

[code]
string find_addr( const list&lt;employee&gt;&amp; emps, const string&amp; name ) {
    const auto i = find_if( begin(emps), end(emps),
                      [&amp;](const auto&amp; e) { return e == name; } );
    return i != end(emps) ? i-&gt;addr : &quot;&quot;;
}
[/code]

On the compiler that I checked, if name() returns a const string&amp;, then e == name optimizes to the same code as e.name() == name. No penalty here.  However, if name() returns a string, then the impact is significant (the complexity prevented inlining and temporaries were created).]]></description>
		<content:encoded><![CDATA[<p>Since employee supports find(&#8230;, name), instead of e.name() == name the lambda could contain e == name.  If name() returns a reference to const string, then I suspect it won&#8217;t matter.  If name() returns a string, then I suspect we are paying price greater than e == name (on each iteration).  Writing the lambda to avoid the potential temporaries yeidlds:</p>
<pre class="brush: plain; title: ; notranslate">
string find_addr( const list&lt;employee&gt;&amp; emps, const string&amp; name ) {
    const auto i = find_if( begin(emps), end(emps),
                      [&amp;](const auto&amp; e) { return e == name; } );
    return i != end(emps) ? i-&gt;addr : &quot;&quot;;
}
</pre>
<p>On the compiler that I checked, if name() returns a const string&amp;, then e == name optimizes to the same code as e.name() == name. No penalty here.  However, if name() returns a string, then the impact is significant (the complexity prevented inlining and temporaries were created).</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3: Using the Standard Library (or, Temporaries Revisited) (3/10) by Róbert Dávid</title>
		<link>http://herbsutter.com/2013/05/13/gotw-3-using-the-standard-library-or-temporaries-revisited-310/#comment-10204</link>
		<dc:creator><![CDATA[Róbert Dávid]]></dc:creator>
		<pubDate>Fri, 17 May 2013 23:16:16 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1888#comment-10204</guid>
		<description><![CDATA[@Olaf van der Spek: The other guys already told about find_if being self documenting: To discover that this code is doing a search, you only need to read the function name in the find_if case, but for range for, you need to check the loop body, and if the condition is more complex than this, you might need to take more than 0.1s to discover that it is a search. It&#039;s also less error prone (no silently compiling mistakes e.g. if( name = emp.name() ) ).

And if you are into unit testing, you would also like that find_if version has only 2 branches (I&#039;m not really into testing STL implementation), while the range-for version has 4 (depending on how you define branches), so you need more test cases for 100% code coverage.]]></description>
		<content:encoded><![CDATA[<p>@Olaf van der Spek: The other guys already told about find_if being self documenting: To discover that this code is doing a search, you only need to read the function name in the find_if case, but for range for, you need to check the loop body, and if the condition is more complex than this, you might need to take more than 0.1s to discover that it is a search. It&#8217;s also less error prone (no silently compiling mistakes e.g. if( name = emp.name() ) ).</p>
<p>And if you are into unit testing, you would also like that find_if version has only 2 branches (I&#8217;m not really into testing STL implementation), while the range-for version has 4 (depending on how you define branches), so you need more test cases for 100% code coverage.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by litb1</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10203</link>
		<dc:creator><![CDATA[litb1]]></dc:creator>
		<pubDate>Fri, 17 May 2013 22:33:34 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10203</guid>
		<description><![CDATA[You also need to consider multi element initializer lists when making a constructor &quot;explicit&quot; (see the tuple &quot;explicit&quot; constructor).

The question becomes, do you want this to work?

    void f (complex c);
    f ({ 0.f, 1.f });

Simply making the two parameter constructor explicit just to guard against the one-argument case then is unsuitable because it guards against the multi arg case too. You will need to overload it (possibly using constructor delegation in the implementation).]]></description>
		<content:encoded><![CDATA[<p>You also need to consider multi element initializer lists when making a constructor &#8220;explicit&#8221; (see the tuple &#8220;explicit&#8221; constructor).</p>
<p>The question becomes, do you want this to work?</p>
<p>    void f (complex c);<br />
    f ({ 0.f, 1.f });</p>
<p>Simply making the two parameter constructor explicit just to guard against the one-argument case then is unsuitable because it guards against the multi arg case too. You will need to overload it (possibly using constructor delegation in the implementation).</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by GregM</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10202</link>
		<dc:creator><![CDATA[GregM]]></dc:creator>
		<pubDate>Fri, 17 May 2013 21:47:01 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10202</guid>
		<description><![CDATA[&quot;Just focusing one issue I haven’t seen mentioned by others already, I’d question the appropriateness of offering the increment operators at all.&quot;

I had a similar thought when I read the class, &quot;what exactly does it mean to increment a complex number?&quot;.  I don&#039;t think I&#039;ve ever used increment on a floating point number.]]></description>
		<content:encoded><![CDATA[<p>&#8220;Just focusing one issue I haven’t seen mentioned by others already, I’d question the appropriateness of offering the increment operators at all.&#8221;</p>
<p>I had a similar thought when I read the class, &#8220;what exactly does it mean to increment a complex number?&#8221;.  I don&#8217;t think I&#8217;ve ever used increment on a floating point number.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Klaim - Joel Lamotte (@MJKlaim)</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10201</link>
		<dc:creator><![CDATA[Klaim - Joel Lamotte (@MJKlaim)]]></dc:creator>
		<pubDate>Fri, 17 May 2013 21:43:41 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10201</guid>
		<description><![CDATA[To make an easy to use interface:

1. Make it impossible or very hard to use incorrectly (fail to compile, crashes, etc when used incorrectly - see std::chrono as a simple example) - this is the most important guideline for type design because it both prevent errors by mistake and, more importantly, it drives the usage (it&#039;s like walls for a blind: when you touch it you know you can&#039;t go this way);
2. Make sure each operation have only one way to be done - this actually improve composition (note that if two functions does the same thing but in a different way that impact the semantic, it&#039;s two operations);
3. Hide complexity. Unfortunately it is not always efficient to hide totally the implementation details of our types (maybe it will be with modules but don&#039;t count on it until we got implementations), but if you can hide the details, the only thing visible is how to use it.
4. Give a unique responsability to the type, not more.
5. Make it as short as possible, that is, a minimum of functions (depends on the domain and responsability);
6. Use names as obvious as possible in the domain context;
7. Document the interface. I put this at last because I consider it of last resort but you always end up doing it for member functions doing not that obvious operations.]]></description>
		<content:encoded><![CDATA[<p>To make an easy to use interface:</p>
<p>1. Make it impossible or very hard to use incorrectly (fail to compile, crashes, etc when used incorrectly &#8211; see std::chrono as a simple example) &#8211; this is the most important guideline for type design because it both prevent errors by mistake and, more importantly, it drives the usage (it&#8217;s like walls for a blind: when you touch it you know you can&#8217;t go this way);<br />
2. Make sure each operation have only one way to be done &#8211; this actually improve composition (note that if two functions does the same thing but in a different way that impact the semantic, it&#8217;s two operations);<br />
3. Hide complexity. Unfortunately it is not always efficient to hide totally the implementation details of our types (maybe it will be with modules but don&#8217;t count on it until we got implementations), but if you can hide the details, the only thing visible is how to use it.<br />
4. Give a unique responsability to the type, not more.<br />
5. Make it as short as possible, that is, a minimum of functions (depends on the domain and responsability);<br />
6. Use names as obvious as possible in the domain context;<br />
7. Document the interface. I put this at last because I consider it of last resort but you always end up doing it for member functions doing not that obvious operations.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by GregM</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10200</link>
		<dc:creator><![CDATA[GregM]]></dc:creator>
		<pubDate>Fri, 17 May 2013 21:42:44 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10200</guid>
		<description><![CDATA[Dave, I asked the strlen question in GoTW #1, and Herb assures me that it&#039;s not there is a &quot;good&quot; string library.  It checks for the first character being 0 before it calls strlen.

While equality may feel simpler than inequality, I generally prefer to check for success rather than failure when the two options are basically equivalent.]]></description>
		<content:encoded><![CDATA[<p>Dave, I asked the strlen question in GoTW #1, and Herb assures me that it&#8217;s not there is a &#8220;good&#8221; string library.  It checks for the first character being 0 before it calls strlen.</p>
<p>While equality may feel simpler than inequality, I generally prefer to check for success rather than failure when the two options are basically equivalent.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #1 Solution: Variable Initialization – or Is It? by Seungbum Woo</title>
		<link>http://herbsutter.com/2013/05/09/gotw-1-solution/#comment-10199</link>
		<dc:creator><![CDATA[Seungbum Woo]]></dc:creator>
		<pubDate>Fri, 17 May 2013 21:14:28 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1831#comment-10199</guid>
		<description><![CDATA[I am using GCC 4.8.1 under Windows7 ( using MinGW-w64 )   I am using an experimental version of MinGW-w64 for its std::thread support.  I should try MinGW-w64&#039;s released version.

I confirmed that VC++ 2012 Nov CTP works fine with v{vv}.

Thank you for your answer.]]></description>
		<content:encoded><![CDATA[<p>I am using GCC 4.8.1 under Windows7 ( using MinGW-w64 )   I am using an experimental version of MinGW-w64 for its std::thread support.  I should try MinGW-w64&#8242;s released version.</p>
<p>I confirmed that VC++ 2012 Nov CTP works fine with v{vv}.</p>
<p>Thank you for your answer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Dave Harris</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10198</link>
		<dc:creator><![CDATA[Dave Harris]]></dc:creator>
		<pubDate>Fri, 17 May 2013 20:48:57 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10198</guid>
		<description><![CDATA[Interfaces are easy to use correctly if they follow established conventions and best practices. (Ideally your established conventions will reflect best practice, but best practice evolves, so the longer your conventions have been established the greater the danger they are out of date.) They should follow the principle of least surprise. Ideally a reasonably skilled programmer using them would not need to look up the documentation to see how they work.

The example class is presumably intended to be a numeric type, so it should follow the conventions established by int, double and other numeric types. It doesn&#039;t. Note that its definition of operator+() isn&#039;t [i]intrinsically[/i] wrong; it isn&#039;t impossible to use correctly: but it is different to how operator+() works for double so very liable to be used incorrectly. It doesn&#039;t matter if the documentation describes the idiosyncratic behaviour correctly and lucidly. Few programmers will think to read it.

Programmers have ample opportunities to be creative. Deciding the behaviour of operator+() is not one of them.]]></description>
		<content:encoded><![CDATA[<p>Interfaces are easy to use correctly if they follow established conventions and best practices. (Ideally your established conventions will reflect best practice, but best practice evolves, so the longer your conventions have been established the greater the danger they are out of date.) They should follow the principle of least surprise. Ideally a reasonably skilled programmer using them would not need to look up the documentation to see how they work.</p>
<p>The example class is presumably intended to be a numeric type, so it should follow the conventions established by int, double and other numeric types. It doesn&#8217;t. Note that its definition of operator+() isn&#8217;t [i]intrinsically[/i] wrong; it isn&#8217;t impossible to use correctly: but it is different to how operator+() works for double so very liable to be used incorrectly. It doesn&#8217;t matter if the documentation describes the idiosyncratic behaviour correctly and lucidly. Few programmers will think to read it.</p>
<p>Programmers have ample opportunities to be creative. Deciding the behaviour of operator+() is not one of them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Mikhail Belyaev</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10197</link>
		<dc:creator><![CDATA[Mikhail Belyaev]]></dc:creator>
		<pubDate>Fri, 17 May 2013 20:45:13 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10197</guid>
		<description><![CDATA[Another thought: if we do not make it a template (which we possibly do not want) the whole class is actually better off being a constexpr class. It won&#039;t hurt, it&#039;ll help in some cases and it is certainly a good thing to do with classes this simple.
It won&#039;t optimize anything, but lets you actually say in your code that that whole bunch of complex arithmetics on constants should run at compile-time.]]></description>
		<content:encoded><![CDATA[<p>Another thought: if we do not make it a template (which we possibly do not want) the whole class is actually better off being a constexpr class. It won&#8217;t hurt, it&#8217;ll help in some cases and it is certainly a good thing to do with classes this simple.<br />
It won&#8217;t optimize anything, but lets you actually say in your code that that whole bunch of complex arithmetics on constants should run at compile-time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #1 Solution: Variable Initialization – or Is It? by Herb Sutter</title>
		<link>http://herbsutter.com/2013/05/09/gotw-1-solution/#comment-10196</link>
		<dc:creator><![CDATA[Herb Sutter]]></dc:creator>
		<pubDate>Fri, 17 May 2013 20:33:57 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1831#comment-10196</guid>
		<description><![CDATA[@Seungbum Woo: Looks like a compiler bug. What compiler and environment are you using?

I just tried:

- GCC 4.8 and VC++ 2012 Nov CTP which both like it;

- GCC 4.7.2 which thinks F f{a}; is illegal; and

- Intel ICC 13.0.1 which thinks v{vv} is illegal.

I believe GCC 4.8 and VC++ 2012 Nov CTP have it right.]]></description>
		<content:encoded><![CDATA[<p>@Seungbum Woo: Looks like a compiler bug. What compiler and environment are you using?</p>
<p>I just tried:</p>
<p>- GCC 4.8 and VC++ 2012 Nov CTP which both like it;</p>
<p>- GCC 4.7.2 which thinks F f{a}; is illegal; and</p>
<p>- Intel ICC 13.0.1 which thinks v{vv} is illegal.</p>
<p>I believe GCC 4.8 and VC++ 2012 Nov CTP have it right.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Dave Harris</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10195</link>
		<dc:creator><![CDATA[Dave Harris]]></dc:creator>
		<pubDate>Fri, 17 May 2013 20:19:29 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10195</guid>
		<description><![CDATA[Am I the only person who would prefer:[code]
return i == end(emps) ? &quot;&quot; : i-&gt;addr;
[/code]
Equality feels slightly simpler than inequality. I&#039;d also prefer to avoid converting the c-string to a std::string:[code]
return i == end(emps) ? string() : i-&gt;addr;
[/code]
The conversion probably won&#039;t allocate memory, but it&#039;s still doing unnecessary work - possibly including a call to strlen(), for example.]]></description>
		<content:encoded><![CDATA[<p>Am I the only person who would prefer:
<pre class="brush: plain; title: ; notranslate">
return i == end(emps) ? &quot;&quot; : i-&gt;addr;
</pre>
<p>Equality feels slightly simpler than inequality. I&#8217;d also prefer to avoid converting the c-string to a std::string:
<pre class="brush: plain; title: ; notranslate">
return i == end(emps) ? string() : i-&gt;addr;
</pre>
<p>The conversion probably won&#8217;t allocate memory, but it&#8217;s still doing unnecessary work &#8211; possibly including a call to strlen(), for example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Herb Sutter</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10194</link>
		<dc:creator><![CDATA[Herb Sutter]]></dc:creator>
		<pubDate>Fri, 17 May 2013 20:02:38 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10194</guid>
		<description><![CDATA[@Olaf: OK, clarified (in GotW #2) along the lines that “I’m assuming something like .name() is available and .name() == name has the same semantics.”

@Sam: Good point, noted.

@Adrian: A program that does that has undefined behavior and could achieve the same effect by dereferencing a random int cast to a pointer. Within defined behavior, the range-for can iterate only one way, from the front in order visiting every element exactly once, and the only possibly variation in behavior is that it could stop early if there&#039;s a break or return inside vs. visiting all elements.]]></description>
		<content:encoded><![CDATA[<p>@Olaf: OK, clarified (in GotW #2) along the lines that “I’m assuming something like .name() is available and .name() == name has the same semantics.”</p>
<p>@Sam: Good point, noted.</p>
<p>@Adrian: A program that does that has undefined behavior and could achieve the same effect by dereferencing a random int cast to a pointer. Within defined behavior, the range-for can iterate only one way, from the front in order visiting every element exactly once, and the only possibly variation in behavior is that it could stop early if there&#8217;s a break or return inside vs. visiting all elements.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #2 Solution: Temporary Objects by Herb Sutter</title>
		<link>http://herbsutter.com/2013/05/13/gotw-2-solution-temporary-objects/#comment-10193</link>
		<dc:creator><![CDATA[Herb Sutter]]></dc:creator>
		<pubDate>Fri, 17 May 2013 19:51:29 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1874#comment-10193</guid>
		<description><![CDATA[@Robert: That&#039;s not the same example as in the article. :) I agree your two lines are equivalent, and in both cases the calling code can easily see the bug locally in code review just by knowing the return type of the function (by value). What&#039;s different to me in find_addr is that we&#039;d be returning a reference that refers, not &quot;directly within this here object I&#039;m returning,&quot; but &quot;deep within some data structure plus I&#039;m not going to tell you exactly which node it is either.&quot; The article was doing the latter, and there&#039;s no way to check the calling code&#039;s correctness during code review without carefully reading the English documentation of find_addr to figure out if the reference was still valid. Doing the former, returning a reference directly into this visible object the caller knows about, is much less problematic IMO and does not require English documentation analysis to spot the bug.

@tivrfoa: I just compared using &quot;no optimizations&quot; vs. &quot;max optimizations&quot; compiler flags.]]></description>
		<content:encoded><![CDATA[<p>@Robert: That&#8217;s not the same example as in the article. :) I agree your two lines are equivalent, and in both cases the calling code can easily see the bug locally in code review just by knowing the return type of the function (by value). What&#8217;s different to me in find_addr is that we&#8217;d be returning a reference that refers, not &#8220;directly within this here object I&#8217;m returning,&#8221; but &#8220;deep within some data structure plus I&#8217;m not going to tell you exactly which node it is either.&#8221; The article was doing the latter, and there&#8217;s no way to check the calling code&#8217;s correctness during code review without carefully reading the English documentation of find_addr to figure out if the reference was still valid. Doing the former, returning a reference directly into this visible object the caller knows about, is much less problematic IMO and does not require English documentation analysis to spot the bug.</p>
<p>@tivrfoa: I just compared using &#8220;no optimizations&#8221; vs. &#8220;max optimizations&#8221; compiler flags.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by mttpd</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10192</link>
		<dc:creator><![CDATA[mttpd]]></dc:creator>
		<pubDate>Fri, 17 May 2013 17:32:26 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10192</guid>
		<description><![CDATA[Good points in the previous comments.

One more thing which comes to mind -- given that it makes sense for &quot;complex&quot; to have value semantics (as opposed to reference semantics), we&#039;d probably want to consider preventing inheritance (for the same reason it&#039;s not a good idea to inherit from an STL container).

As in:
Guideline #4: A base class destructor should be either public and virtual, or protected and nonvirtual. 
http://www.gotw.ca/publications/mill18.htm

I don&#039;t see the case for public &amp; virtual (since I don&#039;t see the case for our &quot;complex&quot; class having reference semantics). Hence, I&#039;d go with the other alternative.

This is where C++11 comes in, as it allows us to use &quot;final&quot; to prevent inheritance:
http://en.cppreference.com/w/cpp/language/final]]></description>
		<content:encoded><![CDATA[<p>Good points in the previous comments.</p>
<p>One more thing which comes to mind &#8212; given that it makes sense for &#8220;complex&#8221; to have value semantics (as opposed to reference semantics), we&#8217;d probably want to consider preventing inheritance (for the same reason it&#8217;s not a good idea to inherit from an STL container).</p>
<p>As in:<br />
Guideline #4: A base class destructor should be either public and virtual, or protected and nonvirtual.<br />
<a href="http://www.gotw.ca/publications/mill18.htm" rel="nofollow">http://www.gotw.ca/publications/mill18.htm</a></p>
<p>I don&#8217;t see the case for public &amp; virtual (since I don&#8217;t see the case for our &#8220;complex&#8221; class having reference semantics). Hence, I&#8217;d go with the other alternative.</p>
<p>This is where C++11 comes in, as it allows us to use &#8220;final&#8221; to prevent inheritance:<br />
<a href="http://en.cppreference.com/w/cpp/language/final" rel="nofollow">http://en.cppreference.com/w/cpp/language/final</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Adrian</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10190</link>
		<dc:creator><![CDATA[Adrian]]></dc:creator>
		<pubDate>Fri, 17 May 2013 17:11:09 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10190</guid>
		<description><![CDATA[Just focusing one issue I haven&#039;t seen mentioned by others already, I&#039;d question the appropriateness of offering the increment operators at all.

The increment and decrement operators make intuitive sense for discrete types, like counters, chars, certain enumerations, even bools.

Perhaps because I&#039;ve always thought of these operators as analogues of Pascal&#039;s succ() and pred(), it&#039;s always seemed odd to me that C and C++ provide them for floating point types.  What does it mean to increment a type that approximates a continuous quantity?  It&#039;s not intuitive (to me), given a double d, what [code]++d[/code] would mean.  Does it increment by 1.0 or does it increment by some epsilon to get to the next higher, representable value?  Both would be useful, and, given that I could always write [code]d += 1.0;[/code] for the former, defining operator++ for the latter would make the most sense.  But alas, that&#039;s not the case.  That might just be my own bias.

Even if I make peace with increment and decrement on a double, what should they mean on a complex?  That&#039;s even less intuitive.  A complex is a two-dimensional quantity.  Should increment affect the real part or the imaginary part or both?  It&#039;s simply not obvious--at least not to everyone.

In the interest of making it easy to use correctly and difficult to use incorrectly, I&#039;d leave out the increment and decrement operators for this class at least until a client of the class made a strong case for them.  (A strong case might be that having these operators enables a class or function template that otherwise can&#039;t use them.)  In the mean time, a client can write [code]c += 1.0;[/code], which is not only correct, it&#039;s _obviously_ correct.  To everyone.

To me, that&#039;s the difference between a well-implemented class interface and a well-_designed_ one.]]></description>
		<content:encoded><![CDATA[<p>Just focusing one issue I haven&#8217;t seen mentioned by others already, I&#8217;d question the appropriateness of offering the increment operators at all.</p>
<p>The increment and decrement operators make intuitive sense for discrete types, like counters, chars, certain enumerations, even bools.</p>
<p>Perhaps because I&#8217;ve always thought of these operators as analogues of Pascal&#8217;s succ() and pred(), it&#8217;s always seemed odd to me that C and C++ provide them for floating point types.  What does it mean to increment a type that approximates a continuous quantity?  It&#8217;s not intuitive (to me), given a double d, what
<pre class="brush: plain; title: ; notranslate">++d</pre>
<p> would mean.  Does it increment by 1.0 or does it increment by some epsilon to get to the next higher, representable value?  Both would be useful, and, given that I could always write
<pre class="brush: plain; title: ; notranslate">d += 1.0;</pre>
<p> for the former, defining operator++ for the latter would make the most sense.  But alas, that&#8217;s not the case.  That might just be my own bias.</p>
<p>Even if I make peace with increment and decrement on a double, what should they mean on a complex?  That&#8217;s even less intuitive.  A complex is a two-dimensional quantity.  Should increment affect the real part or the imaginary part or both?  It&#8217;s simply not obvious&#8211;at least not to everyone.</p>
<p>In the interest of making it easy to use correctly and difficult to use incorrectly, I&#8217;d leave out the increment and decrement operators for this class at least until a client of the class made a strong case for them.  (A strong case might be that having these operators enables a class or function template that otherwise can&#8217;t use them.)  In the mean time, a client can write
<pre class="brush: plain; title: ; notranslate">c += 1.0;</pre>
<p>, which is not only correct, it&#8217;s _obviously_ correct.  To everyone.</p>
<p>To me, that&#8217;s the difference between a well-implemented class interface and a well-_designed_ one.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Adrian</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10189</link>
		<dc:creator><![CDATA[Adrian]]></dc:creator>
		<pubDate>Fri, 17 May 2013 16:34:41 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10189</guid>
		<description><![CDATA[&quot;When you see B [the range-based for loop], you know for certain without inspecting the body of the loop that it is a loop that visits the element of emps in order.&quot;  Certainly there are some things the body _might_ do that would contradict that.  For example, if the body modifies the container in a way that would invalidate outstanding iterators, you no longer can be sure that the loop will proceed normally. Consider:

[code]
for (const auto &amp; e : emps) {
  if (e.name == &quot;Herb&quot;) {
    emps.push_front(employee(&quot;Bjarne&quot;));
  }
}
[/code]

Obviously, this isn&#039;t an advisable thing to do in the loop, but the compiler isn&#039;t likely to notice.  Thus a range-based for loop might give you more confidence that it visits each element in order, but, unless emps is const, you cannot be absolutely certain without looking inside the body and without knowing the type of container and that container type&#039;s iterator invalidation rules.  Right?]]></description>
		<content:encoded><![CDATA[<p>&#8220;When you see B [the range-based for loop], you know for certain without inspecting the body of the loop that it is a loop that visits the element of emps in order.&#8221;  Certainly there are some things the body _might_ do that would contradict that.  For example, if the body modifies the container in a way that would invalidate outstanding iterators, you no longer can be sure that the loop will proceed normally. Consider:</p>
<pre class="brush: plain; title: ; notranslate">
for (const auto &amp; e : emps) {
  if (e.name == &quot;Herb&quot;) {
    emps.push_front(employee(&quot;Bjarne&quot;));
  }
}
</pre>
<p>Obviously, this isn&#8217;t an advisable thing to do in the loop, but the compiler isn&#8217;t likely to notice.  Thus a range-based for loop might give you more confidence that it visits each element in order, but, unless emps is const, you cannot be absolutely certain without looking inside the body and without knowing the type of container and that container type&#8217;s iterator invalidation rules.  Right?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Sam Kramer</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10185</link>
		<dc:creator><![CDATA[Sam Kramer]]></dc:creator>
		<pubDate>Fri, 17 May 2013 15:32:38 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10185</guid>
		<description><![CDATA[The astute reader will have noticed that the guidance from this tip is derived directly from the book Effective STL, Item 43: Prefer algorithm calls to hand-written loops. It&#039;s good to know that best practices from C++98 are still largely relevant in C++11.]]></description>
		<content:encoded><![CDATA[<p>The astute reader will have noticed that the guidance from this tip is derived directly from the book Effective STL, Item 43: Prefer algorithm calls to hand-written loops. It&#8217;s good to know that best practices from C++98 are still largely relevant in C++11.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #2 Solution: Temporary Objects by tivrfoa</title>
		<link>http://herbsutter.com/2013/05/13/gotw-2-solution-temporary-objects/#comment-10183</link>
		<dc:creator><![CDATA[tivrfoa]]></dc:creator>
		<pubDate>Fri, 17 May 2013 15:21:34 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1874#comment-10183</guid>
		<description><![CDATA[Hello. &quot;with aggressive optimizations&quot;. How are these &quot;aggressive optimizations&quot;? Could someone show an example? Thank you.]]></description>
		<content:encoded><![CDATA[<p>Hello. &#8220;with aggressive optimizations&#8221;. How are these &#8220;aggressive optimizations&#8221;? Could someone show an example? Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Rainer Blome</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10180</link>
		<dc:creator><![CDATA[Rainer Blome]]></dc:creator>
		<pubDate>Fri, 17 May 2013 13:00:58 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10180</guid>
		<description><![CDATA[1. What makes interfaces “easy to use correctly, hard to use incorrectly”?

The first answer coming to my mind is &quot;Documentation&quot;.
The standard classes are quite well documented - imagine how difficult it would be to use them correctly without this documentation, and how easy it would be to use them incorrectly.

It makes me a little sad that no one mentioned this so far.

Among the very effective programmers that I got to know, the share of those who actually write good documentation is surprisingly low. There are even some who explicitly argue against documentation. I often wonder why this is so.

Reasons might be:

* Documentation is (usually) not being paid for. What usually matters is functionality.

* Not every one is a born writer. Some very effective programmers that I got to know write surprisingly bad natural language. My guess is that most of these actually know this (but some don&#039;t :-). Many people dislike doing things that they are not good at. Thus I assume that some simply hate writing documentation because it makes them feel bad, or even inadequate.

* Writing good documentation is a skill that by most people must be learned. This takes at least effort and time, and thereby usually money.

* Tools that check for language errors in documentation are not widespread. This is in contrast to the code, where the compiler and tools like lint can help to work out the kinks.

Granted, in contexts where the code is not likely to ever be read, writing documentation has only one benefit that I can think of: The writer can hone his writing skills. 

However, in contexts where the code is likely to be read, maintained or reused, documentation is crucial. In my professional experience (15y), this is the case in by far the majority of projects. In some circumstances, especially when key people leave a project, missing or bad documentation can become extremely expensive.]]></description>
		<content:encoded><![CDATA[<p>1. What makes interfaces “easy to use correctly, hard to use incorrectly”?</p>
<p>The first answer coming to my mind is &#8220;Documentation&#8221;.<br />
The standard classes are quite well documented &#8211; imagine how difficult it would be to use them correctly without this documentation, and how easy it would be to use them incorrectly.</p>
<p>It makes me a little sad that no one mentioned this so far.</p>
<p>Among the very effective programmers that I got to know, the share of those who actually write good documentation is surprisingly low. There are even some who explicitly argue against documentation. I often wonder why this is so.</p>
<p>Reasons might be:</p>
<p>* Documentation is (usually) not being paid for. What usually matters is functionality.</p>
<p>* Not every one is a born writer. Some very effective programmers that I got to know write surprisingly bad natural language. My guess is that most of these actually know this (but some don&#8217;t :-). Many people dislike doing things that they are not good at. Thus I assume that some simply hate writing documentation because it makes them feel bad, or even inadequate.</p>
<p>* Writing good documentation is a skill that by most people must be learned. This takes at least effort and time, and thereby usually money.</p>
<p>* Tools that check for language errors in documentation are not widespread. This is in contrast to the code, where the compiler and tools like lint can help to work out the kinks.</p>
<p>Granted, in contexts where the code is not likely to ever be read, writing documentation has only one benefit that I can think of: The writer can hone his writing skills. </p>
<p>However, in contexts where the code is likely to be read, maintained or reused, documentation is crucial. In my professional experience (15y), this is the case in by far the majority of projects. In some circumstances, especially when key people leave a project, missing or bad documentation can become extremely expensive.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Marc</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10177</link>
		<dc:creator><![CDATA[Marc]]></dc:creator>
		<pubDate>Fri, 17 May 2013 12:18:17 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10177</guid>
		<description><![CDATA[Just had to give it a final try.

[code]
class complex 
{
	// allow access to private members
	friend ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; c );

public:
	// doubles as default ctor
	complex( double r = 0, double i = 0 ) : real(r), imag(i) { }

	// to efficiently implement operator+ 
	complex&amp; operator+=(const complex&amp; c) 
	{
		real += c.real;
		imag += c.imag;

		return *this;
	}

	// return reference to result
	complex&amp; operator++()
	{        
		++real;        
		return *this;    
	}    

	// return unchanged object by value
	complex operator++( int ) 
	{        
		auto temp = *this;        
		++real;        
		return temp;    
	}   

	// ... more functions that complement the above ...
private:    
	double real, imag;
};

// IO operator cannot be a class member. 
// Class member ops require the left operand to be a class object
ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; c ) 
{        
	os &lt;&lt; &quot;(&quot; &lt;&lt; c.real &lt;&lt; &quot;,&quot; &lt;&lt; c.imag &lt;&lt; &quot;)&quot;;
	return os;
}

// allows implicit conversion of operands
complex operator+ ( const complex &amp;c1, const complex&amp; c2 ) 
{   
	complex c(c1);
	c += c2; 
	return c;
}  
[/code]]]></description>
		<content:encoded><![CDATA[<p>Just had to give it a final try.</p>
<pre class="brush: plain; title: ; notranslate">
class complex 
{
	// allow access to private members
	friend ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; c );

public:
	// doubles as default ctor
	complex( double r = 0, double i = 0 ) : real(r), imag(i) { }

	// to efficiently implement operator+ 
	complex&amp; operator+=(const complex&amp; c) 
	{
		real += c.real;
		imag += c.imag;

		return *this;
	}

	// return reference to result
	complex&amp; operator++()
	{        
		++real;        
		return *this;    
	}    

	// return unchanged object by value
	complex operator++( int ) 
	{        
		auto temp = *this;        
		++real;        
		return temp;    
	}   

	// ... more functions that complement the above ...
private:    
	double real, imag;
};

// IO operator cannot be a class member. 
// Class member ops require the left operand to be a class object
ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; c ) 
{        
	os &lt;&lt; &quot;(&quot; &lt;&lt; c.real &lt;&lt; &quot;,&quot; &lt;&lt; c.imag &lt;&lt; &quot;)&quot;;
	return os;
}

// allows implicit conversion of operands
complex operator+ ( const complex &amp;c1, const complex&amp; c2 ) 
{   
	complex c(c1);
	c += c2; 
	return c;
}  
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Barry</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10176</link>
		<dc:creator><![CDATA[Barry]]></dc:creator>
		<pubDate>Fri, 17 May 2013 11:46:43 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10176</guid>
		<description><![CDATA[This extra branch;
[code]return i != end(emps) ? i-&gt;addr : &quot;&quot;;[/code]

Find just returns an iterator that you still have to check. The range-for version just returns.]]></description>
		<content:encoded><![CDATA[<p>This extra branch;</p>
<pre class="brush: plain; title: ; notranslate">return i != end(emps) ? i-&gt;addr : &quot;&quot;;</pre>
<p>Find just returns an iterator that you still have to check. The range-for version just returns.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #2 Solution: Temporary Objects by Róbert Dávid</title>
		<link>http://herbsutter.com/2013/05/13/gotw-2-solution-temporary-objects/#comment-10175</link>
		<dc:creator><![CDATA[Róbert Dávid]]></dc:creator>
		<pubDate>Fri, 17 May 2013 09:14:37 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1874#comment-10175</guid>
		<description><![CDATA[@GregM: [code]std::get&lt;0&gt;()[/code] (blog ate my pointy braces) isn&#039;t a member function of std::tuple, still isn&#039;t returning by value..
@Herb: Wait a minute. Are you saying that [code]auto&amp; x = function_that_creates_widget_and_returns_by_value().get_name();[/code] is &#039;good&#039;, but [code]auto&amp; x = get_widget_name(function_that_creates_widget_and_returns_by_value());[/code] isn&#039;t? I so far failed to create any example where a get(xx) construct can have a dangling reference what is fixed just by replacing that call to a xx.get() construct, that&#039;s why I think the two are equivalent, thus should use the same guidelines for return type.]]></description>
		<content:encoded><![CDATA[<p>@GregM:
<pre class="brush: plain; title: ; notranslate">std::get&lt;0&gt;()</pre>
<p> (blog ate my pointy braces) isn&#8217;t a member function of std::tuple, still isn&#8217;t returning by value..<br />
@Herb: Wait a minute. Are you saying that
<pre class="brush: plain; title: ; notranslate">auto&amp; x = function_that_creates_widget_and_returns_by_value().get_name();</pre>
<p> is &#8216;good&#8217;, but
<pre class="brush: plain; title: ; notranslate">auto&amp; x = get_widget_name(function_that_creates_widget_and_returns_by_value());</pre>
<p> isn&#8217;t? I so far failed to create any example where a get(xx) construct can have a dangling reference what is fixed just by replacing that call to a xx.get() construct, that&#8217;s why I think the two are equivalent, thus should use the same guidelines for return type.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Brian M</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10174</link>
		<dc:creator><![CDATA[Brian M]]></dc:creator>
		<pubDate>Fri, 17 May 2013 09:07:25 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10174</guid>
		<description><![CDATA[Think I would as reviewer simply say use a std:: complex or other library and go off and have lunch!
But was never any good with made up exam questions....

ps Unfortunately saw similar when someone was writing a linked list class. It was a good lunch!]]></description>
		<content:encoded><![CDATA[<p>Think I would as reviewer simply say use a std:: complex or other library and go off and have lunch!<br />
But was never any good with made up exam questions&#8230;.</p>
<p>ps Unfortunately saw similar when someone was writing a linked list class. It was a good lunch!</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Brian M</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10173</link>
		<dc:creator><![CDATA[Brian M]]></dc:creator>
		<pubDate>Fri, 17 May 2013 08:56:39 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10173</guid>
		<description><![CDATA[One important thing is that abstraction does not always equate to better or more readable code.  The use of more familiar constructs is often a better overall solution, errors are more likely to be seen by the casual reader etc. Although the examples are fairly trivial some are certainly more comfortable and easier to read  and Jay gets it about right with his comment. The important thing to remember is that code is just a means to an end! If we are thinking at this level of trivia is there something else wrong?]]></description>
		<content:encoded><![CDATA[<p>One important thing is that abstraction does not always equate to better or more readable code.  The use of more familiar constructs is often a better overall solution, errors are more likely to be seen by the casual reader etc. Although the examples are fairly trivial some are certainly more comfortable and easier to read  and Jay gets it about right with his comment. The important thing to remember is that code is just a means to an end! If we are thinking at this level of trivia is there something else wrong?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Olaf van der Spek</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10172</link>
		<dc:creator><![CDATA[Olaf van der Spek]]></dc:creator>
		<pubDate>Fri, 17 May 2013 08:04:18 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10172</guid>
		<description><![CDATA[@Herb Sure. IMO you disregarded this note: &quot;Note: As with GotW #2, don’t change the semantics of the function, even though they could be improved.&quot;
Whether employee == string is valid and has the same semantics as employee::name() == string isn&#039;t given, so you shouldn&#039;t assume it is.]]></description>
		<content:encoded><![CDATA[<p>@Herb Sure. IMO you disregarded this note: &#8220;Note: As with GotW #2, don’t change the semantics of the function, even though they could be improved.&#8221;<br />
Whether employee == string is valid and has the same semantics as employee::name() == string isn&#8217;t given, so you shouldn&#8217;t assume it is.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by khurshid</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10171</link>
		<dc:creator><![CDATA[khurshid]]></dc:creator>
		<pubDate>Fri, 17 May 2013 08:00:19 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10171</guid>
		<description><![CDATA[Sorry,  forgot default ctor.

[code]
class complex{
   complex() = default;
   complex( const complex&amp; other) = default;
   complex&amp; operator = (const complex&amp; other) = default;
   // other method and ctors   ..............................
}
[/code]]]></description>
		<content:encoded><![CDATA[<p>Sorry,  forgot default ctor.</p>
<pre class="brush: plain; title: ; notranslate">
class complex{
   complex() = default;
   complex( const complex&amp; other) = default;
   complex&amp; operator = (const complex&amp; other) = default;
   // other method and ctors   ..............................
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by khurshid</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10170</link>
		<dc:creator><![CDATA[khurshid]]></dc:creator>
		<pubDate>Fri, 17 May 2013 07:50:29 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10170</guid>
		<description><![CDATA[[code]

class complex 
{
public:
   constexpr explicit complex( double r, double i = 0.0 ) noexcept
        : real_ { r }
       , imag_ { i }
    { }

   constexpr complex operator+ ( const complex&amp; other ) const noexcept
   {
        return complex( real_ + other.real_,  imag_ + other.imag_ );
    }

  complex&amp; operator++() noexcept
   {
        ++real_;
        return *this;
    }

    complex operator++( int )  noexcept
   {
        auto temp = *this;
        ++real_;
        return temp;
    }

    // ... more functions that complement the above ...
    
    constexpr double real()const noexcept { return real_; }
    constexpr double imaginary() const noexcept { return imag_;}
    
private:
    double real_ = 0.0;
    double imag_ = 0.0;
};

std::ostream&amp;  operator &lt;&lt; (std::ostream&amp; os,  const complex&amp; c)
{
       return os &lt;&lt; &#039;(&#039; &lt;&lt; c.real() &lt;&lt; &#039;,&#039; &lt;&lt; c.imaginary() &lt;&lt; &#039;)&#039; ;
}

[/code]]]></description>
		<content:encoded><![CDATA[<pre class="brush: plain; title: ; notranslate">

class complex 
{
public:
   constexpr explicit complex( double r, double i = 0.0 ) noexcept
        : real_ { r }
       , imag_ { i }
    { }

   constexpr complex operator+ ( const complex&amp; other ) const noexcept
   {
        return complex( real_ + other.real_,  imag_ + other.imag_ );
    }

  complex&amp; operator++() noexcept
   {
        ++real_;
        return *this;
    }

    complex operator++( int )  noexcept
   {
        auto temp = *this;
        ++real_;
        return temp;
    }

    // ... more functions that complement the above ...
    
    constexpr double real()const noexcept { return real_; }
    constexpr double imaginary() const noexcept { return imag_;}
    
private:
    double real_ = 0.0;
    double imag_ = 0.0;
};

std::ostream&amp;  operator &lt;&lt; (std::ostream&amp; os,  const complex&amp; c)
{
       return os &lt;&lt; '(' &lt;&lt; c.real() &lt;&lt; ',' &lt;&lt; c.imaginary() &lt;&lt; ')' ;
}

</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Marc</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10169</link>
		<dc:creator><![CDATA[Marc]]></dc:creator>
		<pubDate>Fri, 17 May 2013 07:48:05 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10169</guid>
		<description><![CDATA[But made error(s). So, back to work]]></description>
		<content:encoded><![CDATA[<p>But made error(s). So, back to work</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Marc</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10168</link>
		<dc:creator><![CDATA[Marc]]></dc:creator>
		<pubDate>Fri, 17 May 2013 07:36:27 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10168</guid>
		<description><![CDATA[Thought I&#039;d give it a try

[code]
using namespace std;

// IO operator cannot be a class member. 
// Class member ops require the left operand to be a class object
ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; c ) 
{        
	os &lt;&lt; &quot;(&quot; &lt;&lt; c.real &lt;&lt; &quot;,&quot; &lt;&lt; c.imag &lt;&lt; &quot;)&quot;;

	return os;
}

complex operator+ ( const&amp; complex c1, const&amp; complex c2 ) 
{   
	complex c = c1;
	c.real += c2.real; // &#039;+=&#039; assumed
	c.imag += c2.imag;

	return c;
}  

class complex 
{
	// allow access to private members
	friend ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; c );
	friend complex operator+ ( const&amp; complex c1, const&amp; complex c2 );

public:
	complex( double r = 0, double i = 0 ) : real(r), imag(i) { } // doubles as default ctor

	complex&amp; operator++() // return reference to result
	{        
		++real;        
		return *this;    
	}    

	complex operator++( int ) 
	{        
		auto temp = *this;        
		++real;        
		return temp;    
	}   

	// ... more functions that complement the above ...
private:    
	double real, imag;
};
[/code]]]></description>
		<content:encoded><![CDATA[<p>Thought I&#8217;d give it a try</p>
<pre class="brush: plain; title: ; notranslate">
using namespace std;

// IO operator cannot be a class member. 
// Class member ops require the left operand to be a class object
ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; c ) 
{        
	os &lt;&lt; &quot;(&quot; &lt;&lt; c.real &lt;&lt; &quot;,&quot; &lt;&lt; c.imag &lt;&lt; &quot;)&quot;;

	return os;
}

complex operator+ ( const&amp; complex c1, const&amp; complex c2 ) 
{   
	complex c = c1;
	c.real += c2.real; // '+=' assumed
	c.imag += c2.imag;

	return c;
}  

class complex 
{
	// allow access to private members
	friend ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; c );
	friend complex operator+ ( const&amp; complex c1, const&amp; complex c2 );

public:
	complex( double r = 0, double i = 0 ) : real(r), imag(i) { } // doubles as default ctor

	complex&amp; operator++() // return reference to result
	{        
		++real;        
		return *this;    
	}    

	complex operator++( int ) 
	{        
		auto temp = *this;        
		++real;        
		return temp;    
	}   

	// ... more functions that complement the above ...
private:    
	double real, imag;
};
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Eric Duhon (@duhonedd)</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10167</link>
		<dc:creator><![CDATA[Eric Duhon (@duhonedd)]]></dc:creator>
		<pubDate>Fri, 17 May 2013 07:25:46 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10167</guid>
		<description><![CDATA[I figured using user literals would helps with syntax and avoid implicit cast problems from float(i.e. was the float a real or imaginary). Ended up with a lot of code though.

&lt;code&gt;
#include 

using namespace std;

//template
class Imaginary
{
public:
    constexpr Imaginary() = default;
    constexpr Imaginary(const Imaginary&amp;) = default;
    Imaginary&amp; operator=(const Imaginary&amp;) = default;
    explicit constexpr Imaginary(double _value) : m_Value(_value) { }
    
    Imaginary&amp; operator-()
    {
        m_Value = -m_Value;
        return *this;
    }
    
    Imaginary&amp; operator+=(const Imaginary&amp; _other)
    {
        m_Value += _other.m_Value;
        return *this;
    }
    
    ostream&amp; PrintToStream(ostream&amp; _stream) const
    {
        _stream &lt;&lt; m_Value &lt;&lt; &#039;i&#039;;
        return _stream;
    }
    
private:
    //Don&#039;t want getters/setters for this, don&#039;t want a way to treat this as a standard float.
    double m_Value = 0;
};

Imaginary operator+(const Imaginary&amp; _arg1, const Imaginary&amp; _arg2)
{
    Imaginary result = _arg1;
    result += _arg2;
    return result;
}

ostream&amp; operator&lt;&lt;(ostream&amp; _stream, const Imaginary&amp; _value)
{
    return _value.PrintToStream(_stream);
}

//could use _fi or something for Imaginary and then templatize the Complex and Imaginary class
constexpr Imaginary operator&quot;&quot; _i (long double _value)
{
    return Imaginary(static_cast(_value));
}

constexpr Imaginary operator&quot;&quot; _i (unsigned long long _value)
{
    return Imaginary(static_cast(_value));
}

class Complex
{
public:
    constexpr Complex() = default;
    constexpr Complex(const Complex&amp;) = default;
    Complex&amp; operator=(const Complex&amp;) = default;
    
    constexpr Complex(double _real, const Imaginary&amp; _imag) : m_Real(_real), m_Imag(_imag) {}
    constexpr Complex(double _real) : Complex(_real, 0_i) {}
    constexpr Complex(const Imaginary&amp; _imag) : Complex(0, _imag) {}

    Complex&amp; operator+=(const Complex&amp; _other)
    {
        m_Real += _other.m_Real;
        m_Imag += _other.m_Imag;
        return *this;
    }

    Complex&amp; operator++() 
    {
        *this += 1;
        return *this;
    }
    
    Complex operator++(int)
    {
        Complex result = *this;
        ++(*this);        
        return result;
    }
    
    ostream&amp; PrintToStream(ostream&amp; _stream) const
    {
        _stream &lt;&lt; m_Real &lt;&lt; &quot;+&quot; &lt;&lt; m_Imag;
        return _stream;
    }

private:
    double m_Real = 0;
    Imaginary m_Imag = 0_i;
};

Complex operator+(const Complex&amp; _arg1, const Complex&amp; _arg2)
{
    Complex result = _arg1;
    result += _arg2;
    return result;
}

Complex operator+(double _real, const Imaginary&amp; _imag)
{
    return Complex(_real, _imag);
}

Complex operator+(const Imaginary&amp; _imag, double _real)
{
    return Complex(_real, _imag);
}

ostream&amp; operator&lt;&lt;(ostream&amp; _stream, const Complex&amp; _value)
{
    return _value.PrintToStream(_stream);
}

int main(int argc, const char * argv[])
{
    auto im = -4_i;    
    cout &lt;&lt; im &lt;&lt; endl;
    
    auto im2 = 2_i + im;
    cout &lt;&lt; im2 &lt;&lt; endl;
    
    Complex cx1 = 1 + 3_i;
    cout &lt;&lt; cx1 &lt;&lt; endl;
    
    Complex cx2 = 4_i + -3;
    cx2 += cx1;
    cout &lt;&lt; cx2 &lt;&lt; endl;
    
    cout &lt;&lt; ++cx2 &lt;&lt; endl;
    cout &lt;&lt; cx2++ &lt;&lt; endl;
    cout &lt;&lt; cx2 &lt;&lt; endl;
    
    cx2 = cx2 + cx1;
    cout &lt;&lt; cx2 &lt;&lt; endl;
    
    cx2 += -14_i;
    cout &lt;&lt; cx2 &lt;&lt; endl;
    
    return 0;
}
&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>I figured using user literals would helps with syntax and avoid implicit cast problems from float(i.e. was the float a real or imaginary). Ended up with a lot of code though.</p>
<p><code><br />
#include </p>
<p>using namespace std;</p>
<p>//template<br />
class Imaginary<br />
{<br />
public:<br />
    constexpr Imaginary() = default;<br />
    constexpr Imaginary(const Imaginary&amp;) = default;<br />
    Imaginary&amp; operator=(const Imaginary&amp;) = default;<br />
    explicit constexpr Imaginary(double _value) : m_Value(_value) { }</p>
<p>    Imaginary&amp; operator-()<br />
    {<br />
        m_Value = -m_Value;<br />
        return *this;<br />
    }</p>
<p>    Imaginary&amp; operator+=(const Imaginary&amp; _other)<br />
    {<br />
        m_Value += _other.m_Value;<br />
        return *this;<br />
    }</p>
<p>    ostream&amp; PrintToStream(ostream&amp; _stream) const<br />
    {<br />
        _stream &lt;&lt; m_Value &lt;&lt; &#039;i&#039;;<br />
        return _stream;<br />
    }</p>
<p>private:<br />
    //Don&#039;t want getters/setters for this, don&#039;t want a way to treat this as a standard float.<br />
    double m_Value = 0;<br />
};</p>
<p>Imaginary operator+(const Imaginary&amp; _arg1, const Imaginary&amp; _arg2)<br />
{<br />
    Imaginary result = _arg1;<br />
    result += _arg2;<br />
    return result;<br />
}</p>
<p>ostream&amp; operator&lt;&lt;(ostream&amp; _stream, const Imaginary&amp; _value)<br />
{<br />
    return _value.PrintToStream(_stream);<br />
}</p>
<p>//could use _fi or something for Imaginary and then templatize the Complex and Imaginary class<br />
constexpr Imaginary operator"" _i (long double _value)<br />
{<br />
    return Imaginary(static_cast(_value));<br />
}</p>
<p>constexpr Imaginary operator"" _i (unsigned long long _value)<br />
{<br />
    return Imaginary(static_cast(_value));<br />
}</p>
<p>class Complex<br />
{<br />
public:<br />
    constexpr Complex() = default;<br />
    constexpr Complex(const Complex&amp;) = default;<br />
    Complex&amp; operator=(const Complex&amp;) = default;</p>
<p>    constexpr Complex(double _real, const Imaginary&amp; _imag) : m_Real(_real), m_Imag(_imag) {}<br />
    constexpr Complex(double _real) : Complex(_real, 0_i) {}<br />
    constexpr Complex(const Imaginary&amp; _imag) : Complex(0, _imag) {}</p>
<p>    Complex&amp; operator+=(const Complex&amp; _other)<br />
    {<br />
        m_Real += _other.m_Real;<br />
        m_Imag += _other.m_Imag;<br />
        return *this;<br />
    }</p>
<p>    Complex&amp; operator++()<br />
    {<br />
        *this += 1;<br />
        return *this;<br />
    }</p>
<p>    Complex operator++(int)<br />
    {<br />
        Complex result = *this;<br />
        ++(*this);<br />
        return result;<br />
    }</p>
<p>    ostream&amp; PrintToStream(ostream&amp; _stream) const<br />
    {<br />
        _stream &lt;&lt; m_Real &lt;&lt; &quot;+&quot; &lt;&lt; m_Imag;<br />
        return _stream;<br />
    }</p>
<p>private:<br />
    double m_Real = 0;<br />
    Imaginary m_Imag = 0_i;<br />
};</p>
<p>Complex operator+(const Complex&amp; _arg1, const Complex&amp; _arg2)<br />
{<br />
    Complex result = _arg1;<br />
    result += _arg2;<br />
    return result;<br />
}</p>
<p>Complex operator+(double _real, const Imaginary&amp; _imag)<br />
{<br />
    return Complex(_real, _imag);<br />
}</p>
<p>Complex operator+(const Imaginary&amp; _imag, double _real)<br />
{<br />
    return Complex(_real, _imag);<br />
}</p>
<p>ostream&amp; operator&lt;&lt;(ostream&amp; _stream, const Complex&amp; _value)<br />
{<br />
    return _value.PrintToStream(_stream);<br />
}</p>
<p>int main(int argc, const char * argv[])<br />
{<br />
    auto im = -4_i;<br />
    cout &lt;&lt; im &lt;&lt; endl;</p>
<p>    auto im2 = 2_i + im;<br />
    cout &lt;&lt; im2 &lt;&lt; endl;</p>
<p>    Complex cx1 = 1 + 3_i;<br />
    cout &lt;&lt; cx1 &lt;&lt; endl;</p>
<p>    Complex cx2 = 4_i + -3;<br />
    cx2 += cx1;<br />
    cout &lt;&lt; cx2 &lt;&lt; endl;</p>
<p>    cout &lt;&lt; ++cx2 &lt;&lt; endl;<br />
    cout &lt;&lt; cx2++ &lt;&lt; endl;<br />
    cout &lt;&lt; cx2 &lt;&lt; endl;</p>
<p>    cx2 = cx2 + cx1;<br />
    cout &lt;&lt; cx2 &lt;&lt; endl;</p>
<p>    cx2 += -14_i;<br />
    cout &lt;&lt; cx2 &lt;&lt; endl;</p>
<p>    return 0;<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Lars Viklund</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10166</link>
		<dc:creator><![CDATA[Lars Viklund]]></dc:creator>
		<pubDate>Fri, 17 May 2013 06:57:02 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10166</guid>
		<description><![CDATA[Aren&#039;t generic lambdas C++1y?]]></description>
		<content:encoded><![CDATA[<p>Aren&#8217;t generic lambdas C++1y?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Alf P. Steinbach</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10165</link>
		<dc:creator><![CDATA[Alf P. Steinbach]]></dc:creator>
		<pubDate>Fri, 17 May 2013 05:07:50 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10165</guid>
		<description><![CDATA[The notion of &quot;easy to use correctly and hard to use incorrectly&quot; is generally a good idea, but not always. As an example, a simple 2D mutable &lt;code&gt;Point&lt;/code&gt; class is easiest to use correctly when it just has two public members and a constructor or two. Improbable as it may seem, for &lt;code&gt;double&gt;/code&gt; members Visual C++ has a hard time optimizing optimizing code that accesses the members indirectly, or at least, it still was a bit deficient re such optimizations the year before last, so this has also to do with fundamental low-level efficiency.

The &quot;easy to use correctly&quot; has to do with the class as a provider of practically useful operations and with the class as a single well understood abstraction, which is more at the language independent design level, while the &quot;hard to use incorrectly&quot; has to do with the class as an enforcer of design constraints, which is more at the language specific and even compiler specific level, Accommodating Visual C++&#039;s optimization problems is an example of possibly valid compiler-specific concerns. I think such choices need to be DOCUMENTED in order to be understood by maintenance programmers or even just by client code programmers.

Having noted that efficiency is a valid consideration, one should also note that it&#039;s easy to walk into the premature optimization trap. And the class presented in this question looks like an example of that. Apparently it&#039;s meant to provide a convenient increment operator, a &lt;code&gt;operator++&lt;/code&gt;, but that would be better provided for a class derived from &lt;code&gt;std::complex&amp;lt&gt;T&lt;/code&gt; (derived class in order to minimize the problem of multiple definitions of such an operator).

The above addresses some main concerns of the &quot;JG&quot; question, question 1. Others have already discussed the trivia of the &quot;Guru&quot; question, question 2, what the compiler can tell you if you give it the code. I think the &quot;Guru&quot; designation for that is a bit misplaced: nowadays it&#039;s not hard to feed some code to a compiler.

So far the existing answers have failed to mention what a compiler can&#039;t tell you, such as the pros and cons of choosing `void` as result type for `operator++`. Much in the same vein as choosing an as yet somewhat unconventional signed size type. This has to do with the more challenging &quot;hard to use incorrectly&quot; aspect &#8211; challenging in part because some established conventions in C++ are at odds with that goal.]]></description>
		<content:encoded><![CDATA[<p>The notion of &#8220;easy to use correctly and hard to use incorrectly&#8221; is generally a good idea, but not always. As an example, a simple 2D mutable <code>Point</code> class is easiest to use correctly when it just has two public members and a constructor or two. Improbable as it may seem, for <code>double&gt;/code&gt; members Visual C++ has a hard time optimizing optimizing code that accesses the members indirectly, or at least, it still was a bit deficient re such optimizations the year before last, so this has also to do with fundamental low-level efficiency.</p>
<p>The "easy to use correctly" has to do with the class as a provider of practically useful operations and with the class as a single well understood abstraction, which is more at the language independent design level, while the "hard to use incorrectly" has to do with the class as an enforcer of design constraints, which is more at the language specific and even compiler specific level, Accommodating Visual C++'s optimization problems is an example of possibly valid compiler-specific concerns. I think such choices need to be DOCUMENTED in order to be understood by maintenance programmers or even just by client code programmers.</p>
<p>Having noted that efficiency is a valid consideration, one should also note that it's easy to walk into the premature optimization trap. And the class presented in this question looks like an example of that. Apparently it's meant to provide a convenient increment operator, a </code><code>operator++</code>, but that would be better provided for a class derived from <code>std::complex&amp;lt&gt;T</code> (derived class in order to minimize the problem of multiple definitions of such an operator).</p>
<p>The above addresses some main concerns of the "JG" question, question 1. Others have already discussed the trivia of the "Guru" question, question 2, what the compiler can tell you if you give it the code. I think the "Guru" designation for that is a bit misplaced: nowadays it's not hard to feed some code to a compiler.</p>
<p>So far the existing answers have failed to mention what a compiler can't tell you, such as the pros and cons of choosing `void` as result type for `operator++`. Much in the same vein as choosing an as yet somewhat unconventional signed size type. This has to do with the more challenging "hard to use incorrectly" aspect &ndash; challenging in part because some established conventions in C++ are at odds with that goal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Balakrishnan B</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10164</link>
		<dc:creator><![CDATA[Balakrishnan B]]></dc:creator>
		<pubDate>Fri, 17 May 2013 04:57:58 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10164</guid>
		<description><![CDATA[I dont understand what does an extra branch mean in the find_if version. Does everyone refer to a function call? I guess algorithms like find_if would be most likely be inlined as it takes template arguments.]]></description>
		<content:encoded><![CDATA[<p>I dont understand what does an extra branch mean in the find_if version. Does everyone refer to a function call? I guess algorithms like find_if would be most likely be inlined as it takes template arguments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by infogulch</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10163</link>
		<dc:creator><![CDATA[infogulch]]></dc:creator>
		<pubDate>Fri, 17 May 2013 02:47:06 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10163</guid>
		<description><![CDATA[GregM &quot;It is when all but one of the parameters is defaulted.&quot;

Ah I didn&#039;t catch that, thanks.]]></description>
		<content:encoded><![CDATA[<p>GregM &#8220;It is when all but one of the parameters is defaulted.&#8221;</p>
<p>Ah I didn&#8217;t catch that, thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Crazy Eddie</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10162</link>
		<dc:creator><![CDATA[Crazy Eddie]]></dc:creator>
		<pubDate>Fri, 17 May 2013 02:37:05 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10162</guid>
		<description><![CDATA[Well, others have mentioned that the postfix ++ should be based on prefix ++ so I wont reiterate that.  The problem with &lt;&lt; being part of the class likewise has been covered, but making it a friend is a mistake.

What this class needs is &#039;getters&#039; for real and imag.  Then + and &lt;&lt; can be implemented as non-friend functions.  Although setters for simple data in a class is sometimes a mistake, it&#039;s warranted here.  The sanity that the operators provide is simply to keep two values locked together in a sensible way.  There are a great many reasons why we&#039;d want getters for real and imag and the class is rather useless without them.

Adding the getters (not necessarily setters) also obeys the principle that you should be able to tell if two objects are equal based on their public interface alone.  In other words, we want to be able to write == as a non-friend function as well.  I forget where I got this principle but it&#039;s a good one.

That said, operator + should defer to +=, which should be added.

[code]
complex operator + (complex left, complex const&amp; right)
{
  return left += right;
}
[/code]

The compiler will get rid of the unnecessary temporaries either by using move construction or RVO.]]></description>
		<content:encoded><![CDATA[<p>Well, others have mentioned that the postfix ++ should be based on prefix ++ so I wont reiterate that.  The problem with &lt;&lt; being part of the class likewise has been covered, but making it a friend is a mistake.</p>
<p>What this class needs is &#039;getters&#039; for real and imag.  Then + and &lt;&lt; can be implemented as non-friend functions.  Although setters for simple data in a class is sometimes a mistake, it&#039;s warranted here.  The sanity that the operators provide is simply to keep two values locked together in a sensible way.  There are a great many reasons why we&#039;d want getters for real and imag and the class is rather useless without them.</p>
<p>Adding the getters (not necessarily setters) also obeys the principle that you should be able to tell if two objects are equal based on their public interface alone.  In other words, we want to be able to write == as a non-friend function as well.  I forget where I got this principle but it&#039;s a good one.</p>
<p>That said, operator + should defer to +=, which should be added.</p>
<pre class="brush: plain; title: ; notranslate">
complex operator + (complex left, complex const&amp; right)
{
  return left += right;
}
</pre>
<p>The compiler will get rid of the unnecessary temporaries either by using move construction or RVO.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #2 Solution: Temporary Objects by Rick Yorgason</title>
		<link>http://herbsutter.com/2013/05/13/gotw-2-solution-temporary-objects/#comment-10161</link>
		<dc:creator><![CDATA[Rick Yorgason]]></dc:creator>
		<pubDate>Fri, 17 May 2013 02:32:12 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1874#comment-10161</guid>
		<description><![CDATA[@Herb But in practice, I&#039;ve never seen it cause much of a problem. Any time a function is returning a &amp;, whether it&#039;s a getter or a function like yours, the caller always knows one thing: the return value is valid if used immediately.

So if you&#039;re returning a const&amp;, I know that both of these are safe:
A) string s = find_addr(emps, &quot;Rick&quot;);
B) if(find_addr( emps, &quot;Rick&quot; ).find( &quot;Toronto&quot; ) != string::npos) {}

But if you&#039;re returning a value, one of those is less efficient.

I agree that hanging references/iterators is a very common problem, but every time I&#039;ve encountered it, the caller made a conscious decision to store a reference rather than a value, including in the example you provided.]]></description>
		<content:encoded><![CDATA[<p>@Herb But in practice, I&#8217;ve never seen it cause much of a problem. Any time a function is returning a &amp;, whether it&#8217;s a getter or a function like yours, the caller always knows one thing: the return value is valid if used immediately.</p>
<p>So if you&#8217;re returning a const&amp;, I know that both of these are safe:<br />
A) string s = find_addr(emps, &#8220;Rick&#8221;);<br />
B) if(find_addr( emps, &#8220;Rick&#8221; ).find( &#8220;Toronto&#8221; ) != string::npos) {}</p>
<p>But if you&#8217;re returning a value, one of those is less efficient.</p>
<p>I agree that hanging references/iterators is a very common problem, but every time I&#8217;ve encountered it, the caller made a conscious decision to store a reference rather than a value, including in the example you provided.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Mikhail Belyaev</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10160</link>
		<dc:creator><![CDATA[Mikhail Belyaev]]></dc:creator>
		<pubDate>Fri, 17 May 2013 02:24:13 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10160</guid>
		<description><![CDATA[Follow-up:

My bad. It is generally better to write operator+ as a friend for classes with implicit constructors:

[code]
//... inside the class
friend complex operator+(const complex&amp; lhv, const complex&amp; rhv) {
    return { lhv.real+rhv.real, lhv.imag+rhv.imag };
}
//...
[/code]

This way both cmplx + 2.2 and 2.2 + cmplx will work.]]></description>
		<content:encoded><![CDATA[<p>Follow-up:</p>
<p>My bad. It is generally better to write operator+ as a friend for classes with implicit constructors:</p>
<pre class="brush: plain; title: ; notranslate">
//... inside the class
friend complex operator+(const complex&amp; lhv, const complex&amp; rhv) {
    return { lhv.real+rhv.real, lhv.imag+rhv.imag };
}
//...
</pre>
<p>This way both cmplx + 2.2 and 2.2 + cmplx will work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Mikhail Belyaev</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10159</link>
		<dc:creator><![CDATA[Mikhail Belyaev]]></dc:creator>
		<pubDate>Fri, 17 May 2013 02:19:14 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10159</guid>
		<description><![CDATA[[code]
// 1. why not use std::complex?
class complex {
public:
    // 2. where&#039;s the default/move/copy constructor?
    complex() = default;
    complex(const complex&amp;) = default;
    // the 0 instead of 0.0 is not really an issue, but why not be a kind Samaritan?
    complex( double r, double i = 0.0 ): real(r), imag(i) { }

    // 3. this was messed up
    complex operator+ (const complex&amp; other ) {
        return { real+other.real, imag+other.imag };
    }

    // 4. this was way totally messed up
    friend ostream&amp; operator &lt;&lt; (ostream&amp; os, const complex&amp; cm) {
        return os &lt;&lt; &quot;(&quot; &lt;&lt; cm.real &lt;&lt; &quot;,&quot; &lt;&lt; cm.imag &lt;&lt; &quot;)&quot;;
    }

    // 5. as was this
    const complex&amp; operator++() {
        ++real;
        return *this;
    }

    // 6. the postfix ++ was actually the only operator that worked as it should
    // but it&#039;s generally better to write it using prefix ++
    complex operator++( int ) {
        auto temp = *this;
        ++*this;
        return temp;
    }

    // ... more functions that complement the above ...

private:
    double real, imag;
};

[/code]]]></description>
		<content:encoded><![CDATA[<pre class="brush: plain; title: ; notranslate">
// 1. why not use std::complex?
class complex {
public:
    // 2. where's the default/move/copy constructor?
    complex() = default;
    complex(const complex&amp;) = default;
    // the 0 instead of 0.0 is not really an issue, but why not be a kind Samaritan?
    complex( double r, double i = 0.0 ): real(r), imag(i) { }

    // 3. this was messed up
    complex operator+ (const complex&amp; other ) {
        return { real+other.real, imag+other.imag };
    }

    // 4. this was way totally messed up
    friend ostream&amp; operator &lt;&lt; (ostream&amp; os, const complex&amp; cm) {
        return os &lt;&lt; &quot;(&quot; &lt;&lt; cm.real &lt;&lt; &quot;,&quot; &lt;&lt; cm.imag &lt;&lt; &quot;)&quot;;
    }

    // 5. as was this
    const complex&amp; operator++() {
        ++real;
        return *this;
    }

    // 6. the postfix ++ was actually the only operator that worked as it should
    // but it's generally better to write it using prefix ++
    complex operator++( int ) {
        auto temp = *this;
        ++*this;
        return temp;
    }

    // ... more functions that complement the above ...

private:
    double real, imag;
};

</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by GregM</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10157</link>
		<dc:creator><![CDATA[GregM]]></dc:creator>
		<pubDate>Fri, 17 May 2013 01:15:43 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10157</guid>
		<description><![CDATA[Joe &quot;I don’t think explicit is necessary because implicit conversion isn’t possible with multi-parameter constructors.&quot;

It is when all but one of the parameters is defaulted.]]></description>
		<content:encoded><![CDATA[<p>Joe &#8220;I don’t think explicit is necessary because implicit conversion isn’t possible with multi-parameter constructors.&#8221;</p>
<p>It is when all but one of the parameters is defaulted.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Tom</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10156</link>
		<dc:creator><![CDATA[Tom]]></dc:creator>
		<pubDate>Fri, 17 May 2013 01:00:06 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10156</guid>
		<description><![CDATA[[code]
template&lt;typename FloatType = double&gt;
class complex {
public:
    inline complex(const FloatType r = FloatType(),const FloatType i = FloatType() )
        : real{r}, imag{i}
    { }
 
    complex&amp; operator+= (const FloatType&amp; r) {
        real += r; 
        return *this;
    }
 
    friend
    inline complex operator+ (const complex&amp; a, const complex&amp; b) {
        return {a.real + b.real, a.imag + b.imag};
    }
 
    friend
    inline ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; cp ) const {
        return os &lt;&lt; &quot;(&quot; &lt;&lt; cp.real &lt;&lt; &quot;,&quot; &lt;&lt; cp.imag &lt;&lt; &quot;)&quot;;
    }
 
    inline complex&amp; operator++() {
        ++real;
        return *this;
    }
 
    inline complex operator++( FloatType ) {
        auto temp = *this;
        ++real;
        return temp;
    }
 
    // ... more functions that complement the above ...
 
private:
    FloatType real, imag;
};
[/code]]]></description>
		<content:encoded><![CDATA[<pre class="brush: plain; title: ; notranslate">
template&lt;typename FloatType = double&gt;
class complex {
public:
    inline complex(const FloatType r = FloatType(),const FloatType i = FloatType() )
        : real{r}, imag{i}
    { }
 
    complex&amp; operator+= (const FloatType&amp; r) {
        real += r; 
        return *this;
    }
 
    friend
    inline complex operator+ (const complex&amp; a, const complex&amp; b) {
        return {a.real + b.real, a.imag + b.imag};
    }
 
    friend
    inline ostream&amp; operator&lt;&lt;( ostream&amp; os, const complex&amp; cp ) const {
        return os &lt;&lt; &quot;(&quot; &lt;&lt; cp.real &lt;&lt; &quot;,&quot; &lt;&lt; cp.imag &lt;&lt; &quot;)&quot;;
    }
 
    inline complex&amp; operator++() {
        ++real;
        return *this;
    }
 
    inline complex operator++( FloatType ) {
        auto temp = *this;
        ++real;
        return temp;
    }
 
    // ... more functions that complement the above ...
 
private:
    FloatType real, imag;
};
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Barry</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10155</link>
		<dc:creator><![CDATA[Barry]]></dc:creator>
		<pubDate>Thu, 16 May 2013 22:51:20 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10155</guid>
		<description><![CDATA[Yes, in this specific example the fact that we&#039;re returning a string by-value dwarfs the extra branch after the find(), but I am more interested in the general case, where we do call some function lots of times, and its return is very light (say it returns a pointer to the found element or 0).]]></description>
		<content:encoded><![CDATA[<p>Yes, in this specific example the fact that we&#8217;re returning a string by-value dwarfs the extra branch after the find(), but I am more interested in the general case, where we do call some function lots of times, and its return is very light (say it returns a pointer to the found element or 0).</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by TheMatto</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10154</link>
		<dc:creator><![CDATA[TheMatto]]></dc:creator>
		<pubDate>Thu, 16 May 2013 22:35:45 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10154</guid>
		<description><![CDATA[Minor point. The range-for loop can be written [code]for ( auto &amp; e : emps ) {...}[/code] given that emps is const.]]></description>
		<content:encoded><![CDATA[<p>Minor point. The range-for loop can be written
<pre class="brush: plain; title: ; notranslate">for ( auto &amp; e : emps ) {...}</pre>
<p> given that emps is const.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #1 Solution: Variable Initialization – or Is It? by Seungbum Woo</title>
		<link>http://herbsutter.com/2013/05/09/gotw-1-solution/#comment-10153</link>
		<dc:creator><![CDATA[Seungbum Woo]]></dc:creator>
		<pubDate>Thu, 16 May 2013 22:33:08 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1831#comment-10153</guid>
		<description><![CDATA[[code]
#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

class F {
public:
    F(const vector&lt;int&gt;&amp; vv) :v{vv} { cout &lt;&lt; v[0] &lt;&lt; endl; }
    void test() { cout &lt;&lt; v[0] &lt;&lt; endl; }
private:
    const vector&lt;int&gt;&amp; v;
};

int main()
{
    vector&lt;int&gt; a {2,3};
    F f {a};
    f.test();
}
[/code]

The output of this is
2 
garbage

if I change the F constructor to
[code]
     F(const vector&lt;int&gt;&amp; vv) :v(vv) { cout &lt;&lt; v[0] &lt;&lt; endl; }
[/code]

The output is correct as expected :
2
2

Why does the first one with {} not work as expected?   Thanks!]]></description>
		<content:encoded><![CDATA[<pre class="brush: plain; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

class F {
public:
    F(const vector&lt;int&gt;&amp; vv) :v{vv} { cout &lt;&lt; v[0] &lt;&lt; endl; }
    void test() { cout &lt;&lt; v[0] &lt;&lt; endl; }
private:
    const vector&lt;int&gt;&amp; v;
};

int main()
{
    vector&lt;int&gt; a {2,3};
    F f {a};
    f.test();
}
</pre>
<p>The output of this is<br />
2<br />
garbage</p>
<p>if I change the F constructor to</p>
<pre class="brush: plain; title: ; notranslate">
     F(const vector&lt;int&gt;&amp; vv) :v(vv) { cout &lt;&lt; v[0] &lt;&lt; endl; }
</pre>
<p>The output is correct as expected :<br />
2<br />
2</p>
<p>Why does the first one with {} not work as expected?   Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by nosenseetal</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10152</link>
		<dc:creator><![CDATA[nosenseetal]]></dc:creator>
		<pubDate>Thu, 16 May 2013 22:27:14 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10152</guid>
		<description><![CDATA[@Jay imao both of your code blocks are uglier than Herbs find version because you have std function that is designed to (tadaaaam) find element in a range. Period. :)
Beside I would guesstimate all this talk about perf is unrealistic... I mean if you really call this method like a mad man you probably want to go to the dark side and return a reference, also kill std::list with fire . :)]]></description>
		<content:encoded><![CDATA[<p>@Jay imao both of your code blocks are uglier than Herbs find version because you have std function that is designed to (tadaaaam) find element in a range. Period. :)<br />
Beside I would guesstimate all this talk about perf is unrealistic&#8230; I mean if you really call this method like a mad man you probably want to go to the dark side and return a reference, also kill std::list with fire . :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by austinwiltshire</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10151</link>
		<dc:creator><![CDATA[austinwiltshire]]></dc:creator>
		<pubDate>Thu, 16 May 2013 22:14:23 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10151</guid>
		<description><![CDATA[@Jay
Are you confusing readability for familiarity? :)]]></description>
		<content:encoded><![CDATA[<p>@Jay<br />
Are you confusing readability for familiarity? :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #1 Solution: Variable Initialization – or Is It? by C++&#039;s most vexing parse again &#124; BlogoSfera</title>
		<link>http://herbsutter.com/2013/05/09/gotw-1-solution/#comment-10150</link>
		<dc:creator><![CDATA[C++&#039;s most vexing parse again &#124; BlogoSfera]]></dc:creator>
		<pubDate>Thu, 16 May 2013 22:02:46 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1831#comment-10150</guid>
		<description><![CDATA[[&#8230;] Taken directly from http://herbsutter.com/2013/05/09/gotw-1-solution/ [&#8230;]]]></description>
		<content:encoded><![CDATA[<p>[&#8230;] Taken directly from <a href="http://herbsutter.com/2013/05/09/gotw-1-solution/" rel="nofollow">http://herbsutter.com/2013/05/09/gotw-1-solution/</a> [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Jay Miller</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10149</link>
		<dc:creator><![CDATA[Jay Miller]]></dc:creator>
		<pubDate>Thu, 16 May 2013 21:42:38 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10149</guid>
		<description><![CDATA[Two issues.  One is that I think Barry&#039;s point is relevant even though, in this case, an extra branch is swamped out by dynamic memory allocation.  Similarly to using prefix increment, doing things in a consistent manner every time that yields better performance in a subset of cases is a Good Thing if it doesn&#039;t make readability worse (though sometimes being consistent makes for better readability even if it&#039;s slightly more complex).

That brings us to the second issue, which is that I can&#039;t in good conscience say that

[code]
string find_addr( const list&lt;employee&gt;&amp; emps, const string&amp; name ) {
    const auto i = find_if( begin(emps), end(emps),
                      [&amp;](const auto&amp; e) { return e.name() == name; } );
    return i != end(emps) ? i-&gt;addr : &quot;&quot;;
}
[/code]

is more readable than

[code]
string find_addr( const list&lt;employee&gt;&amp; emps, const string&amp; name ) {
    for (const auto&amp; e : emps) {
        if (e.name() == name) {
            return e.addr;
        }
    }
    return string(); 
}
[/code]

Granted it&#039;s fewer lines, but I can reason very intuitively about the bottom.  The difference is small, so if the find_if case were faster then I&#039;d use it.  But an extra branch /and/ not as readable?]]></description>
		<content:encoded><![CDATA[<p>Two issues.  One is that I think Barry&#8217;s point is relevant even though, in this case, an extra branch is swamped out by dynamic memory allocation.  Similarly to using prefix increment, doing things in a consistent manner every time that yields better performance in a subset of cases is a Good Thing if it doesn&#8217;t make readability worse (though sometimes being consistent makes for better readability even if it&#8217;s slightly more complex).</p>
<p>That brings us to the second issue, which is that I can&#8217;t in good conscience say that</p>
<pre class="brush: plain; title: ; notranslate">
string find_addr( const list&lt;employee&gt;&amp; emps, const string&amp; name ) {
    const auto i = find_if( begin(emps), end(emps),
                      [&amp;](const auto&amp; e) { return e.name() == name; } );
    return i != end(emps) ? i-&gt;addr : &quot;&quot;;
}
</pre>
<p>is more readable than</p>
<pre class="brush: plain; title: ; notranslate">
string find_addr( const list&lt;employee&gt;&amp; emps, const string&amp; name ) {
    for (const auto&amp; e : emps) {
        if (e.name() == name) {
            return e.addr;
        }
    }
    return string(); 
}
</pre>
<p>Granted it&#8217;s fewer lines, but I can reason very intuitively about the bottom.  The difference is small, so if the find_if case were faster then I&#8217;d use it.  But an extra branch /and/ not as readable?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by sehe (@sehetw)</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10148</link>
		<dc:creator><![CDATA[sehe (@sehetw)]]></dc:creator>
		<pubDate>Thu, 16 May 2013 21:36:10 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10148</guid>
		<description><![CDATA[0. implicit conversion constructor could be undesired
1. missing includes iosfwd
2. ostream not by reference
3. operator &lt;&lt; wrong param ordering
4. operator &lt;&lt; &quot;wrong&quot; return type (no chaining)
   fixing operator&lt;&lt;:
[code]
    // in class
    friend std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, complex const&amp; c); 

    // in cpp
    #include &lt;ostream&gt;

    std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, complex const&amp; c) {
        return os &lt;&lt; &#039;(&#039; &lt;&lt; c.real &lt;&lt; &#039;,&#039; &lt;&lt; c.imag &lt;&lt; &#039;)&#039;;
    }
[/code]

5. operator+() actually implements more of `operator+=` and could take a const&amp; to allow for rvalues (implicit conversions)

   fixing it as operator+ (note const): 

[code]
    complex operator+ ( complex const &amp;other ) const {
        return { real + other.real, imag + other.imag };
    }
[/code]

6. operator++() (prefix version) should return by reference

7. Consider using friend operator+ instead of member operator+ to allow for implicit conversion of the first operand as well:

[code]
    friend complex operator+ ( complex const&amp; a, complex const &amp;other )  {
        return { a.real + other.real, a.imag + other.imag };
    }
[/code]

   Note this becomes less useful if the constructor was made explicit]]></description>
		<content:encoded><![CDATA[<p>0. implicit conversion constructor could be undesired<br />
1. missing includes iosfwd<br />
2. ostream not by reference<br />
3. operator &lt;&lt; wrong param ordering<br />
4. operator &lt;&lt; &quot;wrong&quot; return type (no chaining)<br />
   fixing operator&lt;&lt;:</p>
<pre class="brush: plain; title: ; notranslate">
    // in class
    friend std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, complex const&amp; c); 

    // in cpp
    #include &lt;ostream&gt;

    std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, complex const&amp; c) {
        return os &lt;&lt; &#039;(&#039; &lt;&lt; c.real &lt;&lt; &#039;,&#039; &lt;&lt; c.imag &lt;&lt; &#039;)&#039;;
    }
</pre>
<p>5. operator+() actually implements more of `operator+=` and could take a const&amp; to allow for rvalues (implicit conversions)</p>
<p>   fixing it as operator+ (note const): </p>
<pre class="brush: plain; title: ; notranslate">
    complex operator+ ( complex const &amp;other ) const {
        return { real + other.real, imag + other.imag };
    }
</pre>
<p>6. operator++() (prefix version) should return by reference</p>
<p>7. Consider using friend operator+ instead of member operator+ to allow for implicit conversion of the first operand as well:</p>
<pre class="brush: plain; title: ; notranslate">
    friend complex operator+ ( complex const&amp; a, complex const &amp;other )  {
        return { a.real + other.real, a.imag + other.imag };
    }
</pre>
<p>   Note this becomes less useful if the constructor was made explicit</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #4: Class Mechanics (7/10) by Tianyu Zhu</title>
		<link>http://herbsutter.com/2013/05/16/gotw-4-class-mechanics-710/#comment-10147</link>
		<dc:creator><![CDATA[Tianyu Zhu]]></dc:creator>
		<pubDate>Thu, 16 May 2013 21:30:24 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1920#comment-10147</guid>
		<description><![CDATA[Something other people haven&#039;t pointer out:
operator + is actually operator +=. That is, the following code:

Complex a(1, 0), b(0, 1);
a + b;

Actually changes the value of a.

I would probably implement operator + like this:

complex operator +(complex other) const {
    return complex(real+other.real, imag+other.imag);
}

Or perhaps in terms of an implemented += operator.

I wouldn&#039;t necessarily take arguments by const&amp; since this is a relatively small class and it may not be necessary.]]></description>
		<content:encoded><![CDATA[<p>Something other people haven&#8217;t pointer out:<br />
operator + is actually operator +=. That is, the following code:</p>
<p>Complex a(1, 0), b(0, 1);<br />
a + b;</p>
<p>Actually changes the value of a.</p>
<p>I would probably implement operator + like this:</p>
<p>complex operator +(complex other) const {<br />
    return complex(real+other.real, imag+other.imag);<br />
}</p>
<p>Or perhaps in terms of an implemented += operator.</p>
<p>I wouldn&#8217;t necessarily take arguments by const&amp; since this is a relatively small class and it may not be necessary.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited) by Herb Sutter</title>
		<link>http://herbsutter.com/2013/05/16/gotw-3-solution-using-the-standard-library-or-temporaries-revisited/#comment-10146</link>
		<dc:creator><![CDATA[Herb Sutter]]></dc:creator>
		<pubDate>Thu, 16 May 2013 21:26:07 +0000</pubDate>
		<guid isPermaLink="false">http://herbsutter.com/?p=1912#comment-10146</guid>
		<description><![CDATA[@Olaf: Can you elaborate?]]></description>
		<content:encoded><![CDATA[<p>@Olaf: Can you elaborate?</p>
]]></content:encoded>
	</item>
</channel>
</rss>