我已经看过所有我能找到的相关问题,并且无法想出对这种特定情况的答案。boost :: python纯静态工厂构造函数和std :: unique_ptr的虚拟基类
我有一个C++纯虚拟基类接口,我想公开给Python。实现从基地继承和未暴露:
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的子类化基地,只能够通过create()
工厂函数来构造新的基本情况。
我试过两种方法来包装boost :: python。首先,类似于Boost.Python: How to expose std::unique_ptr我试图创建一个__init__
功能释放的unique_ptr:
namespace bp = boost::python;
bp::class_<Base, boost::noncopyable>("Base", bp::no_init)
.def("__init__",
bp::make_function([](Base& self) { return Master::create().release(); },
bp::return_value_policy<bp::manage_new_object>(),
boost::mpl::vector<Base*, Base&>(), "Create a new Base")
.staticmethod("__init__")
这编译并在Python加载,但__init__
实际上不是静态的!
create(...)
create((Base)arg1) -> Base :
Create a new Base
C++ signature :
Base* create(Base {lvalue})
第二届方法我试过是使用make_constructor
如下:
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>>();
然而,这并不编译: /usr/include/boost/python/make_constructor.hpp:40:20: fatal error: call to deleted constructor of std::unique_ptr<Base, std::default_delete<Base>> dispatch(x, is_pointer<T>());