Export (0) Print
Expand All

1 Introduction

Visual Studio .NET 2003

The common language runtime executes Microsoft intermediate language (MSIL). The MSIL instruction set is sufficiently powerful to run any existing C++ program. Viewed in this way, the runtime is another target architecture for the C++ language. However once a C++ program is compiled to MSIL, it may utilize the powerful features of the runtime.

The term managed code refers to the MSIL code produced by the compiler. Compiling to managed code is accomplished by use of the /clr compiler option. Unmanaged code is the native machine target code produced by an ordinary compilation.

The vast majority of existing C++ functions can be compiled to MSIL with no change in semantics, as described in Section 24.1.

At the time of this release, there are some exceptions that must be compiled to native code. However, since managed and unmanaged code can be mixed in a single compilation unit, this does not prevent the program as a whole from accessing managed features.

Compiling with the /clr option does not alter the semantics of an existing C++ program. For example, C++ classes do not become garbage collected or seamlessly interoperate with Visual Basic unless they are modified. Such features are only provided for Managed Extensions classes, as described here.

As mentioned above, any C++ program can be compiled to the common language runtime. However, the runtime defines a particular object model that does not support all features of the C++ language. For example, multiple inheritance of classes is not supported. There are currently no compiler options or pragmas that turn all of a C++ program's classes into Managed Extensions classes.

There are three main design goals of the Managed Extensions:

  • Protection of Investment. Make it possible to expose all or part of existing C++ programs to the Microsoft .NET Framework. Likewise, make it possible to access managed features in existing C++ programs.
  • Access and Control. Allow mixing of managed and unmanaged code, provide direct access to low-level unmanaged APIs, and so on. The highest performance managed APIs can be written in Visual C++ with Managed Extensions. Also allow access and control over managed features such as boxing. This provides better performance.
  • Ease of Use. The extensions are first-class features of the language and are directly supported by the compiler. There are no preprocessing or debugger issues for the user to resolve.

The following example shows the compilation of a simple "hello, world" C++ program using the Managed Extensions:

Example

// mcpp_helloworld.cpp
// compile with: /clr
#using <mscorlib.dll>
int main() {
   System::Console::WriteLine(S"hello, world");
}

Output

hello, world
Show:
© 2015 Microsoft