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

Mike Rovner mike at bindkey.com
Tue Nov 12 06:43:43 CET 2002

"David Abrahams" <dave at boost-consulting.com> wrote in message
news:u8yzzxxjb.fsf at boost-consulting.com...
> > I have problem _creating that iterator_ from another object.
> > For creating an iterator I have to call member function.
> > For calling it I have to have a C++ object reference, i.e.
> >
> > Scheme& s;
> > MyIter s.*pm();
> >
> > Now I'm trying to wrap that class (that shall return an iterator):
> >
> >   class_<Scheme>("Scheme")
> >     .def("__iter__", mem_fun_ref(&Scheme::GetIter))
> Why aren't you just using
>      .def("__iter__", &Scheme::GetIter)
> here? What is mem_fun_ref supposed to buy you?

I'm sorry again. I'm using mem_fun_ref because

class Scheme {
//has only
   CustomIter GetIter();

and I use

class MyIter {
   MyIter(CustomIter& init);
   T next();

  .def("__iter__", identity)
  .def("next, &MyIter::next)

instead of CustomIter to convert it to Python iterator protocol.
So mem_fun_ref returns some
mem_fun_ref_t<T,Scheme> object instance with operator() which when called
with Scheme& produces MyIter object.
That wrapped MyIter object shall be return value from wrapped
Scheme::GetIter() member function.

I already figured out that if I want call .def with object it shall be
'object'. So I need to wrap mem_fun_ref_t<T,Scheme> into callable python
object and instantiate it, right?

Wrapping is easy:

typedef mem_fun_ref_t<T,Scheme> Gen;
class_<Gen>("_gen").def("__call__", detail::operator_<Gen>()); // is it

but how to instantiate that new object?


More information about the Cplusplus-sig mailing list