Boost logo

Boost :

Subject: Re: [boost] Improving the assignment operators of various Boosttypes
From: David Abrahams (dave_at_[hidden])
Date: 2008-09-10 15:30:48


on Wed Sep 10 2008, "Giovanni Piero Deretta" <gpderetta-AT-gmail.com> wrote:

> If you do not want to deal with move emulation (which I've found very
> brittle in complex expressions),

Do you mean the old-style move emulation that relied on T(T&) copy ctors
(known to be brittle) or does this assessment apply also to the
adobe-style emulation
(http://stlab.adobe.com/group__move__related.html)? If so, could you
explain in more detail?

> a simple way to to gain the advantage
> of T::operator=(T rhs) even when assigning from lvalues is something
> like:
>
> template<class T> T destructive_copy(T& x) {
> using std::swap;
> T result;
> swap(result, x);
> return x;
> }

This only works when T is both default constructible and has an
optimized swap. The latter is detectable, roughly speaking, so the
function could be altered to handle it, but the former is not, which it
seems to me makes destructive_copy not very useful in generic code.

...

> For types which do not have an optimized swap is suboptimal, so some
> sfinae trick on is_swappable might be needed. Ah, of course it
> requires T to be DefaultConstructible, and most importantly
> CopyConstructible so it doesn't handle move only types.

That's a lot of caveats.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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