Is it possible to have anonymous C ++ functions with a boost?

I am trying to solve the problem that anonymous functions make it a lot easier, and I was wondering if this is possible in C ++.

What I would like to do (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 should do. In C #, I would do p => p * 5. I know this is easy with C ++ 0x, but I can't use it. I feel I should be able to do this using boost :: lambda or merging boost :: bind and boost :: function with placeholders, but I can't get it to work. This may not be possible, and this is also normal, but please answer if this is not possible. Thanks.

EDIT: Well, it seems like a simple int example is fine, but what about a more complex structure? So let's 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 will be along the lines p => p.temp * 5. Is it possible to do this in C ++ with boost?

EDIT 2: OK, now I'm just wondering: D How can I name a function in a 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 be the syntax for this with C ++ lambda expressions?

+6
c ++ boost anonymous-function
source share
3 answers

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

For a simple case, such as p => p * 5 , which you mention in your question, it seems to me that using Lambda or Bind would be reasonable:

 DoSomething(1, _1 * 5); 

Edit: Your second example falls into one area where the syntax becomes quickly verbose: access to a member (data or function). Since the dot operator cannot be overloaded in C ++, you need to use the bind expression to get the "x" from the argument:

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

or, using Boost.Lambda, use the overloaded β†’ * operator:

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

Edit 2: Ok, for the last time :) In the last question you write that in C # you write p => ChangeVal(p) , but the code above shows that ChangeVal accepts int, not NumHolder, so it doesn’t understand what do you mean.

Assuming ChangeVal accepts int and that you want the anonymous function to ChangeVal(the_arg.x) equivalent ChangeVal(the_arg.x) , you should write this using Boost.Lambda:

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

or using Boost.Bind (also works with Lambda):

 DoSomething(1, bind(&ChangeVal, bind(&NumHolder::x, _1)); 
+5
source share

No, this cannot be done in a simple way. boost :: lambda may help, but in my opinion the code is so hard to read when using, so I would avoid that.

I think the C # p=>p*5 equivalent will be _1*5 , but I just looked at it briefly, so I'm not sure. For simple things, this works, but as soon as you need control structures, you will have to use a different set of control structures that are functionally based and not imperative. I found it so different from regular C ++ code that I decided for myself that it should not be used because it makes the code so difficult to read for others.

+2
source share

boost does not extend C ++ syntax. there are no anonymous functions in C ++.

0
source share

All Articles