[C++-sig] make_constructor with custodian_and_ward not possible?

André Anjos andre.dos.anjos at gmail.com
Tue Jan 15 12:21:42 CET 2008


Hi, I'm trying to inject constructors to an existing class using Boost.Python,
to which I have not access to (for modifying the source). The basic Idea is
reproduced in the example bellow. In the way it is, it compiles. If the 
comments on the lines with the "custodian_and_ward" are removed (any of them),
gcc (3.4)/boost (1.33.1) complains:

/afs/cern.ch/sw/lcg/external/Boost/1.33.1/slc4_ia32_gcc34/include/
boost-1_33_1/boost/python/with_custodian_and_ward.hpp:25:
error: no matching function for call to `get(mpl_::int_<0>, const
boost::python::detail::offset_args<PyObject*, mpl_::int_<1> >&)'

Here is what I'm trying:

#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost::python;

class Holder {
  public:
    Holder(int x): my_x() { my_x[0] = x; /*...*/ my_x[4] = x; }
    const int* get_c_array() const { return my_x; }
  private:
    int my_x[5];
};

class Pointer {
  public:
    Pointer (const int* ref): cannot_copy(ref) {}
    //... methods to access the contents of "cannot_copy"
  private:
    const int* cannot_copy; //for a special reason I cannot copy this integer
};

//Make Foo to use data in Bar, want it to be the custodian in this case
boost::shared_ptr<Pointer> make_pointer(const Holder& h) {
  return boost::shared_ptr<Pointer>(new Pointer(h.get_c_array()));
}

BOOST_PYTHON_MODULE(test)
{
  class_<Holder>("Holder", init<int>())
    ;

  class_<Pointer>("Pointer", no_init)
    //.def("__init__", make_constructor(&make_pointer,
with_custodian_and_ward_postcall<0, 1>())) //if comment out, gives error above
    //.def("__init__", make_constructor(&make_pointer,
with_custodian_and_ward<1, 2>())) //if comment out, gives error above
    .def("__init__", make_constructor(&make_pointer, default_call_policies()))
    ;
}

Always in the same point. Is this what I'm trying not possible? Many thanks 
for any hint.

Andre





More information about the Cplusplus-sig mailing list