Here is the code I’m trying to run.
#include <functional>
int doFunc(std::function<void()> someFunc)
{
someFunc();
return 1;
}
int doFunc(std::function<bool()> someFunc)
{
someFunc();
return 2;
}
int main()
{
return doFunc([]() -> bool {return false;});
//return doFunc([](){});
}
When I try to call doFunc()
with a lambda that clearly returns a bool, the compiler complains:
<source>: In function 'int main()':
<source>:17:22: error: call of overloaded 'doFunc(main()::<lambda()>)' is ambiguous
17 | return doFunc([]() -> bool {return false;});
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
However, when I pass in a lambda that is clearly returns void
, it successfully prints out 1. Why can’t it identify the right version of doFunc()
?
I tried creating a function and passing that in instead of a lambda, and I got the same result. Passing in a void function was fine, but passing in a function that returns bool resulted in the same error.