简体   繁体   English

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

[英]Exporting non-default-constructible classes with Boost.Python

How do I export a non-default-constructible class with Boost.Python?如何使用 Boost.Python 导出非默认构造的 class?

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

You need to expose the constructor with init<...> .您需要使用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.请注意,您需要将init放在class_参数中,而不是放在单独的.def()调用中,否则 Boost 会假设您有一个默认构造函数。

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_ini t?您是否尝试过使用no_ini t? 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建非默认可构造类的虚拟对象 - Create dummy object of non-default-constructible class 使用非默认可构造类型填充std :: array(无可变参数模板) - Populate std::array with non-default-constructible type (no variadic templates) 如何正确初始化非默认可构造的类成员? - How to properly initialize non-default-constructible class member? 如何初始化不可默认构造的不可复制对象的元组? - How to initialize a tuple of non-default-constructible not-copyable objects? 移动具有非默认构造 class 保护的构造函数 - Move constructor with protection for non-default-constructible class 如何创建C ++ 11不可默认构造的分配器? - How to create a C++ 11 non-default-constructible allocator? 初始化非默认可构造元素的 std::array? - Initializing an std::array of non-default-constructible elements? std::map emplace 不可移动不可复制非默认可构造类型 - std::map emplace non-movable non-copyable non-default-constructible type 如何初始化std :: array <T, 2> 其中T是不可复制的,非默认可构造的? - How to initialize an std::array<T, 2> where T is non-copyable and non-default-constructible? 如何使用 C++ 11 样式的非默认构造分配器指定初始大小? - How to specify an initial size using C++ 11 style non-default-constructible allocator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM