C++/CLI

C++/CLI

C++/CLI (Common Language Infrastructure) is Microsoft's language specification intended to supersede Managed Extensions for C++. Completely revised to simplify the older Managed C++ syntax (which is now deprecated), it provides much more clarity and code readability than Managed C++. C++/CLI is standardized by Ecma as ECMA-372. It is currently only available in Visual Studio 2005 and 2008 (also included in the Express Editions).

yntax 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 __gc or __value). 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 new() in MC++ have been split: in C++/CLI, .NET reference types are created with the new keyword gcnew. Also, C++/CLI has introduced the concept of generics (conceptually similar to unmanaged C++ templates, but quite different in their implementation).

Handles

Back in MC++, there were two different types of pointers: __nogc pointers were normal C++ pointers, while __gc pointers worked on .NET reference types. In C++/CLI the only type of pointer is the normal C++ pointer, and the .NET reference types are accessed through a "handle", with the new syntax ClassName^ instead of ClassName*. This new construct is especially helpful when managed and unmanaged code is mixed; it clarifies which objects are under .NET automatic garbage collection and which objects the programmer must remember to explicitly destroy.// Managed extensions for C++
#using using namespace System::Collections;__gc class referencetype{protected: String* stringVar; int intArr __gc [] ; ArrayList* doubleList;public: referencetype(String* str, int* pointer, int number) // Which one is managed? { doubleList = new ArrayList(); System::Console::WriteLine(str->Trim() + number.ToString()); ;// C++/CLI
#using using namespace System::Collections::Generic;ref class referencetype{protected: String^ stringVar; array^ intArr; List^ doubleList;public: referencetype(String^ str, int* pointer, int number) // Ambiguous no more { doubleList = gcnew List(); System::Console::WriteLine(str->Trim() + number); ;

Tracking references

A tracking reference in C++/CLI is a handle that is passed by reference rather than by value. They correspond to the "ref" keyword applied to types in C#, or "ByRef" in Visual Basic .NET. C++/CLI uses a "^%" syntax to indicate a tracking reference to a handle. It is similar in concept to using "*&" (reference to a pointer) in Standard C++.

The following code shows an example use for 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 them being passed by value rather than by reference.int main(){ array^ arr = gcnew array(10); int i = 0;

for each(String^% s in arr) s = i++.ToString();

return 0;}

The code above additionally serves as an example in how .NET languages allow users to do slightly different things, as e.g. C# does not allow the user to use foreach loops in the directly corresponding way. That is; foreach(ref string s in arr) is illegal in C# as it does not allow foreach loops to pass values by reference, and other workarounds need to be used in this case.

Finalizers and automatic variables

Another change in C++/CLI is the introduction of the finalizer syntax !ClassName(), a special type of nondeterministic destructor that is run as a part of the garbage collection routine, so now the destructor syntax ~ClassName() better reflects the "traditional" C++ semantics of deterministic destruction (that is, destructors that can be called by user code). Moreover, the destruction of all managed objects with a defined destructor in a method can be made automatic with the new syntax shown in the example.

In the raw .NET paradigm (for example, direct programming in CIL), the deterministic destruction model is implemented through the IDisposable interface method Dispose (which is just what the C++/CLI compiler turns the destructor into), while the nondeterministic one overrides the protected Finalize method of the root Object class.

Usually, deterministic destruction is recommended when non-managed resources are involved or when system-wide limited resources (network/database connections, file streams, etc.) are used. In all other cases, the non-deterministic approach should suffice. Whenever a deterministic destructor is used, the programmer can still benefit from the Garbage Collector, to avoid possible unmanaged memory/resource leaks, by also creating a finalizer (GC-destructor) that just checks whether the destructor has been invoked and calls it if it hasn't (thus adding little performance penalty).// C++/CLIref class MyClass // : IDisposable (this is added by the compiler){public: MyClass(); // constructor ~MyClass(); // (deterministic) destructor (turned into IDisposable.Dispose() by the compiler)protected: !MyClass(); // finalizer (non-deterministic destructor) (former destructor syntax => virtual void Finalize())

