[英]Using Boost Python with shared_ptr<const T>
Boost Python 1.63 (python 2.7.13) works well with shared_ptr<T>
; Boost Python 1.63(python 2.7.13)与
shared_ptr<T>
; if I write this in C++: 如果我用C ++编写:
shared_ptr<Foo> create_shared_ptr() { return shared_ptr{...}; }
void accept_shared_ptr(const shared_ptr<Foo>& p) { }
...
class_<Foo, boost::noncopyable>("Foo", no_init); //Expose abstract class
register_ptr_to_python< shared_ptr<Foo> >();
def("create_shared_ptr", create_shared_ptr);
def("accept_shared_ptr", accept_shared_ptr);
Then I can write this in Python and everything works: 然后,我可以用Python编写此代码,一切正常:
accept_shared_ptr(create_shared_ptr())
The problem comes when I try and wrap shared_ptr<const Foo>
. 当我尝试包装
shared_ptr<const Foo>
时,问题就来了。 (Which I need to do because I am wrapping a library that returns this.) If I modify the C++ functions as follows: (这是我需要做的,因为我包装了一个返回该库的库。)如果按如下方式修改C ++函数:
shared_ptr<const Foo> create_shared_ptr() { return shared_ptr{...}; }
void accept_shared_ptr(const shared_ptr<const Foo>& p) { }
Then I get the error: 然后我得到错误:
Boost.Python.ArgumentError: Python argument types in mod_python.accept_shared_ptr(Foo) did not match C++ signature: accept_shared_ptr(std::shared_ptr<Foo const>)
It seems the internals are implementing conversion from python Foo
to C++ shared_ptr<Foo>
, but not to C++ shared_ptr<const Foo>
. 似乎内部人员正在实现从python
Foo
到C ++ shared_ptr<Foo>
,但没有实现到C ++ shared_ptr<const Foo>
。 Using 运用
register_ptr_to_python< shared_ptr<const Foo> >();
doesn't help. 没有帮助。 How can I fix this?
我怎样才能解决这个问题?
The issue must be in the definitions of your classes/methods. 问题必须出在您的类/方法的定义中。 This code works for me (I have boost 1.62 and python 2.7.13):
这段代码对我有用(我已经提升了1.62和python 2.7.13):
class Foo {
public:
virtual ~Foo() = default;
virtual std::string xxx() = 0;
};
class Bar: public Foo {
public:
~Bar() override = default;
std::string xxx() override { return "xxx"; }
};
std::shared_ptr<const Foo> create_shared_ptr() {
return std::shared_ptr<const Foo>(new Bar);
}
void accept_shared_ptr(const std::shared_ptr<const Foo>& p)
{
; // do nothing
}
BOOST_PYTHON_MODULE(myLib)
{
using namespace boost::python;
class_<Foo, boost::noncopyable>("Foo", no_init);
register_ptr_to_python< std::shared_ptr<const Foo> >();
def("create_shared_ptr", create_shared_ptr);
def("accept_shared_ptr", accept_shared_ptr);
}
Then, in python I can do: 然后,在python中,我可以执行以下操作:
$ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myLib
>>> ptr = myLib.create_shared_ptr()
>>> ptr
<myLib.Foo object at 0x7f8b5fde0aa0>
>>> myLib.accept_shared_ptr(ptr)
Most probably your function create_shared_ptr
somehow returns a wrong value that is misunderstood by python. 很可能您的函数
create_shared_ptr
以某种方式返回了错误的值,该值被python误解了。
Adding 添加
implicitly_convertible<std::shared_ptr<Foo>,std::shared_ptr<const Foo>>();
solves the problem. 解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.