1

I have a C++11 lambda function and it needs to be realised without C++0x. I tried it with boost.lambda without success so far.

The lambda function captures 4 variables and takes 2 as parameters. In the body of the lambda function are several if/else, cases and around 100 lines of code.

  • Is this even possible with boost.lambda/ how can this be realised?
  • Would be glad if you could point out a library/give an example.

Hope I wrote all information you need.

€: I got it running,thanks. A little follow up question:

  • is there also a way to give the Obj.operater()(); to another function as a callbackfunction/function pointer?

My first try was like this:

    Lambda Obj( C, D);
    command ( Obj.operator()(typeA A, typeB B));

Thanks

joey
  • 21
  • 3
  • 2
    Use predicates. I don't think lambdas in C++0x was disigned to have 100+ line of code. –  Aug 29 '14 at 08:14
  • 2
    SURELY if your lambda function is 100 lines long, it's NOT intended to be a lambda function in the first place? Lambda's are great when they fit in one or two lines, but beyond that, you want real functions that can be used to set breakpoints in, etc. – Mats Petersson Aug 29 '14 at 08:16
  • 3
    write your own functor struct only have ~15 lines of overhead – Bryan Chen Aug 29 '14 at 08:16
  • @MatsPetersson break it to small pieces is good idea. but it have nothing to do with breakpoint and make it a real function. it should really be a functor class/struct – Bryan Chen Aug 29 '14 at 08:17
  • Thanks again for the fast help. I will try to implement a function object and comment how it worked later. Not sure if here is the right place to answer. – joey Aug 29 '14 at 10:06
  • @joey The StackOverflow way to give feedback "this worked" is to select the answer which solved the problem (or helped you the most) and *Accept* it by clicking the green tick mark next to it (at most one accepted answer per question). This gives the answerer and you some rep, and marks the question as resolved. See details in the [help center](http://stackoverflow.com/help/accepted-answer). – Angew is no longer proud of SO Aug 29 '14 at 11:49
  • @joey And the way to give feedback "this didn't quite work" is to comment on the relevant answer, if it's a problem/unclarity in the answer itself. If that comment would be "this works, but now I have a different (unrelated) problem," then it's not appropriate and a new question should be asked instead. SO works best in one-problem-per-question mode. – Angew is no longer proud of SO Aug 29 '14 at 11:51

3 Answers3

6

This is not what Boost.Lambda is designed for. Your best bet is to turn this into a normal function object.

Simply transform this:

[a, &b, c, &d](args) -> ReturnValue { body; }

Into this:

class Lambda
{
  Type_of_a a;
  Type_of_b &b;
  Type_of_c c;
  Type_of_d &d;

public:
  Lambda(Type_of_a a, Type_of_b &b, Type_of_c c, Type_of_d &d) : a(a), b(b), c(c), d(d) {}

  ReturnValue operator() (args) const
  { body; }
};

And create an instance of the Lambda class where you create the original lambda expression.

You can put the definition of Lambda in an unnamed namespace, to simulate the lambda's unnamed type also.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
3

If you're stuck with C++03/C++98 you're better off with creating a functor instead of trying to make Boost.Lambda work with your 100 lines of code. A C++11 lambda is mostly syntactic sugar for creating a functor anyway.

You can easily translate the following lambda:

const auto lambda = [c1, c2, c3, c4](A1 a1, A2 a2) -> RetVal { /*somecode*/ };

Into the following functor:

namespace
{
  struct Functor
  {
    Cap1 c1; Cap2 c2; Cap3 c3; Cap4 c4;

    Functor(Cap1 c1, Cap2 c2, Cap3 c3, Cap4 c4)
    : c1(c1), c2(c2), c3(c3), c4(c4) {}

    RetVal operator()(A1 a1, A2 a2) const
    {
      /*somecode*/
    }
  };
}
dalle
  • 18,057
  • 5
  • 57
  • 81
0

Live demo link:

#include <iostream>
#include <functional>

template <typename Arg1, typename Arg2, typename Cap1, typename Cap2>
struct my_lambda : std::binary_function<Arg1, Arg2, void>
{
    my_lambda(const Cap1& c1, const Cap2& c2) : cap1(c1), cap2(c2) {}

    void operator()(const Arg1& arg1, const Arg2& arg2) const
    {
        // lambda body
        std::cout << (cap1 + cap2 + arg1 + arg2) << std::endl;
    }

    mutable Cap1 cap1;
    mutable Cap2 cap2;
};

template <typename Arg1, typename Arg2, typename Cap1, typename Cap2>
my_lambda<Arg1, Arg2, Cap1, Cap2> lambda(const Cap1& c1, const Cap2& c2)
{
    return my_lambda<Arg1, Arg2, Cap1, Cap2>(c1, c2);
}

int main()
{
    int i = 1, j = 2;
    lambda<int, int>(i, j)(3, 4);

    // same as:
    // [i, j] (int a, int b)
    // {
    //     std::cout << (i + j + a + b) << std::endl;
    // } (3, 4);
}
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160