views:

264

answers:

3

I'm trying to solve a problem that anonymous functions make much, much easier, and was wondering if this was possible in c++.

What I would like to do is (essentially)

template<typename T>
T DoSomething(T one, function<T(T)> dosomething)
{
    return one + dosomething(5);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

This example is very, very simplified for what I have to do. In C# I would do p => p*5. I know this is easy with C++0x, but I can't use that. I feel that I should be able to do it with either boost::lambda, or a compination of boost::bind and boost::function with placeholders, but I can't seem to get it to work. This may not be possible and thats also fine, but please answer if its not possible. Thanks.

EDIT: Ok, it seems the simple case of an int works fine, what about a more complicated structure? So, lets try

struct NumHolder
{
  int x;
}

template<typename T>
T DoSomething(T one, function<T(NumHolder)> dosomething)
{
    NumHolder temp;
    temp = 5
    return one + dosomething(temp);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

Here my C# expression would be along the lines of p => p.temp * 5. Is this possible to do in C++ with boost?

EDIT 2: OK, now I'm just curious :D How would I call a function within the lambda expression? So, if we have

int ChangeVal(int mult)
{
    return mult*5;
}

struct NumHolder
{
  int x;
}

template<typename T>
T DoSomething(T one, function<T(NumHolder)> dosomething)
{
    NumHolder temp;
    temp = 5
    return one + dosomething(temp);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

In C# I could call p => ChangeVal(p). What would the syntax be for this with the C++ lambda expressions?

+2  A: 

No, it isn't possible to do in a simple way. boost::lambda can help, but in my opinion the code is so hard to read when using it so I would avoid it.

I think the equivalent to C# p=>p*5 would be _1*5, but I've only looked at it briefly so I'm not sure. For simple stuff it works, but as soon as you need control structures you will have to use another set of control structures which are functionally based, rather than imperative. I found this so different from normal C++ code that I decided for myself that it is not worth using it, because it makes the code so hard to read for others.

Anders Abel
do you know how to do it in boost lambda?
Steve
A: 

boost doesn't extend syntax of c++. there are no anonymous functions in c++.

Andrey
I know there aren't really anon funcs in c++. I was wondering if boost had a way of faking it
Steve
+4  A: 

As Anders notes in his answer, boost::lambda can be useful, but the code can become hard to read in some cases. It thus depends on what you want to do in your anonymous function.

For simple case like the p => p * 5 you mention in your question, it seems to me that using Lambda or Bind would be reasonable, though:

DoSomething(1, _1 * 5);

Edit: Your second example hits one area where the syntax gets quickly verbose: Member (data or function) access. Because the "dot" operator can't be overloaded in C++, you have to use a bind expression to get the "x" from the argument:

DoSomething(1, bind(&NumHolder::x, _1) * 5);

or, with Boost.Lambda, use the overloaded ->* operator:

DoSomething(1, &_1->* &NumHolder::x * 5);

Edit 2: OK, one last time :) In your last question, you write that in C#, you'd write p => ChangeVal(p), but the code above shows a ChangeVal taking an int, not a NumHolder, so it's not clear what you mean.

Assuming that ChangeVal takes an int and that you want the anonymous function to do the equivalent of ChangeVal(the_arg.x), you'd write this with Boost.Lambda:

DoSomething(1, bind(&ChangeVal, &_1->*&NumHolder::x));

or this with Boost.Bind (works with Lambda too):

DoSomething(1, bind(&ChangeVal, bind(&NumHolder::x, _1));
Éric Malenfant
thanks, is it possible to use this with slightly more complicated structures? I've updated my question with an example
Steve
Is it possible to call a function from within the lambda? Question updated with an example.
Steve
the bind version of the last one works, the lambda version gives an error about converting the functor
Steve
@Steve : Do you mean: `#include <boost/lambda/bind.hpp>` `using namespace boost::lambda;` `DoSomething<int>(1, bind(`? It works for me.
Éric Malenfant