
Jody Hagins <jody-boost-011304@atdesk.com> 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.
Okay, okay. 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