函數名爲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