带有静态工厂构造函数和std::unique\u ptr的python纯虚拟基类
我已经看过了所有我能找到的相关问题,但没有找到这个具体情况的答案带有静态工厂构造函数和std::unique\u ptr的python纯虚拟基类,python,c++,boost,pure-virtual,Python,C++,Boost,Pure Virtual,我已经看过了所有我能找到的相关问题,但没有找到这个具体情况的答案 我有一个C++纯虚拟基类接口,我想把它暴露给Python。该实现继承自Base,且未公开: struct Base : private boost::noncopyable { static std::unique_ptr<Base> create(); virtual ~Base(); virtual int get_int() = 0; virtual void set_int(co
我有一个C++纯虚拟基类接口,我想把它暴露给Python。该实现继承自Base,且未公开:
struct Base : private boost::noncopyable
{
static std::unique_ptr<Base> create();
virtual ~Base();
virtual int get_int() = 0;
virtual void set_int(const int i) = 0;
};
这是用Python编译和加载的,但是\uuuu init\uuu
实际上不是静态的
create(...)
create( (Base)arg1) -> Base :
Create a new Base
C++ signature :
Base* create(Base {lvalue})
我尝试的第二种方法是使用make\u构造函数
,如下所示:
namespace bp = boost::python;
bp::class_<Base, boost::noncopyable, std::unique_ptr<Base>>("Base", bp::no_init)
.def("__init__", bp::make_constructor(&Base::create));
bp::register_ptr_to_python<std::unique_ptr<Master>>();
namespace-bp=boost::python;
bp::class_u(“基”,bp::no_uinit)
.def(“\uuuu init\uuuuu”,bp::make\u构造函数(&Base::create));
bp::将_ptr_注册到_python();
但是,这不会编译:
/usr/include/boost/python/make_constructor.hpp:40:20:致命错误:调用std::unique_ptr dispatch(x,is_pointer())的已删除构造函数代码>我能够使用第二种方法使绑定工作,方法是从std::unique\u ptr切换到std::shared\u ptr,因为shared\u ptr是默认可构造的
这是可能的,因为我能够修改C++库源。< /P>
namespace bp = boost::python;
bp::class_<Base, boost::noncopyable, std::unique_ptr<Base>>("Base", bp::no_init)
.def("__init__", bp::make_constructor(&Base::create));
bp::register_ptr_to_python<std::unique_ptr<Master>>();