gpt4 book ai didi

c++ - 使用 Boost Python & std::shared_ptr

转载 作者:IT老高 更新时间:2023-10-28 22:26:11 26 4
gpt4 key购买 nike

我正在尝试让 Boost Python 与 std::shared_ptr 很好地配合使用。目前,我收到此错误:

Traceback (most recent call last):
File "test.py", line 13, in <module>
comp.place_annotation(circle.centre())
TypeError: No to_python (by-value) converter found for C++ type: std::shared_ptr<cgl::Anchor>

来自调用 circle.centre(),它返回一个 std::shared_ptr。我可以将每个 std::shared_ptr 更改为 boost::shared_ptr(Boost Python 可以很好地使用它)但是要更改的代码量相当可观,我想使用标准库。

circle 方法声明如下:

const std::shared_ptr<Anchor> centre() const
{
return Centre;
}

这样的 anchor 类:

class Anchor
{
Point Where;
Annotation* Parent;
public:

Anchor(Annotation* parent) :
Parent(parent)
{
// Do nothing.
}

void update(const Renderer& renderer)
{
if(Parent)
{
Parent->update(renderer);
}
}

void set(Point point)
{
Where = point;
}

Point where() const
{
return Where;
}
};

而相关的Boost Python代码是:

class_<Circle, bases<Annotation> >("Circle", init<float>())
.def("radius", &Circle::radius)
.def("set_radius", &Circle::set_radius)
.def("diameter", &Circle::diameter)
.def("top_left", &Circle::top_left)
.def("centre", &Circle::centre);

// The anchor base class.
class_<Anchor, boost::noncopyable>("Anchor", no_init)
.def("where", &Anchor::where);

我使用的是 Boost 1.48.0。有什么想法吗?

最佳答案

看起来 boost::python 不支持 C++ 11 std::shared_ptr。

如果您查看文件 boost/python/converter/shared_ptr_to_python.hpp,您会发现 boost::shared_ptr 的模板函数 shared_ptr_to_python(shared_ptr const& x) 的实现(它解释了为什么代码适用于boost::shared_ptr)。

我认为你有几个选择:

  • 使用 boost::shared_ptr(你试图避免)
  • 为 std::shared_ptr 编写 shared_ptr_to_python 的实现(恕我直言,最好的选择)
  • 向 boost::python 开发者发送请求以支持 std::shared_ptr

关于c++ - 使用 Boost Python & std::shared_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13986581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com