🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1593

Re: Example in "The C++ Workbook" by Wiener and Pinson

#1593
From: Kevin Cline
Date: Fri, 04 Aug 1995 17:12
34 lines
1393 bytes
In article <3vtat5INNovr@hubble.asymetrix.com>,
Michael Taylor <michaelt@asymetrix.com> wrote:
 >fenster@shadow.cs.columbia.edu (Sam Fenster) wrote:
 >>a2675528@athena.rrz.Uni-Koeln.DE (Paul Sponagl) writes:
 >>> or do like that
 >>>
 >>> 	inline template < class T > void swap(T& a, T& b)
 >>> 	{
 >>> 		a ^=b; b^=a;a ^=b;
 >>> 	}
 >>
 >>This sets `a' and `b' to zero if they refer to the same object.
 >
 >Wrong!
 >
 >Let a = 1001110
 >and b = 1001110
 >
 >a ^= b; // -> a = 0
 >b ^= a; // -> b = 1001110
 >a ^= b; // -> a = 1001110
 >
 >
 >          Michael Taylor
 >	    michaelt@asymetrix.com

Wrong! You have shown that it works if a and b have the same value.
If a and b REFER TO THE SAME OBJECT, (e.g. swap(A[i],A[j]) where i == j)
then a ^= b has the same effect as a ^= a; it sets a to zero.

 

-- 
Kevin Cline
Technical Details
Message-ID: <3vtke4$ik7@sun132.spd.dsccc.com>
Path: arc1-ca.pugleaf.net!camelot.dsccc.com!not-for-mail
References: <3ujj8d$o4m@scapa.cs.ualberta.ca> <3vj9ak$49m@news.rrz.uni-koeln.de> <FENSTER.95Jul31191916@shadow.cs.columbia.edu> <3vtat5INNovr@hubble.asymetrix.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!camelot.dsccc.com!not-for-mail
From: kcline@sun132.spd.dsccc.com (Kevin Cline)
Newsgroups: comp.std.c++
Subject: Re: Example in "The C++ Workbook" by Wiener and Pinson
Date: 4 Aug 1995 12:12:36 -0500
Organization: DSC Communications Corporation Switch Products Division
Message-ID: <3vtke4$ik7@sun132.spd.dsccc.com>
References: <3ujj8d$o4m@scapa.cs.ualberta.ca> <3vj9ak$49m@news.rrz.uni-koeln.de> <FENSTER.95Jul31191916@shadow.cs.columbia.edu> <3vtat5INNovr@hubble.asymetrix.com>
NNTP-Posting-Host: sun132.spd.dsccc.com