从不可复制的实例创建 boost::python::object

发布于 2024-12-10 15:35:48 字数 928 浏览 0 评论 0原文

我想知道这里是否有人能够帮助我解决我遇到的以下问题。

我似乎无法从绑定到不可复制的 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 技术交流群。

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

发布评论

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

评论(1

慕巷 2024-12-17 15:35:48

用这个

boost::python::object obj(**boost::cref(a)**);

Use this

boost::python::object obj(**boost::cref(a)**);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文