[C++-sig] Random access iterators, and sequence protocol.

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Sat Jul 5 15:41:05 CEST 2003


Hi,

I have a container class that defines an operator [] (std::size_t) and
also has a size function that returns the number of elements in the
container.  It does not provide access to an internal iterator.  To
get the '[]' wrapped with Pyste I need to rename the function as
__getitem__ of course.  I also renamed size to __len__.  However, I
would like to do this:

for i in container:
   i.something()

To do this __getitem__ needs to generate an IndexError when the index
is greater than or equal to size.  Do I _have_ to register an
exception translator from something like std::range_error?  Is there
an easier way to do this?  The docs don't reveal any easier solutions.
It would be nice if this were also as easy as doing it via range, or
iterator. :)

Thanks.

cheers,
prabhu




More information about the Cplusplus-sig mailing list