gpt4 book ai didi

c++ - boost.Python 如何公开 boost::shared_ptr 的 typedef?

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:35 25 4
gpt4 key购买 nike

我有一个 C++ 类定义为:

class MyFuture {
public:
virtual bool isDone() = 0;
virtual const std::string& get() = 0;
virtual void onDone(MyCallBack& callBack) = 0;
virtual ~MyFuture() { /* empty */ }
};

typedef boost::shared_ptr<MyFuture> MyFuturePtr;

我使用 boost.python 将类公开给 Python,因为(此类从未在 Python 中创建,而是从 API 调用返回,因此返回 noncopyable):

BOOST_PYTHON_MODULE(MySDK)
{
class_<MyFuture, noncopyable>("MyFuture", no_init)
.def("isDone", &MyFuture::isDone)
.def("get", &MyFuture::get, return_value_policy<copy_const_reference>())
.def("onDone", &MyFuture::onDone)
;
}

在 python 中,我像这样使用它:

import MySDK

def main():
# more code here ...
future = session.submit()
response = future.get
print response

if __name__ == "__main__":
main()

但这会导致 Python 错误:

File "main.py", line 14, in main
future = session.submit()
TypeError: No to_python (by-value) converter found for C++ type: class boost::shared_ptr<class MySDK::MyFuture>

如何公开 typedef typedef boost::shared_ptr<MyFuture> MyFuturePtr;

更新

将使用 boost.Python 公开的类更改为:

class_<MyFuture, boost::shared_ptr<MyFuture> >("MyFuture", no_init)

导致编译错误:

boost\python\converter\as_to_python_function.hpp(21): 
error C2259: 'MySDK::MyFuture' : cannot instantiate abstract class

最佳答案

关于c++ - boost.Python 如何公开 boost::shared_ptr 的 typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118087/

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