Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

C++/CLI doesn't support lambda expressions

Aug
2,870
153
Now that I can create a plugin in C++/CLI for use with TCC,
I was hoping that I could use LINQ expressions in a plugin.

Here's my test C# Code;
Code:
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string input = "24 +13+12 +21 +46 +14+10+20+14+   30     +18+ 50";
        
        var sortedNumbers = input
            .Split('+')
            .Select(int.Parse)
            .OrderBy(n => n)
            .ToArray();

        Console.WriteLine(string.Join("+", sortedNumbers));
    }
}

Works as expected;
Code:
10+12+13+14+14+18+20+21+24+30+46+50

However, as the subject of this post states,

C++/CLI doesn't support lambda expressions

When attempting to make this a function in a TCC plugin,
VS 2022 kept giving me all of these errors,
and after a Bing search,
I learned that C++/CLI doesn't support lambda expressions.

LINQ in C++/CLI doesn’t have a direct namespace you can import like using namespace System::Linq; the way it does in C#.

LINQ functions are instead part of System::Linq::Enumerable,
meaning you have to reference Enumerable explicitly when using them.

Which led to more errors,
and jumping through hoops.

Posting for my future reference, but others may also be interested.

Joe
 
C++ is very powerful but it is quite a bit older than C# and its syntax and language model is hampered by the fact that one of its original design goals was to be "as upwardly compatible as possible" with C. They eventually discarded that, but a lot of those limitations remain.

C# also had the benefit of being designed after C++ and Java (and other OOP languages) so that the lessons learned could be applied to a fresh canvas.

The short of it is, C++ doesn't have lamba expresssions, and it doesn't have reflection either. And the genericity mechanism (templates) in C++ is a rather preprocessor-y model (since that's how it was originally implemented), and is byzantine in its complexity.
 
As I understand it, C++/CLI was created as a bridge between C++ and the .NET Framework.

Here's an example of C++/CLI using reflection...
Code:
#include <iostream>
using namespace System;
using namespace System::Reflection;

int main()
{
    // Get the type information for System.String
    Type^ type = Type::GetType("System.String");

    if (type != nullptr)
    {
        Console::WriteLine("Properties of System.String:");

        // Get and display all properties
        array<PropertyInfo^>^ properties = type->GetProperties();
        for each (PropertyInfo^ prop in properties)
        {
            Console::WriteLine(prop->Name + " - " + prop->PropertyType->Name);
        }
    }
    else
    {
        Console::WriteLine("Type not found.");
    }

    return 0;
}

Compile and execute...
Code:
R:\>#cl /clr test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34810
for Microsoft (R) .NET Framework version 4.08.9310.0
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 14.43.34810.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

R:\>#test.exe
Properties of System.String:
Chars - Char
Length - Int32

It would have been great if Microsoft had also thought about LINQ in C++/CLI .

Joe

Ref: Walkthrough: Compiling a C++/CLI Program on the Command Line
 
Thankyou @rconn for the ability to create plugins using .NET,
much easier than using C++/CLI to create plugins.

I can now use Lambda expressions;
Code:
            // Example 10: Lambda Expressions
            string input = "24 +13+12 +21 +46 +14+10+20+14+   30     +18+ 50";

            var sortedNumbers = input
                .Split('+')
                .Select(int.Parse)
                .OrderBy(n10 => n10)
                .ToArray();

            Console.WriteLine(string.Join("+", sortedNumbers));

Joe
 

Attachments

Back
Top