Topic: implementation of std::iter_swap
Author: news@be.easynet.net ("news")
Date: Thu, 10 Mar 2005 23:24:38 GMT Raw View
Some stl implementation defines std::iter_swap as follows:
<quote>
template < class It1, class It2 >
void iter_swap(It1 left, It2 right)
{ std::swap( *left, * right ) ; }
<unquote>
Additionally, the std::swap fuction looks like
<quote>
template < class T>
void std::swap(T& left, T& right)
{ ... }
<unquote>
Now in case the dereference operator of the iterator returns a temporary
(in case of a 'proxy' for instance), the call to std::swap fails because
of passing a temporary to a non-const reference.
My question now is: is this implementation of iter_swap correct ?
Thanks,
Toon
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: caj@cs.york.ac.uk (Chris Jefferson)
Date: Thu, 10 Mar 2005 23:57:16 GMT Raw View
news wrote:
> Some stl implementation defines std::iter_swap as follows:
>
> <quote>
> template < class It1, class It2 >
> void iter_swap(It1 left, It2 right)
> { std::swap( *left, * right ) ; }
> <unquote>
>
> Additionally, the std::swap fuction looks like
> <quote>
> template < class T>
> void std::swap(T& left, T& right)
> { ... }
> <unquote>
>
> Now in case the dereference operator of the iterator returns a temporary
> (in case of a 'proxy' for instance), the call to std::swap fails because
> of passing a temporary to a non-const reference.
>
> My question now is: is this implementation of iter_swap correct ?
>
Two parts to this :)
Firstly, you are doing something wrong, as iter_swap requires forward
iterators, and according to table 74 of the standard (forward iterator
requirements) *a returns "T&".
The question "is this a valid way to implement iter_swap" is another
question :) http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html
(issue 187) that the definition of iter_swap will probably be formalised
to say that this is the correct way to implement it (the status is WP or
working paper, which I read to mean "we agree with this, but haven't
stamped it yet).
On other issue you might come across is some implementations of the the
standard library put std:: on all their call to swap, some don't (just
warning you).
To be honest, making proxy iterators is unfortunatly hard. I get the
impression they were going to be supported, then got taken out as too
much trouble (although if you look really hard you can spot places where
they used to be...)
Chris
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: hinnant@metrowerks.com (Howard Hinnant)
Date: Fri, 11 Mar 2005 13:23:22 GMT Raw View
In article <422ffc47$0$1817$6c56d894@reader0.news.be.easynet.net>,
news@be.easynet.net ("news") wrote:
> Some stl implementation defines std::iter_swap as follows:
>
> <quote>
> template < class It1, class It2 >
> void iter_swap(It1 left, It2 right)
> { std::swap( *left, * right ) ; }
> <unquote>
>
> Additionally, the std::swap fuction looks like
> <quote>
> template < class T>
> void std::swap(T& left, T& right)
> { ... }
> <unquote>
>
> Now in case the dereference operator of the iterator returns a temporary
> (in case of a 'proxy' for instance), the call to std::swap fails because
> of passing a temporary to a non-const reference.
>
> My question now is: is this implementation of iter_swap correct ?
iter_swap is currently underspecified. There is a lwg dr with WP status
on this issue:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html
The resolution says that iter_swap just calls swap as you describe.
This isn't officially the standard, but has been voted upon by the
entire committee and applied to the working paper for C++0X.
For any sequence where the dereferenced iterator returns a proxy, and
the algorithm says swap(*i, *j), we run into the identical problem.
Imho, the best way to handle this problem is to overload swap in your
proxy's namespace like so:
void
swap(my_proxy x, my_proxy y)
{
MyType tmp = x;
x = y;
y = tmp;
}
(or other suitable implementation for swapping my_proxy).
-Howard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: news@be.easynet.net ("news")
Date: Fri, 11 Mar 2005 19:40:53 GMT Raw View
Chris Jefferson wrote:
>> Some stl implementation defines std::iter_swap as follows:
>>
>> <quote>
>> template < class It1, class It2 >
>> void iter_swap(It1 left, It2 right)
>> { std::swap( *left, * right ) ; }
>> <unquote>
>>
>> Additionally, the std::swap fuction looks like
>> <quote>
>> template < class T>
>> void std::swap(T& left, T& right)
>> { ... }
>> <unquote>
>>
>> Now in case the dereference operator of the iterator returns a
>> temporary (in case of a 'proxy' for instance), the call to std::swap
>> fails because of passing a temporary to a non-const reference.
>>
>> My question now is: is this implementation of iter_swap correct ?
>>
>
> Firstly, you are doing something wrong, as iter_swap requires forward
> iterators, and according to table 74 of the standard (forward iterator
> requirements) *a returns "T&".
but a bidirectional iterator might return soth. 'convertible to T' as
specified by table 75.
>
> The question "is this a valid way to implement iter_swap" is another
> question :) http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html
> (issue 187) that the definition of iter_swap will probably be formalised
> to say that this is the correct way to implement it (the status is WP or
> working paper, which I read to mean "we agree with this, but haven't
> stamped it yet).
>
> On other issue you might come across is some implementations of the the
> standard library put std:: on all their call to swap, some don't (just
> warning you).
>
> To be honest, making proxy iterators is unfortunatly hard. I get the
> impression they were going to be supported, then got taken out as too
> much trouble (although if you look really hard you can spot places where
> they used to be...)
As a note to defect 187 it is indeed mentioned that since the Kona
meeting, proxy iterators are not permitted anymore. Do you know where I
can find information about this. Thanks.
Toon Knapen
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]