Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-09-02 05:50:56


Dan Eloff <dan.eloff_at_[hidden]> writes:

> If this is my fault the docs need to be more explanatory

I don't think so, unless it's Boost.Python's job to explain how to
build C++ iterators. Your iterator is nonconforming in many ways. I
suggest you use the Boost Iterator Library if you need to build one
(http://www.boost.org/libs/iterator). In this case, you could
probably use a transform_iterator over a counting_iterator.

The problem here is that your operator++ doesn't return a copy of
the original iterator, the way it should (but there are lots of other
problems, so use the iterator library instead):

> The code to reproduce this is:
>
> #include <iterator>
>
> #include <boost/python.hpp>
>
> namespace py = boost::python;
>
> struct ctn
> {
> struct iterator: public std::iterator< std::input_iterator_tag, py::str >
> {
> int mNum;
>
> iterator( int num = 0 ): mNum( num ) {}
>
> py::str operator *() const
> {
> char temp[20];
> const char * str = itoa( mNum, temp, 10 );
> return py::str( str, strlen( str ) );
> }

                 iterator operator ++(int)
                 {
            iterator tmp = *this;
                         mNum++;
                         return tmp;
                 }

> friend bool operator ==( iterator lhs, iterator rhs )
> {
> return ( lhs.mNum == rhs.mNum );
> }
> };
>
> iterator begin()
> {
> return iterator( 0 );
> }
>
> iterator end()
> {
> return iterator( 3 ); // the range (0, 1, 2) three items
> }
> };
>

HTH,

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk