I have the following variadic generic lambda and overloaded foo() function defined.
template <typename Lambda>
auto bar(Lambda&& lambda) {
return [lambda = std::forward<Lambda>(lambda)](auto&& ...args) {
return lambda(std::forward<decltype(args)>(args)...);
};
}
void foo(std::function<void()>&& cmd, std::function<void()>&& callback) { std::cout << "void" << std::endl; }
void foo(std::function<bool()>&& cmd, std::function<bool()>&& callback) { std::cout << "bool" << std::endl; }
The following 3 foo() calls print "void".
int main()
{
// 1
foo(
bar( []() {} ),
bar( []() {} )
);
// 2
foo(
bar( []() { return true; } ),
bar( []() {} )
);
// 3
foo(
bar( []() {} ),
bar( []() { return true;} )
);
// 4) compiler error: foo is ambiguous
// foo(
// bar( []() { return false; } ),
// bar( []() { return true; } )
// );
}
Could you please help me to understand why it successfully compiles statements 1-3 but fails to compile statement 4?
gcc 7.5.0
barhas any purpose. Doesn't seem to make any difference for this example. I guess it would force any functor to have a placeholder return type, but I can't think of what that might do different.