Boost logo

Boost :

Subject: Re: [boost] [move] auto-generated operator=
From: David Abrahams (dave_at_[hidden])
Date: 2009-11-16 09:06:40


On Nov 16, 2009, at 7:46 AM, David Abrahams wrote:

>
> On Sep 8, 2009, at 5:52 AM, Ion Gaztañaga wrote:
>
>> We need a third alternative: a new overload set that maintains "const T &" and properly catches non-const rvalues.
>
> I think we have it:
>
> define one copy assignment operator in terms of swap. Copy elision makes it nearly as fast as a move, and we don't get an operator=(T&) infection in derived classes.
>
> I have no problem with requiring move-enablers to supply a swap.

In fact, I don't even think we need to do that. This seems to work pretty well:

#define BOOST_COPYABLE_AND_MOVABLE(TYPE)\
   public:\
   TYPE& operator=(TYPE t)\
   { return this->operator=(static_cast< ::boost::rv<TYPE>&>(t)); } \
   public:\
   operator ::boost::rv<TYPE>&() \
   { return *reinterpret_cast< ::boost::rv<TYPE>* >(this); }\
   operator const ::boost::rv<TYPE>&() const \
   { return *reinterpret_cast<const ::boost::rv<TYPE>* >(this); }\
   private:\
//

--
David Abrahams
BoostPro Computing
http://boostpro.com

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