C ++ / CLI

from Wikipedia, the free encyclopedia

C ++ / CLI is a variant of the C ++ programming language developed by Microsoft , which enables access to the virtual runtime environment of the .NET platform with the help of specially tailored language extensions.

C ++ / CLI fulfills the specification called Common Language Infrastructure (CLI), also developed by Microsoft, for the language and platform-neutral development and execution of .NET applications. Programs written in C ++ / CLI can be translated into CIL by the compiler and run on the virtual machine of the .NET platform.

A standard for C ++ / CLI officially ratified by Ecma has been available since December 2005 .

Microsoft Visual Studio from version 2005 and the compiler front end of the Edison Design Group offer an implementation of C ++ / CLI.

Purpose of the extensions

The goals when developing C ++ / CLI were:

  • Creation of an elegant syntax that fits in well with the previous C ++. Anyone who is already familiar with C ++ should find the language extensions as natural as possible.
  • Convenient support of special features of the CLI such as properties , events , generic types , automatic garbage collection , reference classes , etc.
  • Good support for language resources that were common in previous C ++, such as templates or deterministic deinitializations, for all types, including the new CLI classes.
  • Compatibility with existing C ++ programs through the introduction of almost exclusively pure extensions compared to ISO-C ++.

Differences to Managed C ++

C ++ / CLI is the result of a fundamental revision of Managed C ++, the first version of C ++ with language extensions for the .NET platform. Managed C ++ suffered from acceptance problems because many programmers found the syntax extensions difficult to read. For example, the syntax contained elements that did not clearly indicate whether managed or unmanaged objects were created.

The syntax elements introduced with Managed C ++ also seemed to be integrated into the language as a makeshift for many programmers. So were z. For example, many keywords have been introduced that start with two underscores. Although this is common with language extensions compared to standard C ++, the large number of such keywords, as well as their strong penetration in programs that made use of the extensions, had a disruptive effect on the overall picture of the source texts.

Examples:

Managed C ++ C ++ / CLI
__gc __interface interface class
Console :: WriteLine ( S "{0}", __box ( 15 ) ); Console :: WriteLine ("{0}", 15);
int f () __gc [] ; // declaration array <int> ^ f (); // declaration 1)
Object * A __gc [] = { __box ( 41 ) , __box ( 42 ) }; array < Object ^> ^ A = {41, 42};

1) Declaration of a function f that returns a CLI sequence (array).

Unlike Managed C ++ which is destructor syntax ~T()no longer on the finalizer displayed. A distinction is made between destructors and finalizers in C ++ / CLI; the finalizer now has the syntax !T(). The destructor is also identical to the function Dispose(this was made possible by technical changes to the CLR ).

Further innovations

Further innovations compared to ISO-C ++ are: improved enum classes (enum class), delegates, packaging ( boxing ), interface classes, sealed classes, attributes, etc.

Object pointer

The most obvious change is the syntax ^for object pointers (sometimes called handles ). Example:

T ^ whole_object_pointer = gcnew T (a, b);

It is gcnew an operator for the allocation of objects from the automatic garbage collection can be managed.

Compare this to the traditional syntax for pointers:

T * plain_old_pointer = new T (a, b);

Deinitialization and memory release

In contrast to normal pointers, when deleting handles delete, the destructor is called, but the memory is not released. Instead, the memory used by the object is returned to the system by automatic garbage collection.

In contrast to other languages ​​with automatic garbage collection (e.g. C # or Java ), the problematic summary of the management of memory and other resources is separated here: Memory and other resources are no longer released together with the help of garbage collection (i.e. not a deterministic one Deinitialization); see finalization .

CLI objects that can be created as automatic variables

Another technical innovation and one of the most important differences to other languages ​​with automatic garbage collection are the CLI objects that can be created as automatic variables (i.e. on the stack). The lifespan of automatic variables ends the moment they leave their scope.

In combination with the new type of object pointers, programming techniques often used in C ++ such as RAII (abbreviation for resource acquisition is initialization ) are also possible for the CLI objects managed with automatic garbage collection. Error-prone coding techniques, as they are known from other programming languages, can thus be avoided.

An example in C ++ / CLI:

void Uebertragung()
{
  MessageQueue source("server\\sourceQueue");
  String^ mqname = safe_cast<String^>(source.Receive().Body);

  MessageQueue dest1("server\\" + mqname), dest2("backup\\" + mqname);
  Message^ message = source.Receive();
  dest1.Send(message);
  dest2.Send(message);
}

When exiting the function Uebertragung(with returnor when an exception occurs), objects implicitly call their function Dispose, in the reverse order to their construction. In the example above, the destructor is first of dest2called, then by dest1and by last source, as these objects in the order source, dest1, dest2were constructed.

If an automatic object goes out of scope or if it is deleted with delete, its destructor is called. The compiler then suppresses the call of the finalization function normally triggered by the automatic memory management .

The omission of finalization functions can have a positive overall effect on the execution speed, but also helps to avoid other problems; for problems when using the finalization function, see finalization .

In contrast to C ++ / CLI, for example, in Java or C # a corresponding function (in C # Dispose, mostly in Java close) must always be explicitly called to release resources . Both languages ​​have therefore developed special syntax constructs to ensure such calls: In C # these are the so-called using blocks , in Java the try-with-resources construct. Such blocks relieve the programmer of ensuring that dispose calls are made when leaving the block, but they always have to be specified and are therefore still more error-prone in this regard than the deterministic deinitialization of C ++ / CLI.

Comparison with other .NET languages

A special feature of C ++ / CLI is the mixability of code that runs on the virtual machine and code that is executed directly on the CPU. Both types of program code can be put together in a single program file. With this option, C ++ / CLI has so far held a special position among the .NET languages.

Web links

Individual evidence

  1. edg.com (PDF) C ++ frontend of the Edison Design Group (EDG)
  2. C ++: The Most Powerful Language for .NET Framework . Retrieved October 21, 2010.