views:

221

answers:

3

Wikipedia seems to say that C++0x will support anonymous functions. Boost also seem to support it. However I'm using .NET so if I could stick with it it would be awesome.

Basically I just want to write some quick code for objects. I have a robot which can have about 85 - 90 states. Most of the states are just "integer values passed to the robot microcontroller". So I tell the robot to go to state 35 for example.

However some states require additionnal manipulations such as user input so I'd like to keep it simple and write just a few lines of code for the differences. I've considered using derived classes but it involves a lot of code just to modify a few lines.

A: 

Anonymous functions, alternatively called Lambda Expressions or Delegates, are a language feature of C# and not part of the .NET framework. I don't think Microsoft has added anonymous functions to managed-C++, and I've found some comments which seem to agree with me.

Not to worry, though. As you mentioned, Boost.Lambda is a nifty library that you can use. What is nice is that it is implemented as templates completely in headers. So, all you have to do is include the headers. Any standards-conforming C++ compiler should support it. I understand your desire to stick with what you already have, but the effort it takes to download and use these headers should really be minimal.

If you really don't want to use Boost, then you can try using C#, but I recommend that you just try the Boost Lambda library. It is probably easier than you think.

A. Levy
The new version of the C++ standard has lambda expressions too, though they're a little weird.
David Seiler
Nice to know that C++ is getting lambda expressions now. But when I answered this question 3 months ago, that wasn't clear...at least not to me.
A. Levy
A: 

If by C++.NET you mean C++/CLI, then yes. When MS supports C++0x lambda expressions in their c++ compiler, that support will extend to C++/CLI. If you want lambda expressions now, then you're stuck with Boost.Lambda.

David Seiler
A: 

C++0x lambda functions are available in C++/CLI with the release of VC2010. However, these are native C++ objects, and cannot be used as C++/CLI delegates. You also have to use gcroot<> to pass in .NET handles.

Anthony Williams