1

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

So if I understood the docs correctly, I should make use of the PYBIND11_MAKE_OPAQUE() macro and a corresponding class_ declaration? What do I then pass to somefunction in Python?
You should pass a python counterpart of your std::vector<int>, the VectorOfInt. Something like a = VectorOfInt([1,2]); b = VectorOfInt([1,2]); somefunction(a, b) I believe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.