<?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"
	>
<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>
	<pubDate>Fri, 05 Dec 2008 03:06:48 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<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-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="137135"]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> on June 22, 2008 at 11:04 pm said:</p>
<blockquote cite="http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/#comment-137135"><p>
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.</p>
</blockquote>
]]></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-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-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's interesting.  Teaching people who are starting to learn programming to use C++ templates doesn'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's new, I'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-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't pass arrays polymorphically to understand why xor version of swapping won'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'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-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'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't have a "nothrow" guarantee.  It'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-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>
	<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-137140</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Mon, 23 Jun 2008 02:39:53 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-137140</guid>
		<description>Instead of calling people asinine you might want to consider giving an argument for what you're claiming...just a suggestion though.</description>
		<content:encoded><![CDATA[<p>Instead of calling people asinine you might want to consider giving an argument for what you&#8217;re claiming&#8230;just a suggestion though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: someone</title>
		<link>http://beans.seartipy.com/2008/06/21/dont-let-the-smart-way-to-write-the-swap-function-in-c-fool-you/#comment-137138</link>
		<dc:creator>someone</dc:creator>
		<pubDate>Mon, 23 Jun 2008 00:55:27 +0000</pubDate>
		<guid isPermaLink="false">http://beans.seartipy.com/?p=275#comment-137138</guid>
		<description>&#62;&#62;so why not do it *the right way* and just use bitwise xor?

I certainly hope you aren't doing this with polymorphic objects. Please do not do bitwise operations on polymorphic objects. If you don't know why please do not ever write code again. Also whoever wrote that second version, stop coding as well. You are asinine and dangerous.</description>
		<content:encoded><![CDATA[<p>&gt;&gt;so why not do it *the right way* and just use bitwise xor?</p>
<p>I certainly hope you aren&#8217;t doing this with polymorphic objects. Please do not do bitwise operations on polymorphic objects. If you don&#8217;t know why please do not ever write code again. Also whoever wrote that second version, stop coding as well. You are asinine and dangerous.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
