Boost logo

Boost :

Subject: Re: [boost] Improving the assignment operators of various Boosttypes
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2008-09-11 06:42:46

On Thu, Sep 11, 2008 at 11:07 AM, Niels Dekker - mail address until
2008-12-31 <nd_mail_address_valid_until_2008-12-31_at_[hidden]> wrote:
> David Abrahams wrote:
>>>> In generic code I strongly prefer
>>>> T& operator=(T arg)
>>>> {
>>>> swap(*this, arg);
>>>> return *this;
>>>> }
>> I'm just saying, I guess, that the form using the member
>> forces me to define a swap member even though it's not really needed
>> by generic code.
> I think we should stick to the guideline to always have a swap member
> function for a class, whenever the class has a custom free swap function.
> Don't you agree?
> BTW the swap member function offers a C++03 way to "move assign" an rvalue:
> std::vector<int> generate_some_data(void);
> std::vector<int> my_data;
> // Move the result into my_data:
> generate_some_data().swap(my_data);

I guess this is a common idiom, I use it a lot.

OTOH if my_data::operator=() used pass by value (which in the case of
standard containers is, unfortunately, not true), the member swap
wouldn't be necessary.

In practice this means that, for your own classes, or you implement
the 'smart' operator= or you need the member swap (to swap out of
temporaries). You do not need both.


Boost list run by bdawes at, gregod at, cpdaniel at, john at