相关疑难解决方法(0)

C++ 11 variadic std :: function参数

一个名为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(无论是声明的还是无状态的)做这项工作.

c++ templates variadic-templates c++11

19
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1