简体   繁体   中英

Wrapping C++ void functions with pybind11

I have a C++ class containing void functions which take STL vectors passed by reference and modify the contents of one of those vectors. For example:

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

And the pybind11 binding looks like this:

.def("somefunction", &somefunction)

I have included the 'pybind11/stl.h' header file which handles the automatic conversion between STL containers and python equivalents and in Python I call somefunction with Python lists but I'm not sure if the C++ layer can modify the result Python list.

My then question is how to write Python bindings for C++ functions which modify the contents of an STL vector passed by reference and return void .

Including pybind11/stl.h results to copy-conversions between c++ and pyhton, see http://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#automatic-conversion

To pass by reference see section http://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#making-opaque-types

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM