python - 使用 pybind11 包装 C++ void 函数

标签 python c++ pybind11

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

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

pybind11 绑定(bind)如下所示:

.def("somefunction", &somefunction)

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

我接下来的问题是如何编写Python C++ 函数的绑定(bind),修改通过引用传递的 STL vector 的内容并返回 void .

最佳答案

pybind11/STL.h 结果包含到 c++ 和 pyhton 之间的复制转换,请参阅 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 部分

关于python - 使用 pybind11 包装 C++ void 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49919951/

相关文章:

python - Tkinter 导致 Jupyter 内核崩溃?

python - Gekko 整数优化问题

c++ - 模板参数的格式规范

c++ - 从 QR 对象获取 R 矩阵

c++ - 用于生成 pybind11 绑定(bind)的模板元函数

python - pybind11 模块目录

python - TensorFlow - 如何使用每个示例一次且仅一次评估所有测试集

python - 设置 virtualenv 时不能使用 apt get。即使在检查所有者权限之后

c++ - 如何在 C++ 中初始化一个空的全局 vector

python - 如何在 pybind11 中将 python 函数转换/转换为 std::function<double(double*)> ?