From: David Abrahams (dave_at_[hidden])
Date: 2005-02-15 16:47:34
Jody Hagins <jody-boost-011304_at_[hidden]> writes:
> Sure. The same could be said of shared_ptr and many other useful,
> well-established, tools and techniques that help the programmer write
> more correct code.
> IMO, this "operator=() canonical form" is by far the best initial
> solution for any non-trivial class. Make sure your copy-ctor and swap
> do the right thing (which you have to do anyway), and you are set. It
> is simple, elegant, and in most cases has fairly small overhead. It
> also helps emphasize correctness first.
> If the performance becomes a noticable problem, then attack it, but by
> then you will already have several tests for correctness and
> performance, which means that any problems in the changed implementation
> will be detected quickly. As always, a comment about the performance
> needs of the code is nice, as it prevents others from changing the
> performance characteristics later.
I guess I am overly defensive about this because when I "invented" the
technique (**) and brought it to peoples' attention as a way to get
the strong guarantee for any mutating operation, it seemed that the
next conclusion many people (outside the commitee) drew was
strong guarantee == good
basic guarantee == almost useless
And the logical follow-on conclusion among many was
It's worth doing almost anything to get the strong guarantee.
So I've been trying to shake that idea loose for a long time. But,
yeah, as a rule, correctness is more important than performance, and
the copy-and-swap formula is a great way to get a correct assignment
operator, and to avoid code duplication to boot.
(**) I'm _sure_ other people have thought of this technique, so I'm
not really trying to claim credit. More like the opposite. The
fact remains that it seemed like news to the committee when I
brought it up in 1997 as I was trying to get exception-safety
into the standard.
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk