Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass std::vector without copy #12

Open
petrasvestartas opened this issue Feb 26, 2023 · 4 comments
Open

Pass std::vector without copy #12

petrasvestartas opened this issue Feb 26, 2023 · 4 comments

Comments

@petrasvestartas
Copy link

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
Copy link
Owner

tdegeus commented Feb 27, 2023

@petrasvestartas
Copy link
Author

petrasvestartas commented Feb 28, 2023

Dear @tdegeus ,

By returning the output, doesn't it copy the data to a new vector?

@tdegeus
Copy link
Owner

tdegeus commented Feb 28, 2023

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
Copy link
Author

petrasvestartas commented Mar 3, 2023

The second.
I think the only way to do this is by using opaque option from pybind11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants