函数名为test
以std :: function <>作为其参数。C++ 11 variadic std ::函数参数
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)>
?
编辑
我想使这个工作lambda表达式(两人均表示,无国籍)为好。
什么lambda表达式(两人均表示,无国籍)? – 2012-02-11 17:25:27
@丹尼尔,你运气不好。或者让'test'接受任何东西('T')。无论如何,std :: function'不会完全拒绝不兼容的函数对象,所以限制函数模板参数类型的目标似乎对我来说并不是太有用。 – 2012-02-11 17:27:46
我用'(T)'试过了,但是,怎么能'std :: function'?看来我可以使用'std :: result_of <>'来获得'R',但是'A ...'? –
2012-02-11 17:33:22