Boost logo

Boost :

Subject: Re: [boost] Improving the assignment operators of various Boosttypes
From: David Abrahams (dave_at_[hidden])
Date: 2008-09-11 10:22:35


on Thu Sep 11 2008, Joel Falcou <joel.falcou-AT-u-psud.fr> wrote:

> David Abrahams a écrit :
>> self& operator=(self x) { swap(*this,x); return *this; }
>>
>> with the rationale given in
>> http://groups.google.com/group/comp.lang.c++.moderated/msg/5e6014446d559b63> (I suspect Andrei's CUJ article is equally good or better but much
>> longer)
>>
>> ...but I would think given the thread mentioned above that answer would
>> be self-evident, so it makes me wonder if you're trying to ask something
>> else
> Sorry if i was unclear. My question was is the old operator= form still needed
> soemtime , thus making the 2 forms of the operator to be taught.

The only other form that makes sense for operator= doesn't use swap at
all, and does things "the hard way:" destroy LHS resources and then copy
the RHS resources into it. You might choose that approach if you were
willing to accept the basic guarantee in order to avoid the spike in
resource usage caused by having the LHS data and two copies of the RHS
data all at once. The standard containers generally do it that way.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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