locked
Lambda expressions in C++/CLI RRS feed

  • Question

  • Hello,

    Can anyone please provide me the pointer on how to write Lambda expressions in C++/CLI. I wrote below code, but it is giving me compile time errors with error code as C2143 and C2059.

    System::Threading::Tasks::Parallel::ForEach(ListOfItems, item =>

    {
    CreateCopyOfItems(item);

                                     });


    Regards, http://www.shwetalodha.blogspot.in/


    Tuesday, October 8, 2013 12:59 PM

Answers

  • C++/CLI doesn't support lambda expressions. Put the code in a separate method and create a delegate manually:

    ref class foo {
        void bar() {
            List<int> ^listOfItems = gcnew List<int>;
            System::Threading::Tasks::Parallel::ForEach(listOfItems, gcnew Action<int>(this, &foo::moo));
        }
    
        void moo(int item) {
            CreateCopyOfItems(item);
        }
    };

    If the only thing you have in the lambda expression is that CreateCopyOfItems call then you could use that directly in the delegate, without the additional 'moo' function.

    Tuesday, October 8, 2013 1:16 PM

All replies

  • C++/CLI doesn't support lambda expressions. Put the code in a separate method and create a delegate manually:

    ref class foo {
        void bar() {
            List<int> ^listOfItems = gcnew List<int>;
            System::Threading::Tasks::Parallel::ForEach(listOfItems, gcnew Action<int>(this, &foo::moo));
        }
    
        void moo(int item) {
            CreateCopyOfItems(item);
        }
    };

    If the only thing you have in the lambda expression is that CreateCopyOfItems call then you could use that directly in the delegate, without the additional 'moo' function.

    Tuesday, October 8, 2013 1:16 PM
  • Hello Mike,

    Thanks for your quick response. I tried your suggestion, but I am getting lot of other errors now:

    error C2784: 'System::Threading::Tasks::ParallelLoopResult System::Threading::Tasks::Parallel::ForEach(System::Collections::Generic::IEnumerable<T> ^,System::Action<T1,T2> ^)' : could not deduce generic argument for 'System::Collections::Generic::IEnumerable<T> ^' from 'Vector<T>'

    Please note, definition of listOfItems is:

    ReOVector<Equipment*> listOfItems ;


    Regards, http://www.shwetalodha.blogspot.in/

    Tuesday, October 8, 2013 1:43 PM
  • Hmm, does that vector thing implement IEnumerable<T>?
    Tuesday, October 8, 2013 2:09 PM
  • Yes, it does.

    Regards, http://www.shwetalodha.blogspot.in/

    Tuesday, October 8, 2013 3:10 PM
  • This vector of yours looks rather strange. For one thing the type argument appears to be a pointer. That doesn't work with generic types. Even if ReOVector is not a generic ref class and it is a template class this wouldn't work because it could not implement IEnumerable<Equipment *>.

    Also, the listOfItems variable is not a reference, it's missing a hat in the declaration. If that's the intention then you need to use % to pass it to ForEach (and pretty much any other .NET method).

    ...ForEach(%listOfItems,...);

     
    • Proposed as answer by Anna Cc Friday, October 11, 2013 7:55 AM
    Tuesday, October 8, 2013 4:52 PM