diff --git a/include/boost/range/iterator_range.hpp b/include/boost/range/iterator_range.hpp index 8379c54..9b30b7b 100755 --- a/include/boost/range/iterator_range.hpp +++ b/include/boost/range/iterator_range.hpp @@ -142,6 +142,14 @@ namespace boost //! This type typedef iterator_range this_type; + + //! Refence type + // + // Needed because value-type is the same for + // const and non-const iterators + // + typedef BOOST_DEDUCED_TYPENAME + iterator_reference::type reference; //! const_iterator type /*! @@ -306,20 +314,20 @@ namespace boost #endif public: // convenience - value_type& front() const + reference front() const { BOOST_ASSERT( !empty() ); return *m_Begin; } - value_type& back() const + reference back() const { BOOST_ASSERT( !empty() ); IteratorT last( m_End ); return *--last; } - value_type& operator[]( size_type sz ) const + reference operator[]( size_type sz ) const { //BOOST_STATIC_ASSERT( is_random_access ); BOOST_ASSERT( sz < size() ); diff --git a/test/iterator_range.cpp b/test/iterator_range.cpp index fbf168c..2b28b09 100755 --- a/test/iterator_range.cpp +++ b/test/iterator_range.cpp @@ -1,6 +1,6 @@ // Boost.Range library // -// Copyright Thorsten Ottosen 2003-2004. Use, modification and +// Copyright Thorsten Ottosen & Larry Evans 2003-2005. Use, modification and // distribution is subject to the Boost Software License, Version // 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) @@ -26,6 +26,8 @@ using namespace boost; using namespace std; + +void check_reference_type(); void check_iterator_range() { @@ -91,6 +93,8 @@ void check_iterator_range() BOOST_CHECK( rrr == "ello worl" ); rrr = make_iterator_range( rrr, -1, 1 ); BOOST_CHECK( rrr == str ); + + check_reference_type(); } @@ -105,3 +109,33 @@ test_suite* init_unit_test_suite( int argc, char* argv[] ) return test; } + +// +// +// Check that constness is propgated correct from +// the iterator types. +// +// Test contributed by Larry Evans. +// + +template< class Container > +void test_iter_range( Container& a_cont ) +{ + typedef BOOST_DEDUCED_TYPENAME range_result_iterator::type citer_type; + typedef iterator_range riter_type; + riter_type a_riter( make_iterator_range( a_cont ) ); + a_riter.front(); + a_riter.back(); + int i = a_riter[0]; +} + + + +void check_reference_type() +{ + typedef vector veci_type; + veci_type a_vec; + a_vec.push_back( 999 ); + test_iter_range(a_vec); + test_iter_range(a_vec); +}