public: static void Test() { MyClass automatic; // Not a handle, no initialization: compiler calls constructor here // Use 'automatic' anywhere in the method // Equivalent user code: MyClass^ user = gcnew MyClass(); try { /* Use user here */ } finally { delete user; }

// Compiler calls automatic's destructor in the finally of a try containing the whole method ;// C#class MyClass : IDisposable{ public MyClass() {} // constructor public void Dispose() {} // Dispose method (C++/CLI deterministic destructor) ~MyClass() {} // destructor (non-deterministic) (C++/CLI finalizer => protected override void Finalize()) public static void Test() { using(MyClass automatic = new MyClass()) { /* Use automatic here */ } // Compiler calls automatic.Dispose(), in the finally of a try containing the using block

// Equivalent user code:

MyClass user = new MyClass(); try { /* Use user here */ } finally { user.Dispose(); }

Operator overloading

Operator overloading works analogously to unmanaged C++. Every * becomes a ^, every & becomes an %, but the rest of the syntax is unchanged. Except for an important addition: 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 effect, this also means that for .Net framework ref classes, reference operator overloading is often implemented.

This means that comparing two distinct String references via the operator = will give true whenever the two strings are equal. Of course, operator overloading is static, as is and always should be, not only when writing managed codePOV-assertion. Thus, casting to Object ^ will remove the overloading semantics.

The standard semantics would be to only overload by object (the classic C++ way) for native and value types, and to overload only by reference (^) for ref classes. Of course, in C++-only projects, it may be reasonable to decide against overloading by reference, and stick to classic C++ operator overloading semantics also for ref classes, which, as experience tellsFact|date=November 2007, will often enough be used with on-the-stack semantics and implemented with a copy constructor and assignment operator.//effects of reference operator overloadingString ^s1 = "abc";String ^s2 = "ab" + "c";Object ^o1 = s1;Object ^o2 = s2;s1 = s2; // trueo1 = o2; // false

Template syntax

// a template ref class with operator overloading, copy constructor and assignment operator template public ref class ptr_wrapper sealed { private: T *m_ptr; ptr_wrapper(T *i_ptr) :m_ptr(i_ptr) { if (i_ptr = 0) { throw gcnew System::Exception("Trying to initialize ptr_wrapper with null pointer"); } } public: ptr_wrapper(const T &i_ref) :m_ptr(new T(i_ref)) { } ptr_wrapper(const ptr_wrapper %i_other) :m_ptr(new T(const_cast(*i_other))) { } static ptr_wrapper take(T *i_ptr) { return ptr_wrapper(i_ptr); } ~ptr_wrapper() { delete m_ptr; } ptr_wrapper % operator = (const ptr_wrapper %other) { if (other.m_ptr != m_ptr) { T* new_ptr = new T(*other); delete m_ptr; m_ptr = new_ptr; } } static T& operator * (ptr_wrapper %inst) { return *(inst.m_ptr); } static const T& operator * (const ptr_wrapper %inst) { return *(inst.m_ptr); } static T* operator -> (ptr_wrapper %inst) { return inst.m_ptr; } static const T* operator -> (const ptr_wrapper %inst) { return inst.m_ptr; } };

External links

* [http://www.ecma-international.org/publications/standards/Ecma-372.htm ECMA 372: C++/CLI Language Specification]
* [http://msdn.microsoft.com/en-us/library/xey702bw.aspx MSDN documentation for C++/CLI]
* [http://appft1.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PG01&p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.html&r=1&f=G&l=50&s1=%2220060089942%22.PGNR.&OS=DN/20060089942&RS=DN/20060089942 Patent application regarding whitespace in keywords]
* [http://www.research.att.com/~bs/bs_faq.html#CppCLI Bjarne Stroustrup's (developer of C++) views on C++/CLI]
* [http://msdn.microsoft.com/msdnmag/issues/06/00/PureC/default.aspx Stanley B. Lippman. Hello, C++/CLI]
* [http://blogs.msdn.com/slippman/archive/2004/08/05/209606.aspx Stanley B. Lippman. Why C++/CLI Supports both Templates for CLI Types and the CLI Generic Mechanism]
* [http://blogs.msdn.com/hsutter/archive/2003/11/23/53519.aspx Herb Sutter. C++/CLI keywords: Under the hood]
* [http://www.gotw.ca/publications/C++CLIRationale.pdf Herb Sutter. C++/CLI Rationale]
* [http://www.codeproject.com/managedcpp/cppcliintro01.asp A first look at C++/CLI. By Nishant Sivakumar]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Cli-N-Tel — (born Marquette Hawkins) is a D.J./rapper hailing from Compton, California. Most noted for joining the World Class Wreckin Cru (1983 1985) and his contribution for the break through West Coast Electro hop records Surgery and Juice . Shortly after …   Wikipedia

  • Clì Gàidhlig — logo. Clì Gàidhlig ([ˈkʰliː ˈkaːlɪkʲ]) formerly known as Comann an Luchd Ionnsachaidh ([ˈkʰomən̪ˠ ə lˠ̪uxˈkʲũːn̪ˠs̪əxɪ], the Learners Society ), or CLI, is an organisation based in Inverness which seeks to support learners of the Scottish Gaelic… …   Wikipedia

  • CLI — Saltar a navegación, búsqueda Significados de CLI: Command line interface o en español Línea de comandos (interacción con una computadora, y en contraste a un GUI) Call Level Interface (un API de gestión de base de datos SQL) Common Language… …   Wikipedia Español

  • cli — cli·na·men; cli·nan·dri·um; cli·ner; cli·ni·cian; …   English syllables

  • CLI — may refer to: Computing Command line interface, sending commands to a computer by text typed into a command line interpreter (command line shell). Call Level Interface, an SQL database management API Common Language Infrastructure, a Microsoft… …   Wikipedia

  • Cli — Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom. CLI, sigle composé des trois lettres C, L et I, est un acronyme pouvant signifier : Command line interface ou « Interface de ligne de… …   Wikipédia en Français

  • cli´quish|ness — cli|quish «KLEE kihsh, KLIHK ihsh», adjective. 1. like a clique: »At the start visitors would drop in, but the gathering was as cliquish as the Mafia and they would soon duck out (Alistair Cooke). 2. tending to form a clique. –cli´quish|ly,… …   Useful english dictionary

  • cli´quish|ly — cli|quish «KLEE kihsh, KLIHK ihsh», adjective. 1. like a clique: »At the start visitors would drop in, but the gathering was as cliquish as the Mafia and they would soon duck out (Alistair Cooke). 2. tending to form a clique. –cli´quish|ly,… …   Useful english dictionary

  • cli|quish — «KLEE kihsh, KLIHK ihsh», adjective. 1. like a clique: »At the start visitors would drop in, but the gathering was as cliquish as the Mafia and they would soon duck out (Alistair Cooke). 2. tending to form a clique. –cli´quish|ly, adverb.… …   Useful english dictionary

  • CLI —   [Abk. für Command Line Interface, dt. »Kommandozeilen Schnittstelle«] das, eine gewöhnlich vom Betriebssystem zur Verfügung gestellte Zeile, in die der Benutzer Anweisungen eingeben und über die er mit dem System kommunizieren kann. Eine solche …   Universal-Lexikon

  • cli´ent|less — cli|ent «KLY uhnt», noun. 1. a person or group for whom a lawyer, certified public accountant, architect, or other professional person or service acts: »The lawyers have a saying that “the man who pleads his own case has a fool for a client”… …   Useful english dictionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”