开发者

Exporting non-default-constructible classes with Boost.Python

How do I export a non-default-constructible class with Boost.Python?

This code:

class EventHandle {
 public:
  EventHandle() = delete;
  EventHandle(boost::shared_ptr<EventManager> const& em): event_manager_(em) {}
  EventHandle(EventHandle const&) = delete;
  ~EventHandle();
  shared_ptr<EventManager> event_manager_;
}
class_<EventHandle, noncopyable,
  init<boost::shared_ptr<EventManager> const&>>("EventHandle")

gives the following error:

/opt/local/include/boost/python/pointee.hpp: In instantiation of 'boost::python::detail::pointee_impl<false>::apply<boost::python::init<const boost::shared_ptr<EventManager>&> >':
/opt/local/include/boost/python/pointee.hpp:38:1:   instantiated from 'boost::python::pointee<boost::python::init<const boost::shared_ptr<EventManager>&> >'
/opt/local/include/boost/mpl/eval_if.hpp:38:31:   instantiated from 'boost::mpl::eval_if<boost::is_convertible<boost::python::init<const boost::shared_ptr<EventManager>&>*, EventHandle*>, boost::mpl::i开发者_JS百科dentity<boost::python::init<const boost::shared_ptr<EventManager>&> >, boost::python::pointee<boost::python::init<const boost::shared_ptr<EventManager>&> > >'
/opt/local/include/boost/python/object/class_metadata.hpp:179:13:   instantiated from 'boost::python::objects::class_metadata<EventHandle, boost::noncopyable_::noncopyable, boost::python::init<const boost::shared_ptr<EventManager>&>, boost::python::detail::not_specified>'
/opt/local/include/boost/python/class.hpp:174:42:   instantiated from 'boost::python::class_<EventHandle, boost::noncopyable_::noncopyable, boost::python::init<const boost::shared_ptr<EventManager>&> >::id_vector'
/opt/local/include/boost/python/class.hpp:627:55:   instantiated from 'boost::python::class_<T, X1, X2, X3>::class_(const char*, const char*) [with W = EventHandle, X1 = boost::noncopyable_::noncopyable, X2 = boost::python::init<const boost::shared_ptr<EventManager>&>, X3 = boost::python::detail::not_specified]'
/Users/neil/nn/src/core/python_event.cc:21:66:   instantiated from here
/opt/local/include/boost/python/pointee.hpp:28:44: error: no type named 'element_type' in 'class boost::python::init<const boost::shared_ptr<EventManager>&>'
make[3]: *** [CMakeFiles/distributions.dir/core/python_event.cc.o] Error 1
make[2]: *** [CMakeFiles/distributions.dir/all] Error 2
make[1]: *** [CMakeFiles/distributions.dir/rule] Error 2
make: *** [distributions] Error 2


You need to expose the constructor with init<...>. For example, for a constructor taking two ints:

class_<MyClass>("MyClass", init<int, int>())
    ....

Note that you need to place the init inside the class_ parameters rather than in a separate .def() call, or Boost will assume you have a default constructor.

See the tutorial section about constructors.

Edit: For your code, try:

class_<EventHandle, noncopyable>("EventHandle", 
    init<boost::shared_ptr<EventManager> const&>())


Have you tried using no_init? The docs say this is for when you want no constructors, but maybe you can combine it with explicitly asking for one?

class_<EventHandle, noncopyable,
    init<boost::shared_ptr<EventManager> const&> >
    ("EventHandle", no_init)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