Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2003-09-09 10:30:31


Thomas Witt wrote:
> Daniel,
>
> Daniel Frey wrote:
>
>> OTOH you need to provide a swap function, so you have to take into
>> account that the class needs to support it anyway by some code.
>
> ?

This means that swappability (right word?) is something the user needs
to add to a class explicitly. And there are several options how this can
be done.

>> Some classes might not do that with a member function swap, but with a
>> friend function that has two arguments. Also, most classes that have a
>> member-function also provide a free function with two parameters
>> resolving to the member functions. That given, this might also be a
>> reasonable implementation which might work on more compilers today:
>
> Daniel John is talking about std::swap. Your solution does not adress
> one of the fundamental problems I see. Having a member, what might be
> the natural thing to do, would require a free function as well. This is
> tedious and should be avoided.

I think I understand what John is talking about. But I think that a
member function is simply the wrong way to go for a class that wants to
provide swap-functionality. A member-function swap which takes only one
parameter makes a wrong statement as a member function's primary focus
is on the member. But swapping two instances of a class should treat
both arguments equally, thus a free (or friend) function 'void swap( T&,
T& )' is the natural signature that I would expect. And this doesn't
require a member-swap function at all. Also, the user will use
swap(a,b), not a.swap(b) as the latter is not available for build-in
types like int. If we call swap(T&,T&), why should we ask a class to
provide something else??

>> namespace boost
>> {
>> template< typename T >
>> inline void swap( T& lhs, T& rhs )
>> {
>> using std::swap;
>> swap( lhs, rhs );
>> }
>> }
>
> This is a user solution to libraries(users)-cannot-overload-std::swap.
> AFAICS this is not a solution to the std lib problem.

It just shows the direction. OK, how about this:

namespace detail_swap_implementation
{
   template< typename T >
   void swap( T& lhs, T& rhs )
   {
     T tmp( lhs );
     lhs = rhs;
     rhs = tmp;
   }
}

namespace std
{
   template< typename T >
   inline void swap( T& lhs, T& rhs )
   {
     using namespace ::detail_swap_implementation;
     swap( lhs, rhs );
   }
}

> This is _a_ question but not _the_ question.

OK. So, would you like to answer a question? What interface should we
request? Am I the only one who thinks that a member-function "swap" is
the wrong way? Another a question: John already said that his solution
doesn't work on a lot of todays compilers. Maybe we can make it work,
but shouldn't the approach I propose be easier for todays compilers?

Regards, Daniel

-- 
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

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