Subject: Re: [boost] Improving the assignment operators of various Boosttypes
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2008-09-10 21:37:21
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.