【发布时间】:2013-04-09 14:49:07
【问题描述】:
我有一个重载函数,具有以下签名:
void Foo(const std::function<void(int )> &func);
void Foo(const std::function<void(int, int)> &func);
当我想将 Foo() 与 lambda 一起使用时,我必须这样做:
Foo((std::function<void(int )>) [] (int i ) { /* do something */ });
Foo((std::function<void(int, int)>) [] (int i, int j) { /* do something */ });
两者都不是那么用户友好。使用该函数会容易得多,而不必在 lambdas 之前添加强制转换 "(std::function<...>)" - 像这样:
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
}
请帮忙。
附言。我用的是VS2010。
【问题讨论】:
-
您使用的是什么编译器,什么版本?顺便说一句,在 clang 3.3(主干 177501)中工作正常。