Boost logo

Boost :

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


on Wed Sep 10 2008, "Scott McMurray" <me22.ca+boost-AT-gmail.com> wrote:

> On Wed, Sep 10, 2008 at 21:23, Joe Gottman <jgottman_at_[hidden]> wrote:
>> Joel Falcou wrote:
>>>
>>> I may come late and maybe like an unwanted guest in this discussion but I
>>> am curious to know how i can know that for a given class of mine, the swap
>>> value operator= is better than the by const ref operator= ?
>>>
>>
>> My rule of thumb is that if your assignment operator might throw an
>> exception and you want assignment to fulfill the strong exception safety
>> guarantee (i.e. either the assignment succeeds or the target remains
>> unchanged) then you should consider the swapping operator=.
>>
>
> I would even go slightly more expansive than that and say "Use the
> swapping operator= unless you've explicitly decided you want less
> exception safety (like for efficiency reasons in std::vector)".
>
> It means one fewer place that needs updating when members change, and
> I suspect -- though I have no supporting data -- that the optimizer
> should make the swapping version "good enough" in most cases.

Swapping and taking the parameter by value are orthogonal issues; the
latter has only to do with copy elision and not with exception safety.
Please see this thread:
http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/97ad5390e34d63a9/ef6b55d24b512070

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