用 pybind11 包装C++ void 函数

Wrapping C++ void functions with pybind11

本文关键字:void 函数 C++ 包装 pybind11      更新时间:2023-10-16

我有一个包含 void 函数的C++类,这些函数采用通过引用传递的 STL 向量并修改其中一个向量的内容。例如:

void somefunction(std::vector<double> &result, std::vector<double> &array1, std::vector<double> &array2) {some calculations}

pybind11 绑定如下所示:

.def("somefunction", &somefunction)

我已经包含了"pybind11/stl.h"头文件,它处理STL容器和python等价物之间的自动转换,在Python中,我用Python列表调用somefunction,但我不确定C++层是否可以修改resultPython列表。

我当时的问题是如何编写Python C++函数的绑定,这些函数修改通过引用传递的 STL 向量的内容并返回void

包括 c++ 和 pyhton 之间复制转换pybind11/stl.h结果,请参阅 http://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#automatic-conversion

要通过引用,请参阅第 http://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#making-opaque-types 节