Boost logo

Boost :

Subject: Re: [boost] [move][container] Fast-track reviews for Move and Container?
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2009-08-17 17:13:43


Thomas Klimpel escribió:
>> Type(BOOST_RV_REV(Type) t)
>> : ...() //Empty construction
>> {
>> this->swap(t);
>> }

> I hope I can omit the empty construction, since some of my classes
> have many member variables. But on second thought, I should at least
> initialize the pointers to zero, to avoid undefined behavior in the
> destructor.

This is a generic move constructor, if Type has a trivial destructor
there is no need to mark the type as "moved", but as you say, if you
have some pointers, you should prepare the swapped class destructor to
avoid undefined behaviour.

>> Ditto for move assignment:
>>
>> Type & operator=(BOOST_RV_REV(Type) t)
>> {
>> Type tmp(boost::move(t));
>> this->swap(tmp);
>> }

> I hope I can omit the temporary, because two calls to swap are less
efficient than a single call to swap. I can't find any second thought
that forces me to create a temporary. Do I miss something?

There is no reason if you know your type and you can put t in a
destructible state. This is just a generic ("safe"?) move assignment
operator. You could do:

Type & operator=(BOOST_RV_REV(Type) t)
{
   this->swap(t);
}

if semantically is ok for your Type. For some types (shared_ptr,
containers...) a simple swap can lead to unexpected behavior because the
programmer might not expect any value transference:

shared_ptr<X> a(...), b(..);
//Should b take ownership of a's resources?
//That might be quite surprising.
a = boost::move(b);

Best,

Ion


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