C++ 使用boostpython&;std::共享的ptr
我正试图让boostpython与std::shared\u ptr配合使用。目前,我收到以下错误:C++ 使用boostpython&;std::共享的ptr,c++,boost,c++11,boost-python,C++,Boost,C++11,Boost Python,我正试图让boostpython与std::shared\u 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
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>
相关的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);
类(“圆”,init())
.def(“半径”和圆::半径)
.def(“设置半径”和圆::设置半径)
.def(“直径”和圆::直径)
.def(“左上角”&圆圈::左上角)
.def(“中心”和圆::中心);
//锚基类。
类(“锚”,无初始)
.def(“where”、&Anchor::where);
我使用的是Boost 1.48.0。有什么想法吗?< /P> < P>看起来像Boo::Python不支持C++ 11 STD::SyrdypTr.< /P> 如果您查看文件boost/python/converter/shared_ptr_to_python.hpp,您会发现boost::shared_ptr_to_python(shared_ptr const&x)的模板函数shared_ptr to_python的实现(它解释了为什么代码对boost::shared_ptr工作良好) 我认为你有几个选择:
- 使用boost::shared_ptr(您试图避免)
- 为std::shared_ptr(IMHO是最好的选择)将shared_ptr_的实现编写为python
- 向boost::python开发人员发送请求以支持std::shared_ptr
/*使boost::python理解std::shared\u ptr*/
名称空间提升{
模板
T*get_指针(std::shared_ptr p)
{
返回p.get();
}
}
为我工作。您可以将类定义为:
class_<foo, std::shared_ptr<foo>>("Foo", ...);
class_uuo(“Foo”,…);
这样,其他返回std::shared_ptr的方法就可以工作了
虚拟函数/多态性可能需要一些技巧,我链接到的线程中应该包含这些技巧。除非我有误解,否则我认为这可以解决您的问题:
boost::python::register_ptr_to_python<std::shared_ptr<Anchor>>();
boost::python::register_ptr_to_pythontryclass(“Anchor”,no_init)
@kassak我也试过了,同样的问题。swig文档对包装智能指针有一些解释,可能会有所帮助,但我不认为它正在开发中;那家伙要求有人接手。有一个与支持std::shard_ptr相关的开放票证。我们如何删除共享_ptr的上述声明。假设,我共享了\u ptr网络;我想在程序结束时清空网络中的数据。任何关于这方面的帮助都会很有帮助。非常感谢。
/* make boost::python understand std::shared_ptr */
namespace boost {
template<typename T>
T *get_pointer(std::shared_ptr<T> p)
{
return p.get();
}
}
class_<foo, std::shared_ptr<foo>>("Foo", ...);
boost::python::register_ptr_to_python<std::shared_ptr<Anchor>>();