Boost logo

Boost :

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


on Wed Sep 10 2008, "Peter Dimov" <pdimov-AT-pdimov.com> wrote:

> Niels Dekker:
>
>>>> T& operator=(T arg)
>>>> {
>>>> arg.swap(*this);
>>>> return *this;
>>>> }
>
> ...
>
>> Is the choice between swap(arg) and arg.swap(*this)) just a matter of
>> taste?
>
> Yes, I think so. I was just saying that
>
> T& operator=( T arg )
> {
> swap( arg );
> return *this;
> }
>
> is the form I've seen used (by Dave Abrahams, I think*). It certainly looks more
> "idiomatic" (in the sense of being recognizable as an idiom as opposed to
> ordinary code) because of the unusual swap call.
>
>
> *) Turns out that I was right in thinking that. Dave's associative_vector
> submission:
>
> http://lists.boost.org/Archives/boost/2000/05/3200.php
>
> contains:
>
> Self& operator=(Self rhs) { swap(rhs); return *this; } // strong guarantee

That's interesting. I'm pretty sure that when I wrote that, I wasn't
aware of the copy elision advantages and was just trying to write the
most compact strong-guarantee assignment that I could.

> I was able to find a post by Valentin Bonnard also suggesting the same:
>
> http://tech.groups.yahoo.com/group/boost/message/5001

Looks like he wasn't aware of it either

> Incidentally:
>
> http://aspn.activestate.com/ASPN/Mail/Message/boost/1140338
>
> "It doesn't compile with Borland C++Builder 4 or BCC 5.5.

Not that I'm worried about those anymore ;-)

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