<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Don&#8217;t Let the &#8220;Smart&#8221; Way to Write the Swap Function in C++ Fool You</title>
	<atom:link href="http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/feed/" rel="self" type="application/rss+xml" />
	<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/</link>
	<description>"The time has come...to talk of many [technologies]." --Lewis Carroll('The Walrus and the Carpenter')</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:38:09 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: yibu</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/comment-page-1/#comment-138332</link>
		<dc:creator>yibu</dc:creator>
		<pubDate>Tue, 12 Oct 2010 04:36:03 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-138332</guid>
		<description>I tested three versions of integer swapping methods (temp storage, xor, add/subtract) on my laptop (Intel X86, Linux, gcc). Here&#039;s the conclusion:

With moderate compiler optimization: all three are roughly the same.
Without optimization: the traditional (temp storage) method is significantly FASTER than the other two.

I started to test the performance because Wikepedia is saying that temp storage method is probably faster on current architecture due to instruction pipelining and the other two method must be executed sequentially thus no parallelism can be exploited. And my testing result just proved that.</description>
		<content:encoded><![CDATA[<p>I tested three versions of integer swapping methods (temp storage, xor, add/subtract) on my laptop (Intel X86, Linux, gcc). Here&#8217;s the conclusion:</p>
<p>With moderate compiler optimization: all three are roughly the same.<br />
Without optimization: the traditional (temp storage) method is significantly FASTER than the other two.</p>
<p>I started to test the performance because Wikepedia is saying that temp storage method is probably faster on current architecture due to instruction pipelining and the other two method must be executed sequentially thus no parallelism can be exploited. And my testing result just proved that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yibu</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/comment-page-1/#comment-138331</link>
		<dc:creator>yibu</dc:creator>
		<pubDate>Mon, 11 Oct 2010 16:58:13 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-138331</guid>
		<description>I don&#039;t know where the author got the idea of the second version. The &quot;add/subtract&quot; version and &quot;xor&quot; version were clearly mentioned only in the context of integer swapping. To make them even compile for user-defined types, you need to define operator &quot;+&quot;, &quot;-&quot; for that type, which may make it still work if properly designed.

And, have you given it enough thoughts when you say &quot;The code fails even for the integers when the the sum of two passed variables exceeds the maximum integer limit on that machine.&quot;?</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know where the author got the idea of the second version. The &#8220;add/subtract&#8221; version and &#8220;xor&#8221; version were clearly mentioned only in the context of integer swapping. To make them even compile for user-defined types, you need to define operator &#8220;+&#8221;, &#8220;-&#8221; for that type, which may make it still work if properly designed.</p>
<p>And, have you given it enough thoughts when you say &#8220;The code fails even for the integers when the the sum of two passed variables exceeds the maximum integer limit on that machine.&#8221;?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/comment-page-1/#comment-137222</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Fri, 08 Aug 2008 05:09:12 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-137222</guid>
		<description>[quote comment=&quot;137135&quot;]so why not do it *the right way* and just use bitwise xor?

a = a xor b
b = a xor b
a = a xor b

correct(the contract should specify that the references passed in may not be equal), works for all data types, and saves one precious variable.[/quote]</description>
		<content:encoded><![CDATA[<p>[quote comment="137135"]so why not do it *the right way* and just use bitwise xor?</p>
<p>a = a xor b<br />
b = a xor b<br />
a = a xor b</p>
<p>correct(the contract should specify that the references passed in may not be equal), works for all data types, and saves one precious variable.[/quote]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tabrez</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/comment-page-1/#comment-137147</link>
		<dc:creator>tabrez</dc:creator>
		<pubDate>Wed, 25 Jun 2008 11:58:01 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-137147</guid>
		<description>Most people who learn C++ already know either Java, Python or C#. They still need to know how to swap two variables in C++. Templates are no more a beastly topic as it was once considered to be; most people teach basics of templates right from the very first classes, enough to get comfortable with the syntax of STL containers and algorithms. Writing the swap function in C++ without using templates is an utterly useless activity in my opinion.</description>
		<content:encoded><![CDATA[<p>Most people who learn C++ already know either Java, Python or C#. They still need to know how to swap two variables in C++. Templates are no more a beastly topic as it was once considered to be; most people teach basics of templates right from the very first classes, enough to get comfortable with the syntax of STL containers and algorithms. Writing the swap function in C++ without using templates is an utterly useless activity in my opinion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/comment-page-1/#comment-137146</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Mon, 23 Jun 2008 22:28:17 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-137146</guid>
		<description>Hm...that&#039;s interesting.  Teaching people who are starting to learn programming to use C++ templates doesn&#039;t sound like the right tool for the right job.

I suppose if someone is at the level of using templates, the concept of exception safety should be introduced somewhere close.

For someone who&#039;s new, I&#039;d let them start with Ruby, Javascript, Logo, Object Pascal, or even Java.  C++ sounds like a poor choice for an introductory language.</description>
		<content:encoded><![CDATA[<p>Hm&#8230;that&#8217;s interesting.  Teaching people who are starting to learn programming to use C++ templates doesn&#8217;t sound like the right tool for the right job.</p>
<p>I suppose if someone is at the level of using templates, the concept of exception safety should be introduced somewhere close.</p>
<p>For someone who&#8217;s new, I&#8217;d let them start with Ruby, Javascript, Logo, Object Pascal, or even Java.  C++ sounds like a poor choice for an introductory language.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tabrez</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/comment-page-1/#comment-137144</link>
		<dc:creator>tabrez</dc:creator>
		<pubDate>Mon, 23 Jun 2008 10:04:13 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-137144</guid>
		<description>@Anonymous,
@someone is correct, swapping done using xor operator is not a smart way either. It will take up a few lines to explain the reasons, but as @someone said you can lookup why you shouldn&#039;t pass arrays polymorphically to understand why xor version of swapping won&#039;t work for polymorphic objects.

@Michael,
If you are creating an application in C++ and you want to swap two variables, you most definitely would use std::swap function. If you read carefully, I was talking about people who are starting to learn programming, and hence need to write their own swap, search, sort etc. functions. The exercise clears up points like the ones discussed in this post.
I didn&#039;t even bother to make the swap function inline; my example focussed only on the right way to do the swapping. But I will post the std::swap functions as implemented by major STL implementors to show how simple they actually are.</description>
		<content:encoded><![CDATA[<p>@Anonymous,<br />
@someone is correct, swapping done using xor operator is not a smart way either. It will take up a few lines to explain the reasons, but as @someone said you can lookup why you shouldn&#8217;t pass arrays polymorphically to understand why xor version of swapping won&#8217;t work for polymorphic objects.</p>
<p>@Michael,<br />
If you are creating an application in C++ and you want to swap two variables, you most definitely would use std::swap function. If you read carefully, I was talking about people who are starting to learn programming, and hence need to write their own swap, search, sort etc. functions. The exercise clears up points like the ones discussed in this post.<br />
I didn&#8217;t even bother to make the swap function inline; my example focussed only on the right way to do the swapping. But I will post the std::swap functions as implemented by major STL implementors to show how simple they actually are.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/comment-page-1/#comment-137142</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Mon, 23 Jun 2008 05:21:20 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-137142</guid>
		<description>All of your ways are wrong.

The only correct way of swapping is don&#039;t bother writing your own swap.  Use std::swap directly if you can, call it if you cannot.

std::swap(a, b).

Done.  Never reinvent your own wheel.

Your 1st way is wrong because it doesn&#039;t have a &quot;nothrow&quot; guarantee.  It&#039;s especially important when memory is tight.</description>
		<content:encoded><![CDATA[<p>All of your ways are wrong.</p>
<p>The only correct way of swapping is don&#8217;t bother writing your own swap.  Use std::swap directly if you can, call it if you cannot.</p>
<p>std::swap(a, b).</p>
<p>Done.  Never reinvent your own wheel.</p>
<p>Your 1st way is wrong because it doesn&#8217;t have a &#8220;nothrow&#8221; guarantee.  It&#8217;s especially important when memory is tight.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/comment-page-1/#comment-137141</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Mon, 23 Jun 2008 02:44:11 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-137141</guid>
		<description>You might also want to consider doing your homework by reading http://en.wikipedia.org/wiki/XOR_swap_algorithm</description>
		<content:encoded><![CDATA[<p>You might also want to consider doing your homework by reading <a href="http://en.wikipedia.org/wiki/XOR_swap_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/XOR_swap_algorithm</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>

