Boost logo

Boost Users :

From: Grisha Spivak (rustedstranger_at_[hidden])
Date: 2006-05-12 07:32:42


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?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net