🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Example in "The C++ Workbook" by Wiene

#1595
From: Steve Clamage
Date: Fri, 04 Aug 1995 18:16
29 lines
1285 bytes
In article 3vtat5INNovr@hubble.asymetrix.com, michaelt@asymetrix.com (Michael Taylor) writes:
>fenster@shadow.cs.columbia.edu (Sam Fenster) wrote:
>>a2675528@athena.rrz.Uni-Koeln.DE (Paul Sponagl) writes:
>>> 	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

Not wrong! (Golly, but I get tired of this.)

'a' and 'b' are not the same object. They are different objects which
happen to have the same value. The invocation
	swap(a, a)
sets 'a' to zero. You wouldn't normally write that, but when parameters
are passed around by reference, it can happen without your knowing about it.

---
Steve Clamage, stephen.clamage@eng.sun.com
Technical Details
Message-ID: <3vto5r$m5t@engnews2.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenet
References: <3vtat5INNovr@hubble.asymetrix.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenet
From: clamage@Eng.Sun.COM (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Example in "The C++ Workbook" by Wiene
Date: 4 Aug 1995 18:16:27 GMT
Organization: Sun Microsystems Inc.
Message-ID: <3vto5r$m5t@engnews2.Eng.Sun.COM>
References: <3vtat5INNovr@hubble.asymetrix.com>
Reply-To: clamage@Eng.Sun.COM
NNTP-Posting-Host: taumet.eng.sun.com