[C++-sig] Re: custom iterator object

David Abrahams dave at boost-consulting.com
Thu Nov 7 21:33:58 CET 2002

"Mike Rovner" <mike at bindkey.com> writes:

>> > I have the following class, implementing my iterator protocol:
>> >
>> > template<class T>
>> > class Iter
>> > {
>> >     T* First();
>> >     T* Next();
>> > }
>> > returning 0, when iteration is done.
>> >
>> Boost.Python provides several ways to wrap regular C++ iterators as
>> Python iterators. Did you consider using those?
> Sure I did, but couldn't figure out how to do it ;)
> IMHO it's NOT a regular iterator because operations '*' and '++' are
> combined into 'Next'.

No, of course it's not. However, you could consider making a C++
iterator using, say, the Boost Iterator Adaptors library, and then
wrapping that. Maybe that's too much work, though.

>> > I'm trying to wrap it to Python iterator protocol:
>> What's "not good" about it?
> I have problems with return value. In __iter__ I have to return
> self, but there is no Python self yet in wrapping class.  So
> probably I have to duplicate part of the BPL iterator guts, but I
> got lost :(

I'm lost, too. You haven't shown enough of what you're trying to do
for me to be able to help. Please try showing the code you hope to use
to wrap this stuff, and add lots of comments to describe what's

                    David Abrahams
dave at boost-consulting.com * http://www.boost-consulting.com

More information about the Cplusplus-sig mailing list