Re: [iterator] small feature request

Eric Niebler wrote:
The iterator_adaptor<> template has a base() function to access the underlying iterator. Well and good. However, iterator_adaptor<> doesn't have a public typedef for the iterator type returned from this function. Can it be added?
Rationale: Given some iterator of type T that is built of an unknown number of adaptors, extract the underlying iterator. Consider:
template<class Iter> typename disable_if<is_adapted<Iter>,Iter>::type base(Iter const & it) { return it; }
template<class D,class B,class V,class T,class R,class Di> ?????????? base(iterator_adaptor<D,B,V,T,R,Di> const & it) { return base(it.base()); }
I can't think of a way to write the return type of the second overload. If iterator_adaptor had a nested typedef for its Base template parameter, this would be very straightforward.
Seems reasonable. Can you submit a patch? -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams wrote:
Eric Niebler wrote:
Rationale: Given some iterator of type T that is built of an unknown number of adaptors, extract the underlying iterator. Consider:
template<class Iter> typename disable_if<is_adapted<Iter>,Iter>::type base(Iter const & it) { return it; }
template<class D,class B,class V,class T,class R,class Di> ?????????? base(iterator_adaptor<D,B,V,T,R,Di> const & it) { return base(it.base()); }
I can't think of a way to write the return type of the second overload.
I am not fully awake yet, but isn't that just B ? Thomas -- Thomas Witt witt@acm.org

Thomas Witt wrote:
David Abrahams wrote:
Eric Niebler wrote:
Rationale: Given some iterator of type T that is built of an unknown number of adaptors, extract the underlying iterator. Consider:
template<class Iter> typename disable_if<is_adapted<Iter>,Iter>::type base(Iter const & it) { return it; }
template<class D,class B,class V,class T,class R,class Di> ?????????? base(iterator_adaptor<D,B,V,T,R,Di> const & it) { return base(it.base()); }
I can't think of a way to write the return type of the second overload.
I am not fully awake yet, but isn't that just B ?
Thomas
Good morning, Thomas! :-) If I were just returning it.base() then it would be B. Since I'm returning base(it.base()), it could be B or it could be some other type if B is itself an adapted iterator. I don't know how I've managed to get this far in life without knowing how to create a patch file. :-P I'll figure it out and submit one. -- Eric Niebler Boost Consulting www.boost-consulting.com

Eric Niebler wrote:
I don't know how I've managed to get this far in life without knowing how to create a patch file. :-P I'll figure it out and submit one.
Cool. Don't forget the test and doc patches! -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams wrote:
Don't forget the test and doc patches!
Attached. I also corrected a very small problem I saw in the iterators documentation while I was in there. P.S. Happy New Year! -- Eric Niebler Boost Consulting www.boost-consulting.com Index: boost/iterator/iterator_adaptor.hpp =================================================================== RCS file: /cvsroot/boost/boost/boost/iterator/iterator_adaptor.hpp,v retrieving revision 1.14 diff -b -d -u -u -r1.14 iterator_adaptor.hpp --- boost/iterator/iterator_adaptor.hpp 2 Sep 2004 15:40:40 -0000 1.14 +++ boost/iterator/iterator_adaptor.hpp 2 Jan 2005 21:40:58 -0000 @@ -278,6 +278,8 @@ { } + typedef Base base_type; + Base const& base() const { return m_iterator; } Index: libs/iterator/doc/iterator_adaptor_ref.rst =================================================================== RCS file: /cvsroot/boost/boost/libs/iterator/doc/iterator_adaptor_ref.rst,v retrieving revision 1.14 diff -b -d -u -u -r1.14 iterator_adaptor_ref.rst --- libs/iterator/doc/iterator_adaptor_ref.rst 1 Nov 2004 21:23:47 -0000 1.14 +++ libs/iterator/doc/iterator_adaptor_ref.rst 2 Jan 2005 21:40:58 -0000 @@ -19,7 +19,8 @@ friend class iterator_core_access; public: iterator_adaptor(); - explicit iterator_adaptor(Base iter); + explicit iterator_adaptor(Base const& iter); + typedef Base base_type; Base const& base() const; protected: typedef iterator_adaptor iterator_adaptor\_; @@ -115,7 +116,7 @@ ``m_iterator`` default constructed. -``explicit iterator_adaptor(Base iter);`` +``explicit iterator_adaptor(Base const& iter);`` :Returns: An instance of ``iterator_adaptor`` with ``m_iterator`` copy constructed from ``iter``. Index: libs/iterator/test/iterator_adaptor_test.cpp =================================================================== RCS file: /cvsroot/boost/boost/libs/iterator/test/iterator_adaptor_test.cpp,v retrieving revision 1.7 diff -b -d -u -u -r1.7 iterator_adaptor_test.cpp --- libs/iterator/test/iterator_adaptor_test.cpp 19 Aug 2004 15:11:49 -0000 1.7 +++ libs/iterator/test/iterator_adaptor_test.cpp 2 Jan 2005 21:40:58 -0000 @@ -316,6 +316,19 @@ assert((*i).m_x == i->foo()); } + // check that base_type is correct + { + // Test constant iterator idiom + typedef ptr_iterator<int> BaseIter; + + test = static_assert_same<BaseIter::base_type,int*>::value; + test = static_assert_same<constant_iterator<BaseIter>::base_type,BaseIter>::value; + + typedef modify_traversal<BaseIter, boost::incrementable_traversal_tag> IncrementableIter; + + test = static_assert_same<IncrementableIter::base_type,BaseIter>::value; + } + std::cout << "test successful " << std::endl; (void)test; return 0;

