🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#1526
From: Sam Fenster
Date: Mon, 31 Jul 1995 23:19
21 lines
1133 bytes
>>	template < class T >
>>	void swap(T & item1, T & item2)
>>	{
>>		T temp = item1;
>>		item1 = item2;
>>		item2 = temp;
>>	}

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.  You might do
this inadvertently if you were swapping array elements, for instance.

Also, `operator ^= (T &, T &)' is not defined for arbitrary types,
e.g. classes.
Technical Details
Message-ID: <FENSTER.95Jul31191916@shadow.cs.columbia.edu>
Path: arc1-ca.pugleaf.net!news.cs.columbia.edu!fenster
References: <3ujj8d$o4m@scapa.cs.ualberta.ca> <9520904.20199@mulga.cs.mu.OZ.AU> <3vj9ak$49m@news.rrz.uni-koeln.de>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.cs.columbia.edu!fenster
From: fenster@shadow.cs.columbia.edu (Sam Fenster)
Newsgroups: comp.std.c++
Subject: Re: Example in "The C++ Workbook" by Wiener and Pinson
Date: 31 Jul 1995 23:19:16 GMT
Organization: Columbia University Department of Computer Science
Message-ID: <FENSTER.95Jul31191916@shadow.cs.columbia.edu>
References: <3ujj8d$o4m@scapa.cs.ualberta.ca> <9520904.20199@mulga.cs.mu.OZ.AU>
 <3vj9ak$49m@news.rrz.uni-koeln.de>
NNTP-Posting-Host: shadow.cs.columbia.edu
In-reply-to: a2675528@athena.rrz.Uni-Koeln.DE's message of Mon, 31 Jul 1995 19:03:38 GMT