问题描述
在我无法修改的 cpp 代码中,我们有自定义类和自定义指针。 这个类读取一个对象,并且根据 kwargs 也可能写入它。 我需要为它制作 pybind11 绑定。
根据传递的参数,我们得到指向类的常量或非常量指针。
Pybind 抱怨 lambda 返回类型的类型不一致。
制作仅适用于常量或非常量对象的 init()
函数一切顺利。不幸的是,需要支持这两种情况。
在这种情况下,将 python 绑定到 cpp 代码的最佳方法是什么?
In lambda function:
error: inconsistent types 'std::shared_ptr<myClass>' and 'std::shared_ptr<const myClass>'
deduced for lambda return type
return *const_p;
如何修改下面的代码以支持这两种情况? 使用 Cpp 11 编译器。
// those defined elsewhere
typedef std::shared_ptr< myClass > my_ptr;
typedef std::shared_ptr< const myClass > const_my_ptr;
//init function for pybind11
.def(py::init([](py::kwargs kwargs) {
bool object_writable = py::bool_(kwargs["rw"]);
int cache = py::bool_(kwargs["cache"]);
std::string path = py::str(kwargs["path"]);
if (object_writable){
//returns non const
my_ptr p = myClass::read_write(path)
return *p;
}
else{
//returns const
const_my_ptr const_p = myClass::read(path,cache)
return *const_p;
}
}))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)