-
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
[QUESTION] disambiguating lambda arguments in function overloads #3035
Comments
Hi @rfeinman, I found a similar question here: #1085 It looks like Yet there is one solution I came up with (quite ugly and not easy to extend, since it's only based on number of parameters). m.def("func",
[](py::function &f)
{
py::print(py::detail::get_function(f));
pybind11::module inspect_module = pybind11::module::import("inspect");
pybind11::object result = inspect_module.attr("signature")(f).attr("parameters");
auto num_params = pybind11::len(result);
if (num_params == 1)
{
return func_cpp(py::cast<const std::function<int(int)> >(f));
}
else if (num_params == 2)
{
return func_cpp(py::cast<const std::function<int(int, int)> >(f));
}
// else {
// py::print("Error!");
// }
}); |
Thank you @illumitata! This was super helpful |
I have a similar problem, where I try to use overload_cast with class member functions. My question is: How would you adjust @illumitata s solution for cpp class member functions? |
I have a question about writing overloaded function bindings and I could not find the answer anywhere in the docs or FAQ.
In my C++ code I have two overloaded variants of a function (call it "func"), each of which takes a single argument that is a
std::function
instance (lambda function). When I run my code in pure C++, disambiguation of the two argument types works just fine. However, when I try to write a pybind11 python module using thepy::overload_cast
tool, the disambiguation fails and I get a TypeError. Here is a simplified demonstration of the issue I'm running into. I am using C++14.C++ source:
Python source:
Python output:
Is there a way to get the desired overloading behavior with pybind11? I've tried adding type hints to the
f1
andf2
python functions, thinking perhaps this would help pybind11 disambiguate the different lambda arguments. No luck.The text was updated successfully, but these errors were encountered: