-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Enable enable_shared_from_this
support for casts
#1192
base: master
Are you sure you want to change the base?
Conversation
Currently we support `enable_shared_from_this` only if we end up taking over an existing reference or pointer; this extends the support by looking for `std::enable_shared_shared_from_this` inheritance for any type of cast except for an explicit `return_value_policy::copy`. This lets pybind avoid a copy when doing something like: m.def("f", []() { auto b = std::make_shared<B>(); foo(*b); } where `foo` is some function that takes the `B` by lvalue reference, but ends up requiring casting it to Python. (In the motivating example for this PR the specific case was a trampoline overload). Cc: @florianwechsung for input.
Is it possible to do the check if
This is my wrapping code:
This will introduce the unwanted copy construction, as Could the check be done at runtime instead? |
I don't see how a runtime check is possible practically (at least without a ridiculous amount of overhead)--we have a structure:
with no way to determine, from a The current approach works by deducing The only way I could practically see to do this would be to have some |
Ah I see, yeah that makes sense and is much more involved. I'll add |
It appears that this breaks the ability to use a nullptr as default for
This gives a segfault when I |
I think c60a114 should fix that. |
Yep it does! Thanks |
Currently we support
enable_shared_from_this
only if we end up taking over an existing reference or pointer; this extends the support by looking forstd::enable_shared_shared_from_this
inheritance for any type of cast except for an explicitreturn_value_policy::copy
.This lets pybind avoid a copy when doing something like:
where
foo
is some function that takes theB
by lvalue reference, but ends up requiring casting it to Python. (In the motivating example for this PR the specific case was a trampoline overload).The .so impact is minimal (+8192 bytes including the added test code) as the compiler should be able to optimize away the check entirely for non-shared-from-this classes.
Cc: @florianwechsung for input.