 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
news Guest
|
Posted: Thu Mar 10, 2005 11:24 pm Post subject: implementation of std::iter_swap |
|
|
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 ]
|
|
| Back to top |
|
 |
Chris Jefferson Guest
|
Posted: Thu Mar 10, 2005 11:57 pm Post subject: Re: implementation of std::iter_swap |
|
|
news wrote:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
Howard Hinnant Guest
|
Posted: Fri Mar 11, 2005 1:23 pm Post subject: Re: implementation of std::iter_swap |
|
|
In article <422ffc47$0$1817$6c56d894 (AT) reader0 (DOT) news.be.easynet.net>,
[email]news (AT) be (DOT) easynet.net[/email] ("news") wrote:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
news Guest
|
Posted: Fri Mar 11, 2005 7:40 pm Post subject: Re: implementation of std::iter_swap |
|
|
Chris Jefferson wrote:
| Quote: | 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.
| Quote: |
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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|