|
Boost : |
Subject: Re: [boost] Improving the assignment operators of various Boost types
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2008-09-10 14:12:53
Niels Dekker - mail address until 2008-12-31 wrote:
> David Abrahams wrote:
>> In the case of copy-elision for by-value arguments and return values,
>> the compiler is explicitly allowed to _assume_ there is no semantic
>> difference between the original rvalue and its copy. That's low-hanging
>> fruit for a compiler writer, that pays huge dividends.
>
> Basically it was concluded that /if/ an assignment operator is
> implemented by means of copy-and-swap, it should preferably be done as
> follows:
>
> T& operator=(T arg)
> {
> arg.swap(*this);
> return *this;
> }
>
> And preferably /not/ as follows:
>
> T& operator=(const T & arg)
> {
> T(arg).swap(*this);
> return *this;
> }
One downside is that self-assignment is made suboptimal. My typical
operator= implementation is:
T& operator= (T const& that)
{
if (this != &that)
T(that).swap(*this);
return *this;
}
Not sure, however, if it is critical enough.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk