-
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
cast pointer to std::tuple and std::pair #2334
Conversation
I'm still a little bit in doubt on whether we actually want this. After all, On the other hand, consistency with the other casters might not be bad... |
If the existing C++ code returns a pointer, converting it to std::optional in the bindings is more involved (and requires C++17) than just writing |
On the account of "principle of least surprise" (alternatively WTF/s), I think we do want this. Currently these casters seem inconsistent with the rest. |
Fun plot twist. This also doesn't compile: m.def("f", [](std::pair<int, double> *p){ std::cout << p->first << ", " << p->second << std::endl; }); So does it make sense to allow returning Apart from that, I would agree that this should be more similar, yes. |
Actually. Let me answer my own question: No, it doesn't. But it's already like that for the other types as well, so there's no inconsistency between different casters. EDIT: I meant you can accept pointers for the other casters, but |
I implemented only the C++ -> Python conversion, because I don't know how to do the Python -> C++ part. |
We're struggling ourselves with that: see e.g. #2245 Can I ask: since you want to be able to return pointers to things that will get copied anyway. How do you see accepting non-const references and pointers to |
@YannickJadoul I think it's useful to get automatic bindings even in case of non-const references or pointers, but I'm not the best person to discuss it. |
Thanks; any bit of input is interesting. And I see your concern. Anyhow, thanks! :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then yes, why not get this in. At least it's a bit more consistency, until other caster issues get fixed!
Does the following shorter approach not work?
|
Ooops, scratch that previous comment. LGTM. |
Thanks, @wojdyr! :-) |
(Quickly still cross-referencing to #1666, which was already closed) |
This PR copies
handle cast(T *src, ...)
from PYBIND11_TYPE_CASTER to tuple_caster.It enables bindings to functions returning a pointer to std::tuple or std::pair - in the same way as pybind11 handles now pointers to int, std::string, std::array, and everything else.
I don't have a deep understanding of pybind11 internals - please double check this patch before applying.