Why doesn't iter_swap use swap?

chris jefferson caj@cs.york.ac.uk
Tue Aug 24 10:41:00 GMT 2004


Gabriel Dos Reis wrote:

>chris jefferson <caj@cs.york.ac.uk> writes:
>
>| >The usual convervative approach is to assume that if it is there then
>| >someone may have used it.  Then it is a matter of whether you want to
>| >take the blame of breaking a working code -- there might be good
>| >reasons to do so.
>| >
>| >
>| If we decided changing iter_swap was too serious an undertaking at
>| this point, then according to the standard, should we be using swap()
>| or iter_swap() in our implementation of reverse/sort/etc.etc.? I read
>| it as swap personally. If we changed iter_swap it would make no
>| difference as iter_swap would just call swap. If not, then it might be
>| necessary to remove mentions of std::iter_swap(a,b) for swap(*a,*b) so
>| we meet requirements?
>
>Arguments can be constructed for either sides.  I personnaly believe
>that iter_swap should just defer to swap(), thereby providing a point
>of customization.  Of course, that may mean inconveniencing some
>religious anti-ADL camps.
>
>  
>
Ah, I didn't realise that there was religious anti-ADL types :) I would 
have thought that you'd definatly want some way to over-ride swap and 
iter_swap.. but that sounds like a much more large-ranging issue than 
just this.

For now, to get around the problem of having iter_swap able to take 
multiple types, how about something like the code below (this is just 
proof of principle, not serious code ^_^ ). This will call swap() 
whenever the types are the same, but if anyone is still using different 
types, it will just call the old inefficent iter_swap(). This should 
avoid breaking existing code at least? (except now more swaps might get 
called.

Chris


#include<iterator>
using namespace std;

template<class T,class U>
struct __iter_swap_helper { static void swapy(const T& t,const U& u) 
{old_iter_swap(t,u);}
};

template<class T>
struct __iter_swap_helper<T,T> { static void swapy(const T& t,const T& 
u) {swap(*t,*u);}
};

template<class T,class U>
void new_iter_swap(const T &t,const U &u) {
foo<iterator_traits<T>::value_type,iterator_traits<U>::value_type>::__iter_swap_helper(t,u);
}



More information about the Libstdc++ mailing list