resending ... Eric Niebler wrote:
David Abrahams wrote:
Don't forget the test and doc patches!
Attached. I also corrected a very small problem I saw in the iterators documentation while I was in there.
-- Eric Niebler Boost Consulting www.boost-consulting.com Index: boost/iterator/iterator_adaptor.hpp =================================================================== RCS file: /cvsroot/boost/boost/boost/iterator/iterator_adaptor.hpp,v retrieving revision 1.14 diff -b -d -u -u -r1.14 iterator_adaptor.hpp --- boost/iterator/iterator_adaptor.hpp 2 Sep 2004 15:40:40 -0000 1.14 +++ boost/iterator/iterator_adaptor.hpp 2 Jan 2005 21:40:58 -0000 @@ -278,6 +278,8 @@ { } + typedef Base base_type; + Base const& base() const { return m_iterator; } Index: libs/iterator/doc/iterator_adaptor_ref.rst =================================================================== RCS file: /cvsroot/boost/boost/libs/iterator/doc/iterator_adaptor_ref.rst,v retrieving revision 1.14 diff -b -d -u -u -r1.14 iterator_adaptor_ref.rst --- libs/iterator/doc/iterator_adaptor_ref.rst 1 Nov 2004 21:23:47 -0000 1.14 +++ libs/iterator/doc/iterator_adaptor_ref.rst 2 Jan 2005 21:40:58 -0000 @@ -19,7 +19,8 @@ friend class iterator_core_access; public: iterator_adaptor(); - explicit iterator_adaptor(Base iter); + explicit iterator_adaptor(Base const& iter); + typedef Base base_type; Base const& base() const; protected: typedef iterator_adaptor iterator_adaptor\_; @@ -115,7 +116,7 @@ ``m_iterator`` default constructed. -``explicit iterator_adaptor(Base iter);`` +``explicit iterator_adaptor(Base const& iter);`` :Returns: An instance of ``iterator_adaptor`` with ``m_iterator`` copy constructed from ``iter``. Index: libs/iterator/test/iterator_adaptor_test.cpp =================================================================== RCS file: /cvsroot/boost/boost/libs/iterator/test/iterator_adaptor_test.cpp,v retrieving revision 1.7 diff -b -d -u -u -r1.7 iterator_adaptor_test.cpp --- libs/iterator/test/iterator_adaptor_test.cpp 19 Aug 2004 15:11:49 -0000 1.7 +++ libs/iterator/test/iterator_adaptor_test.cpp 2 Jan 2005 21:40:58 -0000 @@ -316,6 +316,19 @@ assert((*i).m_x == i->foo()); } + // check that base_type is correct + { + // Test constant iterator idiom + typedef ptr_iterator<int> BaseIter; + + test = static_assert_same<BaseIter::base_type,int*>::value; + test = static_assert_same<constant_iterator<BaseIter>::base_type,BaseIter>::value; + + typedef modify_traversal<BaseIter, boost::incrementable_traversal_tag> IncrementableIter; + + test = static_assert_same<IncrementableIter::base_type,BaseIter>::value; + } + std::cout << "test successful " << std::endl; (void)test; return 0;
participants (3)
-
David Abrahams
-
Eric Niebler
-
Thomas Witt