一个名为teststd :: function <>的函数作为参数.
template<typename R, typename ...A>
void test(std::function<R(A...)> f)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我执行以下操作:
void foo(int n) { /* ... */ }
// ...
test(foo);
Run Code Online (Sandbox Code Playgroud)
编译器(gcc 4.6.1)说no matching function for call to test(void (&)(int)).
要使最后一行test(foo)编译并正常工作,我该如何修改该test()函数?在test()函数中,我需要f类型为std :: function <>.
我的意思是,是否有任何模板技巧让编译器确定函数的签名(foo在示例中),并将其std::function<void(int)>自动转换?
我想为lambdas(无论是声明的还是无状态的)做这项工作.