Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a lambda function to match a boost::function parameter without using C++0x?

Tags:

c++

stl

boost

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.

like image 943
Peter McG Avatar asked Jul 08 '11 13:07

Peter McG


People also ask

How do you write a lambda function in C++?

Creating a Lambda Expression in C++auto greet = []() { // lambda function body }; Here, [] is called the lambda introducer which denotes the start of the lambda expression. () is called the parameter list which is similar to the () operator of a normal function.

Can lambdas be templated?

Lambda-expressions are not allowed in unevaluated expressions, template arguments, alias declarations, typedef declarations, and anywhere in a function (or function template) declaration except the function body and the function's default arguments.

Can you template Lambda C++?

Lambdas support with C++20 template parameters, can be default-constructed and support copy-assignment, when they have no state, and can be used in unevaluated contexts. Additionally, they detect when you implicitly copy the this pointer.

Why are lambda functions used?

Lambda functions are intended as a shorthand for defining functions that can come in handy to write concise code without wasting multiple lines defining a function. They are also known as anonymous functions, since they do not have a name unless assigned one.


1 Answers

#include <functional>    // STL
#include <boost/lambda/lambda.hpp>   // Boost.Lambda
#include <boost/spirit/include/phoenix_core.hpp>     // Boost.Pheonix
#include <boost/spirit/include/phoenix_operator.hpp> // Boost.Pheonix also

...

// Use STL bind without lambdas
F(0, std::bind2nd(std::equal_to<int>(), 0));
F(123, std::bind2nd(std::equal_to<int>(), 0));

// Use Boost.Lambda (boost::lambda::_1 is the variable)
F(0, boost::lambda::_1 == 0);
F(123, boost::lambda::_1 == 0);

// Use Boost.Phoenix
F(0, boost::phoenix::arg_names::arg1 == 0);
F(123, boost::phoenix::arg_names::arg1 == 0);

You may want to add some using namespace to 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.

like image 53
kennytm Avatar answered Sep 27 '22 17:09

kennytm