使用 Boost.Python 导出非默认构造类

发布于 2024-11-04 19:31:50 字数 2649 浏览 0 评论 0原文

如何使用 Boost.Python 导出非默认构造类?

此代码:

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")

给出以下错误:

/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::identity<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

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::identity<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛清羽墨安 2024-11-11 19:31:51

您需要使用 init<...> 公开构造函数。例如,对于采用两个整数的构造函数:

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

请注意,您需要将 init 放在 class_ 参数中,而不是放在单独的 .def() 调用,否则 Boost 会假设你有一个默认的构造函数。

请参阅 关于构造函数的教程部分

编辑:
对于您的代码,请尝试:

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

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&>())
恰似旧人归 2024-11-11 19:31:51

您尝试过使用 no_init 吗?文档说这是当你不需要构造函数时使用的,但也许你可以将它与明确要求一个构造函数结合起来?

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文