C++ boost::python函数调用中boost::shared_ptr的转换
考虑以下示例:C++ boost::python函数调用中boost::shared_ptr的转换,c++,python,boost,shared-ptr,boost-python,C++,Python,Boost,Shared Ptr,Boost Python,考虑以下示例: #include "Python.h" #include <boost/python.hpp> #include <boost/shared_ptr.hpp> class A {}; class B : public A{}; void foo(boost::shared_ptr<A>& aptr) { } BOOST_PYTHON_MODULE(mypy) { using namespace boost::python;
#include "Python.h"
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
class A {};
class B : public A{};
void foo(boost::shared_ptr<A>& aptr) { }
BOOST_PYTHON_MODULE(mypy)
{
using namespace boost::python;
class_<A, boost::shared_ptr<A> >("A", init<>());
class_<B, boost::shared_ptr<B>, bases<A> >("B", init<>());
def("foo", foo);
}
我明白了
ArgumentError: Python argument types in
mypy.foo(B)
did not match C++ signature:
foo(boost::shared_ptr<A> {lvalue})
ArgumentError:中的Python参数类型
米皮·福(B)
与C++签名不匹配:
foo(boost::shared_ptr{lvalue})
我在谷歌上搜索了很多,但是我找不到一个很好的解释/修复/解决方法。欢迎任何帮助 问题在于,您请求的是对
共享\u ptr
的非常量引用,而Python中的b
实例根本不包含一个;它包含一个共享的\u ptr
。虽然shared_ptr
可以隐式转换为shared_ptr
,shared_ptr&
不能隐式转换为shared_ptr&
如果您可以修改foo
以获取shared\u ptr
,或shared\u ptr const&
,这将解决您的问题
如果没有,您还需要包装一个接受
shared\u ptr&
的版本,如果使用foo
获取boost::python::object
参数,然后从中提取shared\u ptr
?当然可以,但我希望有一种方法不为foo编写任何额外的包装器(我需要的东西很多)
ArgumentError: Python argument types in
mypy.foo(B)
did not match C++ signature:
foo(boost::shared_ptr<A> {lvalue})