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, ) }
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, ) }
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, ) }
In C #, I could call p => ChangeVal (p). What would be the syntax for this with C ++ lambda expressions?
c ++ boost anonymous-function
Steve
source share