How to create a managed delegate from a lambda expression RRS feed

  • Question

  • I would like to write a function in managed C++ that returns a managed delegate for a lambda expression that is defined within the function. E.g. something like:

    System::Func<Boolean> GetOperation()
        return [] () { return true; };

    I get a compile error complaining that lambda function can not be converted to the delegate.

    In C#, of course, the delegate is automatically created for similar code. How can I create a delegate for the lambda function in C++?


    Sunday, April 18, 2010 12:54 AM


  • C++ lambda expressions have only been put into the native C++ language. They work very differently to the lambda expressions in C# and because of this, you can't do what you want.

    Unless Microsoft invest time into making the new C++0x features work with C++/CLI then don't assume that they will work properly with the managed language.

    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    • Marked as answer by Nancy Shao Monday, April 26, 2010 10:03 AM
    Sunday, April 18, 2010 3:20 PM