【发布时间】:2011-07-08 13:07:29
【问题描述】:
如何使用 boost 或 stl 创建一个 lambda 函数,以匹配 F 在 main 的第三个 sn-p 代码中预期的 boost::function 参数?
#include <iostream>
#include <boost/function.hpp>
void F(int a, boost::function<bool(int)> f) {
std::cout << "a = " << a << " f(a) = " << f(a) << std::endl;
}
bool G(int x) {
return x == 0;
}
int main(int arg, char** argv) {
// C++0x
F(123, [](int i) { return i==0; } );
// Using seperate function
F(0, &G);
// How can I do it in place without C++0x
F(123, /* create a lambda here to match */);
}
我不能使用 C++0x,并且想避免创建几个单独的函数。如果有帮助,我可以使用 boost::function 以外的其他东西,我的首要任务是简洁地创建 lambda。
【问题讨论】:
-
我现在想知道......为什么你不能使用 C++0x 但你想使用
std::function这是 C++0x 的一部分?顺便说一句,看看 boost lambda 库,因为它可能是您正在寻找的。span> -
@David: boost::function 不是 std::function