[range/iterator] operator[] forwarding

Hi gurus, under RC_1_34, #include <boost/range.hpp> #include <boost/iterator/counting_iterator.hpp> int main() { boost::make_iterator_range( boost::make_counting_iterator(0), boost::make_counting_iterator(5) )[2]; } VC++7.1 and GCC3.4 says a warning something like "returning address of local variable or temporary". counting_iterator is implemented by iterator_facade, whose operator[] returns *value*. In fact, the result type of iterator_facade's operator[] seems unspecified. Is it possible to do forwarding of iterator's operator[], which is tried by iterator_range? -- Shunsuke Sogame

Shunsuke Sogame wrote:
Hi gurus, under RC_1_34,
#include <boost/range.hpp> #include <boost/iterator/counting_iterator.hpp>
int main() { boost::make_iterator_range( boost::make_counting_iterator(0), boost::make_counting_iterator(5) )[2]; }
VC++7.1 and GCC3.4 says a warning something like "returning address of local variable or temporary".
I think counting_iterator is not a valid random access iterator because operator[]() does not yield a reference.
counting_iterator is implemented by iterator_facade, whose operator[] returns *value*. In fact, the result type of iterator_facade's operator[] seems unspecified. Is it possible to do forwarding of iterator's operator[], which is tried by iterator_range?
Counting iterator should define its reference-type to be the type it returns from operator[]() (even though it is not a reference). Then I think it would work. Do the authors of the iterator lib agree? -Thorsten

Thorsten Ottosen <thorsten.ottosen@dezide.com> writes:
Shunsuke Sogame wrote:
Hi gurus, under RC_1_34,
#include <boost/range.hpp> #include <boost/iterator/counting_iterator.hpp>
int main() { boost::make_iterator_range( boost::make_counting_iterator(0), boost::make_counting_iterator(5) )[2]; }
VC++7.1 and GCC3.4 says a warning something like "returning address of local variable or temporary".
I think counting_iterator is not a valid random access iterator because operator[]() does not yield a reference.
That is not a requirement of random access iterator.
counting_iterator is implemented by iterator_facade, whose operator[] returns *value*. In fact, the result type of iterator_facade's operator[] seems unspecified. Is it possible to do forwarding of iterator's operator[], which is tried by iterator_range?
Counting iterator should define its reference-type to be the type it returns from operator[]() (even though it is not a reference). Then I think it would work.
Do the authors of the iterator lib agree?
No. There are good reasons that it works the way it does. Please read the iterator library docs and refer to the standard if you want to understand this stuff; I don't have time to go into detail right now, sorry. -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams wrote:
Thorsten Ottosen <thorsten.ottosen@dezide.com> writes:
Shunsuke Sogame wrote:
Hi gurus, under RC_1_34,
#include <boost/range.hpp> #include <boost/iterator/counting_iterator.hpp>
int main() { boost::make_iterator_range( boost::make_counting_iterator(0), boost::make_counting_iterator(5) )[2]; }
VC++7.1 and GCC3.4 says a warning something like "returning address of local variable or temporary".
I think counting_iterator is not a valid random access iterator because operator[]() does not yield a reference.
That is not a requirement of random access iterator.
Reading the working draft from n2009, table 93 states that a[n] should be convertible to const T&.
Counting iterator should define its reference-type to be the type it returns from operator[]() (even though it is not a reference). Then I think it would work.
Do the authors of the iterator lib agree?
No. There are good reasons that it works the way it does.
So there is no way to infer the the type of oprerator[]() until we get decltype? -Thorsten

Thorsten Ottosen <thorsten.ottosen@dezide.com> writes:
David Abrahams wrote:
Thorsten Ottosen <thorsten.ottosen@dezide.com> writes:
I think counting_iterator is not a valid random access iterator because operator[]() does not yield a reference.
That is not a requirement of random access iterator.
Reading the working draft from n2009, table 93 states that
a[n] should be convertible to const T&.
Yes. That does not require it to yield a reference.
Counting iterator should define its reference-type to be the type it returns from operator[]() (even though it is not a reference). Then I think it would work.
Do the authors of the iterator lib agree?
No. There are good reasons that it works the way it does.
So there is no way to infer the the type of oprerator[]() until we get decltype?
Or concept support; either would do. -- Dave Abrahams Boost Consulting www.boost-consulting.com

Shunsuke Sogame wrote:
Hi gurus, under RC_1_34,
#include <boost/range.hpp> #include <boost/iterator/counting_iterator.hpp>
int main() { boost::make_iterator_range( boost::make_counting_iterator(0), boost::make_counting_iterator(5) )[2]; }
VC++7.1 and GCC3.4 says a warning something like "returning address of local variable or temporary".
counting_iterator is implemented by iterator_facade, whose operator[] returns *value*. In fact, the result type of iterator_facade's operator[] seems unspecified. Is it possible to do forwarding of iterator's operator[], which is tried by iterator_range?
We could add operator() which would return operator[]()'s result by value. Is anybody opposed to that? -Thorsten

Thorsten Ottosen <thorsten.ottosen@dezide.com> writes:
Shunsuke Sogame wrote:
Hi gurus, under RC_1_34,
#include <boost/range.hpp> #include <boost/iterator/counting_iterator.hpp>
int main() { boost::make_iterator_range( boost::make_counting_iterator(0), boost::make_counting_iterator(5) )[2]; }
VC++7.1 and GCC3.4 says a warning something like "returning address of local variable or temporary".
counting_iterator is implemented by iterator_facade, whose operator[] returns *value*. In fact, the result type of iterator_facade's operator[] seems unspecified. Is it possible to do forwarding of iterator's operator[], which is tried by iterator_range?
We could add operator() which would return operator[]()'s result by value.
Is anybody opposed to that?
Probably. What is the point of doing that? -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams wrote:
Thorsten Ottosen <thorsten.ottosen@dezide.com> writes:
Shunsuke Sogame wrote:
Hi gurus, under RC_1_34,
#include <boost/range.hpp> #include <boost/iterator/counting_iterator.hpp>
int main() { boost::make_iterator_range( boost::make_counting_iterator(0), boost::make_counting_iterator(5) )[2]; }
VC++7.1 and GCC3.4 says a warning something like "returning address of local variable or temporary".
We could add operator() which would return operator[]()'s result by value.
Is anybody opposed to that?
Probably. What is the point of doing that?
It would fix our users problem, so he can use () to do what he want. -Thorsten

Thorsten Ottosen said: (by the date of Thu, 12 Oct 2006 10:48:56 +0200)
We could add operator() which would return operator[]()'s result by value.
Is anybody opposed to that?
Probably. What is the point of doing that?
It would fix our users problem, so he can use () to do what he want.
More consequence then, and forward all operators. But in this scenario some unintended behaviour can appear related to forwarding not intended by the user. -- Janek Kozicki |

Janek Kozicki <janek_listy@wp.pl> writes:
Thorsten Ottosen said: (by the date of Thu, 12 Oct 2006 10:48:56 +0200)
We could add operator() which would return operator[]()'s result by value.
Is anybody opposed to that?
Probably. What is the point of doing that?
It would fix our users problem, so he can use () to do what he want.
More consequence then, and forward all operators.
Sorry, I can't parse that.
But in this scenario some unintended behaviour can appear related to forwarding not intended by the user.
loster and loster. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
David Abrahams
-
Janek Kozicki
-
Shunsuke Sogame
-
Thorsten Ottosen