Boost logo

Boost :

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

On Tue, 2002-02-05 at 01:24, Beman Dawes wrote:
> I assume Jon's reference to Herb Sutter is to Herb's "Exceptional C++"
> (Addison Wesley) which I was just reading the other day. While reading
> items 8 through 19, I kept wondering if some of the uses of swap Herb is
> advocating wouldn't better be served by move. While swap would always
> work, move might generate less code in the cases where it was
> applicable. But I haven't done the analysis, so don't have an opinion
> yet. I think there is a (swap vs move) rule-of-thumb hiding there, but I'm
> not smart enough to identify it without a lot more study.

Only if move can be implemented as a non-throw operation. My preference
would be to define move like this

template< class T >
void move( T & destination, T & source )
  using namespace std;
  swap( destination, source );

Overloads could be added for scalers and classes where...

1) Assignment is a no-throw
2) Destructors are trivial (ie. it doesn't matter that the original
source will be destroyed and not the destination)

For example

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

"move" could then be used instead of "swap" in many of cases given in
Exceptional C++.

Only thing that I would worry about is that it would be too easy to get
the arguments around the wrong way. The bug would then be hidden so
perhaps something like this would help...

template< class T >
class move_helper
  T & destionation_;
  move_helper( T & d ) : destination_( d ) {}
  operator =( T & source )
    move( destination, source );

template< class T >
move_helper< T > move( T & destination )
  return move_helper< T >( destination );

This would allow the user to write
move( x ) = y;

Hamish Mackenzie

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