[C++-sig] Boost iterators

David Abrahams david.abrahams at rcn.com
Fri Apr 19 03:18:18 CEST 2002


----- Original Message -----
From: "Martin Casado" <casado2 at llnl.gov>
> I'm not sure how you would expose vector to act like an iterator since
> interators need to know the current position, so when you call next()
> the position is incremented, bounds are checked, and the correct
> exception is thrown if the bounds are overrun.

Yeah, good point.

> It seems to me that you
> would have to write a C++ wrapper around vector<int> and any other
type you
> would want to expose as an iterator to provide this functionality.  Or
> add a method, __iter__() which returns a type (like the one I created)
> that provides the functionality.
>
> Wouldn't it be simple to have this done within boost so you don't have
> to modify the C++ classes?

Sure; I'm just disputing the approach.

Rather than adding cruft to each function which returns a vector<int>,
What I think you want is a def_iterator() function on class_<>. Then you
just write:

    .add(class_<std::vector<int> >("int_vector")
            .def_iterator())

or possibly a container_class<...> template derived from class_<...>
which does the def_iterator thing for you. If you write this once, you
should be able to return and accept vector<int> in many functions.

-Dave







More information about the Cplusplus-sig mailing list