boost::iterator_adaptor
Hi, here is small code snippet to demonstrate my problrm: #include <stddef.h> #include <iostream> #include <boost/iterator/iterator_adaptor.hpp> template<typename T, size_t stride> class stride_iterator : public boost::iterator_adaptor<stride_iterator<T, stride>, T*> { public: stride_iterator() : stride_iterator::iterator_adaptor_( 0 ) { } explicit stride_iterator( T * p ) : stride_iterator::iterator_adaptor_( p ) { } private: friend class boost::iterator_core_access; void increment() { this->base_reference() = this->base() + stride; } void deccrement() { this->base_reference() = this->base() - stride; } void advance( ptrdiff_t d ) { this->base_reference() = this->base() + d*stride; } ptrdiff_t distance_to( stride_iterator const & i ) const { return ( i.base() - this->base() )/stride; } }; int main() { int arr[12] = {}; arr[3*2] = 33; arr[5*2] = 55; stride_iterator<int, 2> it( arr ); it[2] = -22; it[3] = it[5]; std::cout << arr[2*2] << ' ' << arr[3*2] << ' ' << arr[5*2] << '\n'; return 0; } It prints "-22 33 55" instead of "-22 55 55" (which I expected) - so "it[3] = it[5];" assignment didn't modifies array. Is it intended behaviour? Or maybe I'm doing something wrong?
Grisha Spivak wrote:
<snip>
it[2] = -22; it[3] = it[5]; std::cout << arr[2*2] << ' ' << arr[3*2] << ' ' << arr[5*2] << '\n';
It prints "-22 33 55" instead of "-22 55 55" (which I expected) - so "it[3] = it[5];" assignment didn't modifies array. Is it intended behaviour? Or maybe I'm doing something wrong?
If you change it[3] = it[5]; with it[3] = 55; it will work. The problem is your iterator_facade::operator[] returns a boost::detail::operator_brackets_proxy which, I'm not sure why, does not seem to implement operator=(operator_brackets_proxy const &). There may be a good reason for this but perhaps the author(s) of the library can answer that. -delfin
"Delfin Rojas" <drojas@moodlogic.com> writes:
Grisha Spivak wrote:
<snip>
it[2] = -22; it[3] = it[5]; std::cout << arr[2*2] << ' ' << arr[3*2] << ' ' << arr[5*2] << '\n';
It prints "-22 33 55" instead of "-22 55 55" (which I expected) - so "it[3] = it[5];" assignment didn't modifies array. Is it intended behaviour? Or maybe I'm doing something wrong?
If you change it[3] = it[5]; with it[3] = 55; it will work. The problem is your iterator_facade::operator[] returns a boost::detail::operator_brackets_proxy which, I'm not sure why, does not seem to implement operator=(operator_brackets_proxy const &). There may be a good reason for this but perhaps the author(s) of the library can answer that.
We didn't think of it ;-) http://www.boost.org/libs/iterator/doc/iterator_facade.html#operator explains why operator[] returns a proxy. You can write: int x = it[5]; it[3] = x; to work around this problem. Jeremy and Thomas: do you think we should change the Mutable_RandomAccessIterator concept to require that it[n] = e work for any e convertible to the iterator's value_type? -- Dave Abrahams Boost Consulting www.boost-consulting.com
David Abrahams <dave <at> boost-consulting.com> writes:
We didn't think of it
http://www.boost.org/libs/iterator/doc/iterator_facade.html#operator
explains why operator[] returns a proxy. You can write:
int x = it[5]; it[3] = x;
to work around this problem.
Thanks, I'm just learning how to use boost.iterator library to make some of my code more generic. Maybe construction like "it[3] = it[5];" will not be used, but anyways it's result looks unintuitive.
participants (3)
-
David Abrahams
-
Delfin Rojas
-
Grisha Spivak