Boost logo

Boost :

Subject: Re: [boost] Improving the assignment operators of various Boosttypes
From: Peter Dimov (pdimov_at_[hidden])
Date: 2008-09-10 07:41:09


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

I was able to find a post by Valentin Bonnard also suggesting the same:

http://tech.groups.yahoo.com/group/boost/message/5001

Incidentally:

http://aspn.activestate.com/ASPN/Mail/Message/boost/1140338

"It doesn't compile with Borland C++Builder 4 or BCC 5.5.

...

The next problem was associative_vector's assignment operator.
Borland didn't recognise it as one and generated a default.
Then it couldn't disambiguate the generated assignment operator
and the one supplied. I changed this to

Self& operator=(const Self &rhs)
{ Self temp(rhs); swap(temp); return *this; }"

My memory is correct on this one, too. I'm not sure why BCC 5.5.1 didn't
exhibit the problem in my tests.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk