From -7392628885620792615
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fc772,5cc796235bc9d4d8
X-Google-Attributes: gidfc772,public
X-Google-Thread: 109fba,5cc796235bc9d4d8
X-Google-Attributes: gid109fba,public
X-Google-Thread: f78e5,5cc796235bc9d4d8
X-Google-Attributes: gidf78e5,public
From: "Martijn Lievaart" <nospam@somewhere.org>
Subject: Re: Article on exceptions in January C++ Report
Date: 1998/03/03
Message-ID: <6dhnvs$eb0@netlab.cs.rpi.edu>#1/1
X-Deja-AN: 330515528
References: <6cjhbk$ru6@netlab.cs.rpi.edu> <6cre6n$qbd@netlab.cs.rpi.edu> <6cu53k$918@netlab.cs.rpi.edu> <6d78m5$s4u@netlab.cs.rpi.edu> <6depnb$l9t@netlab.cs.rpi.edu>
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Original-Date: 3 Mar 1998 15:08:28 -0500
X-Approved-For-Group: harkness@skeptics.org comp.lang.c++.moderated
Organization: XS4ALL, networking for the masses
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANPxm7+EDnX0m9pzZAQFxtAF9Fwb2NSCkA8YAkyBSR9MsHazkvYL+RkrX xpIDs2K1j4kJH8ZEE8vdmb4nG4fPf/hv =JIrK
Newsgroups: comp.lang.c++.moderated,comp.std.c++,comp.lang.c++


Roger Glover wrote in message <6depnb$l9t@netlab.cs.rpi.edu>...
--8<--
>If a bitwise swap suffices, there is a way to swap without creating a
>temporary or staging in registers, using only repeated application of
>the
>exclusive or operator:
>
>    a ^= b;
>    b ^= a;  // b now contains the original contents of a
>    a ^= b;  // a now contains the original contents of b
>
--8<--
>
>Is there a true "swapping" situation in which a bitwise swap of values
>does not suffice?  I cannot imagine such a situation!
>

Yes there is.
First you have to realize that a and b *must* be references, not local
values, as the original problem inhibits making copies.
Now when both a and b refer to the same location, the algorith breaks.
While I admit this is not likely, it is possible, so it probably will
happen. This can lead to very obscure bugs.



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



