How do I create a lambda function using boost or the stl to match the boost::function parameter expected by F in the third snippet of code in main?
#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 */);
}
I can’t use C++0x and would like to avoid creating several separate functions. I can use something other that boost::function if that helps, my priority is creating the lambda succinctly.
You may want to add some
using namespaceto simplify the code.Boost.Lambda is strictly for defining functors inline with a C++-like syntax, while Boost.Phoenix is a functional-programming language built on top of C++ abusing (☺) its syntax and compile-time computation capability. Boost.Phoenix is much more powerful than Boost.Lambda, but the former also takes much more time to compile.