[C++-sig] Boost iterators

Martin Casado casado2 at llnl.gov
Fri Apr 26 21:42:01 CEST 2002


> >
> >   I then simply add this to my class with def(..)
> >
> >     class_<vector<int> >("vector_int")
> >        .def_init()
> >        .def("__iter__",static_cast<PyIter<vector<int> >
> > (*)(vector<int>&)>(foo)) >
>
> I don't understand why you need the cast here; it seems as though
> &foo<vector<int> > would do just as well.
>


You don't need the cast for nontemplate methods, however without
the cast on a templated method I get the following error with KCC

 line 72: error: no instance of overloaded function
          "boost::python::class_<T, X1, X2, X3>::def [with T=std::vector<int,
          std::allocator<int>>, X1=boost::python::detail::not_specified,
          X2=boost::python::detail::not_specified,
          X3=boost::python::detail::not_specified]" matches the argument list
            argument types are: (char [9], <unknown-type>)
                  .def("__iter__",&foo<vector<int> >)

It works fine with the cast however.

> >   1. The return value maintins references to the container's
>
> iterators.
>
> >   Is there a way to keep the container alive as long as the python
> >   iterator object is alive?  Something like
>
> return_internal_reference(),
>
> >   but handles values which maintain internal references.
>
> How about with_custodian_and_ward<0,1>() should handle it, right? You
> want the self argument (argument 1) to stay alive as long as the return
> value.



Thats what I figured but I haven't been able to get
with_costudian_and_ward<0,1>() to work in this case.  Given the
following simple setup..

class A{ };
class B { };
 
A getA(const B& b)
{ return A(); }


 This works fine: 
   class_<B>("B").def("getA",getA) ;

 However, this doesn't

  .def("getA",getA,with_custodian_and_ward<0,1>())  


Specifically I get the following error..

"/usr/dnta/kull/developers/thirdPartySoftware/boost-chuzo/boost/boost/python/with_custodian_and_ward.hpp", 
line 33: error:
          incomplete type is not allowed
      BOOST_STATIC_ASSERT(custodian > 0);

and the following warning:

"/usr/dnta/kull/developers/thirdPartySoftware/boost-chuzo/boost/boost/python/with_custodian_and_ward.hpp", 
line 38: warning:
          integer conversion resulted in truncation
      PyObject* nurse = PyTuple_GetItem(args_, custodian - 1);


                        ~~m





More information about the Cplusplus-sig mailing list