I'm trying to make a function which takes any function returning void and generating a function returning bool by simply calling the original function and returning false.
template<typename Arg>
auto voidToBoolTemplate(std::function<void(Arg)> func)
{
return [&](Arg arg)
{
func(arg);
return false;
};
}
However, I'm having trouble passing lambdas to this function.
auto lambda = [](int x)
{
std::cout << x << '\n';
};
voidToBoolTemplate(lambda);
This is the error I get with clang 3.7.0:
Error(s):
source_file.cpp:32:5: error: no matching function for call to 'voidToBoolTemplate'
voidToBoolTemplate(lambda);
^~~~~~~~~~~~~~~~~~
source_file.cpp:5:6: note: candidate template ignored: could not match 'function<void (type-parameter-0-0)>' against '(lambda at source_file.cpp:25:19)'
auto voidToBoolTemplate(std::function<void(Arg)> func)
Assigning the lambda to std::function and then calling the function works.
auto lambda = [](int x)
{
std::cout << x << '\n';
};
std::function<void(int)> foo = lambda;
voidToBoolTemplate(foo);
In addition, if I use a specific type for the argument I can pass a lambda:
auto voidToBoolNonTemplate(std::function<void(int)> func)
{
return [&](int arg)
{
func(arg);
return false;
};
}
auto lambda = [](int x)
{
std::cout << x << '\n';
};
voidToBoolNonTemplate(lambda);
Can anyone explain this behavior?
A live demo: http://rextester.com/COKT94318