Boost logo

Boost :

From: Hamish Mackenzie (boost_at_[hidden])
Date: 2002-02-05 09:08:14


On Tue, 2002-02-05 at 01:07, Howard Hinnant wrote:
> On Monday, February 4, 2002, at 07:37 PM, Jon Kalb wrote:
>
> > Is move what we really want? Or is it swap? I've thought for awhile that
> > swap was a missing "operator." It seems to be such a fundamental
> > operation.
>
> I agree 100% that swap is fundamental, and I would love to see a swap
> operator. I suspect we're talking about the "next" language, but you
> never know. However, I think move is even more fundamental than swap.
> Consider: if scalars support move semantics (as they do swap), then
> "moving" a scalar would be 3 times as expensive as you need:
>
> template <class T>
> void
> swap(T& a, T& b)
> {
> T tmp(a);
> a = b;
> b = tmp;
> }
>
> Three assignments instead of one!
>
> However if moving a scalar could be accomplished by a single assignment,
> then swap could be built on top of move:
>
> template <class T>
> void
> swap(T& a, T& b)
> {
> T tmp(move(a));
> a = move(b);
> b = move(tmp);
> }
>
> (courtesy Andrew Koenig)
>
> And if move is /defined/ to be a non-throwing operation, then unlike the
> current std::swap, this version built on move could be immediately
> applicable to every class that implemented move semantics *and* be
> non-throwing.

How about

template< class T >
void move( T & destination, T & source )
{
  std::swap( destination, source );
}

void move( int & destination, int & source )
{
  destination = source;
}

...

With similar overloads for classes made up of only scaler types. This
leaves it to the designer of the class to decide if assignment is a
valid optimization.

> I agree that we could do move with swap. I just think it would be
> higher quality to do swap with move.

I am not convinced. Swap in my opinion is more basic as it does not
create or destory any objects.

Hamish Mackenzie


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