ECMA-372
   HOME

TheInfoList



OR:

C++/CLI is a variant of the
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
, modified for Common Language Infrastructure. It has been part of
Visual Studio Visual Studio is an integrated development environment (IDE) from Microsoft. It is used to develop computer programs including websites, web apps, web services and mobile apps. Visual Studio uses Microsoft software development platforms such ...
2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI to supersede Managed Extensions for C++. In December 2005,
Ecma International Ecma International () is a nonprofit standards organization for information and communication systems. It acquired its current name in 1994, when the European Computer Manufacturers Association (ECMA) changed its name to reflect the organization ...
published C++/CLI specifications as the ECMA-372 standard.


Syntax changes

C++/CLI should be thought of as a language of its own (with a new set of keywords, for example), instead of the C++ superset-oriented Managed C++ (MC++) (whose non-standard keywords were styled like or ). Because of this, there are some major syntactic changes, especially related to the elimination of ambiguous identifiers and the addition of .NET-specific features. Many conflicting syntaxes, such as the multiple versions of operator in MC++, have been split: in C++/CLI, .NET reference types are created with the new keyword (i.e.
garbage collected Garbage, trash, rubbish, or refuse is waste material that is discarded by humans, usually due to a perceived lack of utility. The term generally does not encompass bodily waste products, purely liquid or gaseous wastes, or toxic waste T ...
new()). Also, C++/CLI has introduced the concept of generics from .NET (similar, for the most common purposes, to standard C++ templates, but quite different in their implementation).


Handles

In MC++, there were two different types of pointers: pointers were normal C++ pointers, while pointers worked on .NET reference types. In C++/CLI, however, the only type of pointer is the normal C++ pointer, while the .NET reference types are accessed through a "handle", with the new syntax (instead of ). This new construct is especially helpful when managed and standard C++ code is mixed; it clarifies which objects are under .NET automatic garbage collection and which objects the programmer must remember to explicitly destroy.


Tracking references

A tracking reference in C++/CLI is a handle of a passed-by-reference variable. It is similar in concept to using "" (reference to a pointer) in standard C++, and (in function declarations) corresponds to the "" keyword applied to types in C#, or "" in
Visual Basic .NET Visual Basic, originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visua ...
. C++/CLI uses a "" syntax to indicate a tracking reference to a handle. The following code shows an example of the use of tracking references. Replacing the tracking reference with a regular handle variable would leave the resulting string array with 10 uninitialized string handles, as only copies of the string handles in the array would be set, due to their being passed by value rather than by reference. int main() Note that this would be illegal in C#, which does not allow loops to pass values by reference. Hence, a workaround would be required.


Finalizers and automatic variables

Another change in C++/CLI is the introduction of the finalizer syntax , a special type of nondeterministic destructor that is run as a part of the garbage collection routine. The C++ destructor syntax also exists for managed objects, and better reflects the "traditional" C++ semantics of deterministic destruction (that is, destructors that can be called by user code with ). In the raw .NET paradigm, the nondeterministic destruction model overrides the protected method of the root class, while the deterministic model is implemented through the
interface Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Int ...
method (which the C++/CLI compiler turns the destructor into). Objects from C# or VB.NET code that override the Dispose method can be disposed of manually in C++/CLI with just as .NET classes in C++/CLI can. // C++/CLI ref class MyClass ;


Operator overloading

Operator overloading In computer programming, operator overloading, sometimes termed ''operator ad hoc polymorphism'', is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading i ...
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


Interoperability

C++/CLI allows C++ programs to consume C# programs in C# DLLs. 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 ^args) The C# source code content of MyCS.dll. namespace MyCS; public class Class1 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 #include #include #include "stdafx.h" using namespace System; #using "..MyCS.dll" int main() The C# code is not in any way C++-aware. namespace MyCS; public class Class1 C++/C# interoperability allows C++ simplified access to the entire world of .NET features.


C++/CX

C++/CX targeting
WinRT Windows Runtime (WinRT) is a platform-agnostic component and application architecture first introduced in Windows 8 and Windows Server 2012 in 2012. It is implemented in C++ and officially supports development in C++ (via C++/WinRT, C++/CX or W ...
, although it produces entirely unmanaged code, borrows the ref and ^ syntax for the reference-counted components of WinRT, which are similar to COM "objects".Inside the C++/CX Design - Visual C++ Team Blog - Site Home - MSDN Blogs
/ref>


References


Further reading

* * * * *


External links

*

{{DEFAULTSORT:C++ CLI .NET programming languages C++ programming language family Ecma standards Microsoft Visual Studio