Pages

November 17, 2010

C++/CLI

Having spent a lot of time programming in C++ over the last few months, I have come to appreciate some of the features of the language, such as the powerful templating mechanism, which is quite a bit more powerful than the .NET generics. There are seem to be a few cases when one would have to resort to tricky reflection techniques in C#/VB.NET, which costs both in terms of runtime overhead and also in code mantainability.

I have only recently became more acutely aware of the C++/CLI. Seems like it has a number of cool features that a worth a look:
  1. template system similar to C++
  2. much cheaper mechanism to invoke native code (compared to PInvoke)
  3. possibility to mix and match managed and unmanaged memory pools for performance optimization.
There is a pretty solid article from a solid C++ guy about C++/CLI.

Here is an excerpt:

C++/CLI represents a tuple. C++ refers, of course, to the C++ programming language invented by Bjarne Stroustrup at Bell Laboratories. It supports a static object model that is optimized for the speed and size of its executables. However, it doesn't support run-time modification of the program other than heap allocation. It allows unlimited access to the underlying machine, but very little access to the types active in the running program and no real access to the associated infrastructure of that program. Herb Sutter, a former colleague of mine at Microsoft and the chief architect of C++/CLI, refers to C++ as a concrete language.


CLI refers to the Common Language Infrastructure, a multitiered architecture supporting a dynamic component programming model. In many ways, this represents a complete reversal of the C++ object model. A runtime software layer, the virtual execution system, runs between the program and the underlying operating system. Access to the underlying machine is fairly constrained. Access to the types active in the executing program and the associated program infrastructure—both as discovery and construction—is supported. The slash (/) represents a binding between C++ and the CLI. The details surrounding this binding make up the general topic of this column.


So, a first approximation of an answer to what is C++/CLI is that it is a binding of the static C++ object model to the dynamic component object model of the CLI. In short, it is how you do .NET programming using C++ rather than C# or Visual Basic®. Like C# and the CLI itself, C++/CLI is undergoing standardization under the European Computer Manufacturers Association (ECMA) and eventually under ISO.


The common language runtime (CLR) is the Microsoft version of the CLI that is specific to the Windows® operating system. Similarly, Visual C++® 2005 is the implementation of C++/CLI.


As a second approximation of an answer, I would say that C++/CLI integrates the .NET programming model within C++ in the same way as, back at Bell Laboratories, we integrated generic programming using templates within the then existing C++. In both of these cases your investment in an existing C++ codebase and in your existing C++ expertise are preserved. This was an essential baseline requirement of the design of C++/CLI.

No comments :