I'm writing code with parameter packs and std::function. The goal is to be able to pass a function and a pack of parameters into a function, and be able to call the function of that pack (and do some other work). Here is the stripped down example of what I want:
#include <functional>
#include <iostream>
template<typename... Args>
using MyFunc = std::function< void(Args...) >;
template<typename... Args>
void call(MyFunc<Args...> f, Args... args) {
f(args...);
}
int main() {
auto lambda = [](int a, double b) {std::cout << a << b << std::endl;};
call<int, double>(lambda, 1, 2.0);
// but this works?
//call(MyFunc<int, double>(lambda), 1, 2.0);
return 0;
}
When I compile this with clang 13.0 I get: could not match 'function<void (int, double, type-parameter-0-0...)>' against '(lambda at pack.cpp:13:19)'.
For g++ 7.5 I get a similar: ‘main()::<lambda(int, double)>’ is not derived from ‘std::function<void(Args ...)>’.
As you can see, I commented an example that works: explicitly converting the argument to std::function. But why can't it do it implicitly?
P.S. I can't use a templated F instead of std::function for a reason that is out of scope of this simple example.