我有一個過載功能,具有以下簽名功能:C++與lambda表達式作爲參數
void Foo(const std::function<void(int )> &func);
void Foo(const std::function<void(int, int)> &func);
當我要與lambda表達式使用美孚(),我必須做這樣的事情:
Foo((std::function<void(int )>) [] (int i ) { /* do something */ });
Foo((std::function<void(int, int)>) [] (int i, int j) { /* do something */ });
兩者都不那麼用戶友好。這將會是一個更容易使用的功能,而不具有lambda表達式之前增加鑄造「(STD ::功能< ...>)」 - 這樣的:
Foo([] (int i ) { /* do something */ }); // executes the 1st Foo()
Foo([] (int i, int j) { /* do something */ }); // executes the 2nd Foo()
所以,我需要另一個重載,接受lambda作爲它的參數,並自動將lambda轉換爲上述簽名之一。如何才能做到這一點?或者,首先有可能嗎?
template <typename Function> void Foo(Function function) {
// insert code here: should be something like
// - check the signature of the 'function'; and
// - call 'Foo()' corresponding to the signature
}
請大家幫忙。
PS。我正在使用VS2010。
你使用什麼編譯器,以及什麼版本?順便說一句,在叮噹聲3.3(幹線177501)中工作正常。 – 2013-04-09 05:41:59
我正在使用VS2010。 – hiapay 2013-04-09 06:41:56