Jump to content

C++/CLI

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 188.236.111.122 (talk) at 05:13, 16 December 2020 (Syntax changes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
C++/CLI
ParadigmStructured, imperative, object-oriented
FamilyC
Designed byMicrosoft
DeveloperMicrosoft
First appeared2005; 20 years ago (2005)
Stable release
Standard ECMA-372 / December 2005; 19 years ago (2005-12)
PlatformCommon Language Infrastructure
Websitewww.ecma-international.org/publications/standards/Ecma-372.htm
Influenced by
C++, Managed Extensions for C++, C#

C++/CLI (C++ modified for Common Language Infrastructure) is a language specification created by Microsoft which supersedes Managed Extensions for C++. It is a complete revision that simplifies the now-deprecated Managed C++ syntax[1] and provides interoperability with Microsoft .Net languages such as C#. C++/CLI was standardized by Ecma as ECMA-372. It is currently available in Visual Studio 2005, 2008, 2010, 2012, 2013, 2015, 2017 and 2019 including the Express editions.

C++/CLI is not mentioned in 'The .NET Language Strategy' by Mads Torgersen, posted February 1, 2017.[2]

delete all

Operator overloading

Operator overloading works analogously to standard C++. Every * becomes a ^, every & becomes an %, but the rest of the syntax is unchanged, except for an important addition: for .NET classes, operator overloading is possible not only for classes themselves, but also for references to those classes. This feature is necessary to give a ref class the semantics for operator overloading expected from .NET ref classes. (In reverse, this also means that for .NET framework ref classes, reference operator overloading often is implicitly implemented in C++/CLI.)

For example, comparing two distinct String references (String^) via the operator == will give true whenever the two strings are equal. The operator overloading is static, however. Thus, casting to Object^ will remove the overloading semantics.

//effects of reference operator overloading
String ^s1 = "abc";
String ^s2 = "ab" + "c";
Object ^o1 = s1;
Object ^o2 = s2;
s1 == s2; // true
o1 == o2; // false

C++/C# Interoperability

C++/CLI allows C++ programs to consume C# programs in C# DLLs.[3] Here the #using keyword shows the compiler where the DLL is located for its compilation metadata. This simple example requires no data marshalling.

#include "stdafx.h"
using namespace System;
#using "...MyCS.dll"

int main(array<System::String ^> ^args) {
    double x = MyCS::Class1::add(40.1, 1.9);
    return 0;
}

The C# source code content of MyCS.DLL.

namespace MyCS {
    public class Class1 {
        public static double add(double a, double b) {
            return a + b;
        }
    }
}

This examples shows how strings are marshalled from C++ strings to strings callable from C# then back to C++ strings. String marshalling copies the string contents to forms usable in the different environments.

#include <string>
#include <iostream>
#include <msclr\marshal_cppstd.h>
#include "stdafx.h"
using namespace System;
#using "..MyCS.dll"

int main() {
	std::string s = "I am cat";
	System::String^ clrString = msclr::interop::marshal_as<System::String^>(s); // string usable from C#
	System::String^ t = MyCS::Class1::process(clrString); // call C# function
	std::string cppString = msclr::interop::marshal_as<std::string>(t); // string usable from C++
	std::cout << "Hello, C++/C# Interop!" << std::endl;
	std::cout << cppString << std::endl;
	return 0;
}

The C# code is not in any way C++-aware.

namespace MyCS {
    public class Class1 {
        public static string process(string a) {
            return a.Replace("cat", "dog") + " with a tail";
        }
    }
}

C++/C# interoperability allows C++ simplified access to the entire world of .Net features.

C++/CX

C++/CX targeting WinRT, although it produces entirely unmanaged code, borrows the ref and ^ syntax for the reference-counted components of WinRT, which are similar to COM "objects".[4]

References