[C++-sig] make_constructor + with_custodian_and_ward

Neal Becker ndbecker2 at gmail.com
Thu Jun 1 16:44:16 CEST 2006

Any ideas on this?  B will hold an 'A const&'.  I think I need
'with_custodian_and_ward' to ensure the 'A' object stays alive.  This code
won't compile:

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/with_custodian_and_ward.hpp>
using namespace boost::python;

struct A {};
struct B {
  B (A const& _a) : a (_a) {}
  A const& a;

B* make_B (A const& a) {
  return new B (a);

  class_<B> ("B", no_init)
    .def ("__init__", make_constructor (make_B,
                                        (arg ("A"))));
/usr/local/src/boost.cvs/boost/python/with_custodian_and_ward.hpp:86: error:
invalid initialization of reference of type ‘PyObject* const&’ from
expression of type ‘const boost::python::detail::offset_args<PyObject*,
mpl_::int_<1> >’
/usr/local/src/boost.cvs/boost/python/detail/caller.hpp:50: error: in
passing argument 1 of ‘unsigned int boost::python::detail::arity(PyObject*

More information about the Cplusplus-sig mailing list