Pass std::vector without copy
Opened this issue · 4 comments
petrasvestartas commented
Hi,
In your examples of std::vector, data is copied.
Is there any example that shows how to bind vectors by reference instead of copy?
tdegeus commented
This example passes a list by reference : https://github.com/tdegeus/pybind11_examples/blob/master/01_py-list_cpp-vector/example.cpp
petrasvestartas commented
Dear @tdegeus ,
By returning the output, doesn't it copy the data to a new vector?
tdegeus commented
Ah @petrasvestartas . Ah yeah you are right, I did not look further than the input as I assumed your question to be about that. What is your question then actually about:
- Do you want to modify inplace? In that case you can just remove the
const
from the input and remove the output. This could be a nice example to add indeed. It would be great if you could open a PR. - Do you want to return a reference to data that is owned by an object that is kept alive? In that case you can use
std::vector<double>&
as return object. Same here, it could be nice to add on example.
Or is your question about yet something else?
petrasvestartas commented
The second.
I think the only way to do this is by using opaque option from pybind11.