Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2006-02-17 11:26:00


Doug Gregor <dgregor_at_[hidden]> writes:

> On Feb 17, 2006, at 8:48 AM, Thorsten Ottosen wrote:
>> I have just added serialization to Boost.Ptr container.
>> I briefly discussed an issue with Robert, namely
>> that the current implementation calls clear() before
>> reading the new values.
>>
>> The alternative is to provide roll-back guarantee by default
>> by creating a local container and swap() in the end.
>> By default this would temporarily use more memory.
>>
>> IMO we should go for the strong guarantee.
>> Any thoughts?
>
> I think the basic guarantee is enough here. Truly paranoid users can
> copy+swap themselves, but for most purposes you are better off not
> having two copies of the container in memory. Most people need better
> performance more than they need the strong guarantee.

That's one right way to look at it. To look at it another way, if you
provide the strong guarantee by copy-and-swap up front, there's no way
to recover the efficiency for the (majority of) people who only need
the basic guarantee.

The *only* reason to use copy-and-swap around the implementation of an
operation is to make it less error-prone. This:

     X& operator=(X rhs) { rhs.swap(*this); return *this; }

is almost idiot-proof if you have a working copy ctor. Although for
many classes it comes at a substantial efficiency cost when compared
with a more laboriously worked-out assignment operator that gives the
basic guarantee, I can't fault anyone for going for correctness and
low maintenance cost first, and efficiency second.

Incidentally, I don't know of any operations other than assignment
whose maintainability are helped by copy/swap in this way.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk