从不可复制的实例创建 boost::python::object
我想知道这里是否有人能够帮助我解决我遇到的以下问题。
我似乎无法从绑定到不可复制的 python 的 c++ 类创建 boost::python::object 。这是一个简化的例子。
#include <boost/python.hpp>
class A
{
public:
static A*
create() {return new A;}
protected:
A(){}
};
void
doSomething(const A& a)
{
boost::python::object obj(a);
}
BOOST_PYTHON_MODULE(test)
{
boost::python::class_<A, boost::noncopyable>("A", boost::python::no_init)
.def("__init__", boost::python::make_constructor(&A::create));
boost::python::def("doSomething", &doSomething);
}
然后在 python 中运行时
import test
a = test.A()
test.doSomething(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: A
我意识到 boost::noncopyable 参数阻止了 A 的 to_python 转换器被注册。有谁知道我如何从 A 实例创建 boost::python::object ?
提前致谢!
I was wondering if any one here might be able to help me out with the following problem I'm having.
I seem unable to create a boost::python::object from a c++ class I've bound to python that is noncopyable. Here is a simplified example..
#include <boost/python.hpp>
class A
{
public:
static A*
create() {return new A;}
protected:
A(){}
};
void
doSomething(const A& a)
{
boost::python::object obj(a);
}
BOOST_PYTHON_MODULE(test)
{
boost::python::class_<A, boost::noncopyable>("A", boost::python::no_init)
.def("__init__", boost::python::make_constructor(&A::create));
boost::python::def("doSomething", &doSomething);
}
Then at runtime in python
import test
a = test.A()
test.doSomething(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: A
I realise that the boost::noncopyable parameter prevents a to_python converter for A being registered. Does anyone know how I might be able to create a boost::python::object from an A instance ?
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用这个
Use this