【发布时间】:2012-03-03 18:36:41
【问题描述】:
名为test 的函数将std::function<> 作为其参数。
template<typename R, typename ...A>
void test(std::function<R(A...)> f)
{
// ...
}
但是,如果我执行以下操作:
void foo(int n) { /* ... */ }
// ...
test(foo);
编译器(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++ c++11 templates variadic-templates std-function