From -2060293992062326878
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: Roger Glover <roger.glover@mindspring.com>
Subject: Re: Article on exceptions in January C++ Report
Date: 1998/03/02
Message-ID: <6depnb$l9t@netlab.cs.rpi.edu>#1/1
X-Deja-AN: 330085661
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>
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Original-Date: Sun, 01 Mar 1998 23:34:36 -0600
Reply-To: roger.glover@mindspring.com
Originator: clamage@taumet
X-UID: 0000000001
X-Status: $$$T
X-Approved-For-Group: herbs@cntc.com comp.lang.c++.moderated
Organization: It is a mistake to think that you are working for someone else
X-Auth: none comp.lang.c++.moderated
Newsgroups: comp.lang.c++.moderated,comp.std.c++,comp.lang.c++


Paul D. DeRocco wrote:

> greg wrote:
> >
> >  * swap() has no effects if it throws an exception,
>
> I think we pretty much proved, in this newsgroup a few months ago,
that it is
> impossible to make swap safe for objects whose constructors might
throw. In
> order to swap a with b, you have to copy a to temporary c, then b to
a, then c
> to b;

This would only be so if memberwise copy is required to implement the
swap.
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

(That didn't do it for you?  Then imagine the objects a and b reinter-
preted as an arrays of (sizeof(a)) unsigned chars, and a loop with the
three statements above and some appropriate indexing.)

Is there a true "swapping" situation in which a bitwise swap of values
does not suffice?  I cannot imagine such a situation!

> if the last one throws, you cannot expect to be able to move a back
into
> b and put c back into a, so your stuck.
>
> I came up with an elaborate scheme that involved having two objects
apparently
> at the same address, by memcpy-ing one out, and then constructing a
new one in
> its place with placement new, but even that fails if you're
registering all
> created objects in a database indexed by their address.

This would also not be a problem with an xor-based bitwise swap, since
it does not require allocation of new storage.


-- Roger Glover


      [ 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              ]




