From -1517805579288684247
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,515fa24b8aaed57
X-Google-Attributes: gidf78e5,public
From: kcline@sun132.spd.dsccc.com (Kevin Cline)
Subject: Re: Example in "The C++ Workbook" by Wiener and Pinson
Date: 1995/08/04
Message-ID: <3vtke4$ik7@sun132.spd.dsccc.com>#1/1
X-Deja-AN: 107568704
references: <3ujj8d$o4m@scapa.cs.ualberta.ca> <3vj9ak$49m@news.rrz.uni-koeln.de> <FENSTER.95Jul31191916@shadow.cs.columbia.edu> <3vtat5INNovr@hubble.asymetrix.com>
organization: DSC Communications Corporation Switch Products Division
newsgroups: comp.std.c++

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




