Skip to content

[QUESTION] disambiguating lambda arguments in function overloads #3035

Open
@rfeinman

Description

@rfeinman

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 the py::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:

#include <pybind11/pybind11.h>
#include <pybind11/functional.h>

namespace py = pybind11;

int func(const std::function<int(int)> &f) { return f(10); }

int func(const std::function<int(int,int)> &f) { return f(10,4); }

PYBIND11_MODULE(mymodule, m) {
    m.def("func", py::overload_cast<const std::function<int(int)> &>(&func));
    m.def("func", py::overload_cast<const std::function<int(int,int)> &>(&func));
}

Python source:

import mymodule

def f1(a):
    return 2*a

def f2(a, b):
    return 2*a + b

print(mymodule.func(f1))
print(mymodule.func(f2))

Python output:

20
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    print(mymodule.func(f2))
TypeError: f2() missing 1 required positional argument: 'b'

Is there a way to get the desired overloading behavior with pybind11? I've tried adding type hints to the f1 and f2 python functions, thinking perhaps this would help pybind11 disambiguate the different lambda arguments. No luck.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions