Managed Extensions for C++ or Managed C++ is a now-deprecated set of language extensions for C++, including grammatical and syntactic extensions, keywords and attributes, to bring the C++ syntax and language to the .NET Framework. These extensions were created by Microsoft to allow C++ code to be targeted to the Common Language Runtime (CLR) in the form of managed code, as well as continue to interoperate with native code.

In 2004, the Managed C++ extensions were significantly revised to clarify and simplify syntax and expand functionality to include managed generics. These new extensions were designated C++/CLI and included in Microsoft Visual Studio 2005.[1] The term Managed C++ and the extensions it refers to are thus deprecated and superseded by the new extensions.

Microsoft introduced Managed Extensions for C++ in Microsoft Visual C++ 2002 (MSVC++). Microsoft attempted to minimise the deviations between standard C++ and Managed Extensions for C++, resulting in core differences between the two being syntactically obscured. MSVC++ 2003 and 2005 also provided support for writing programs in Managed C++. In 2004, Managed Extensions for C++ was deprecated in favor of C++/CLI, a second attempt by Microsoft at supporting programming for the Common Language Infrastructure using C++.[2]

Managed refers to managed code that it is run in, or managed by, the .NET virtual machine that functions as a sandbox for enhanced security in the form of more runtime checks, such as buffer overrun checks. Additionally, applications written in Managed C++ compile to CIL—Common Intermediate Language—and not directly to native CPU instructions like standard C++ applications do.

Managed C++ code could inter-operate with any other language also targeted for the CLR such as C# and Visual Basic .NET as well as make use of features provided by the CLR such as garbage collection. This means Managed C++ occupies a unique position in the gallery of .NET languages. It is the only language that can communicate directly with .NET languages (such as C#, VB.NET) as well as native C++. The other .NET languages can only communicate with C++ code via PInvoke or COM. But since Managed C++ can communicate directly in both managed and standard C++ contexts, it is often used as a "bridge".

Programs coded in Managed C++ provide additional functionality of the .NET Framework and the CLR. Most notable of these is garbage collection, which relieves the programmer of manual memory management. The garbage collector (GC) is handled by the CLR. Memory management is executed quite quickly, but for more performance critical applications, native, unmanaged code is most likely the preferred option.

Managed C++ is geared towards object-oriented programming. A major difference between standard C++ and Managed C++ is that multiple inheritance is not supported, and a class managed under the CLR's garbage collector cannot inherit more than one class. This is because of a limitation of the CLR.

Key features:

Exensible metadata: information provided to describe the structure and types of a managed component. It can be extended and reused to create software components. It is used heavily in C# and Visual Basic .NET

Garbage collection: the CLR is fully managed by a garbage collector for memory management automated by the CLR itself, i.e. the delete operator need not be called in managed C++ code.

Interoperability with .NET languages: code targeted for the .NET Framework produces Microsoft Intermediate Language (MSIL, similar to Java bytecode) output and thus, modules compiled and components (assemblies rather) can be reused by other program components written in another language targeting the .NET Framework, such as JScript .NET, C#, Visual Basic .NET and other 3rd party languages for .NET.

Versioning: new methods and data members can be introduced into existing managed classes without breaking binary compatibility with existing client side software

Binary headers: allows reusing of precompiled metadata; any .exe, .dll, .obj or .netmodule compiled into MSIL can be referenced from a C++ source file

Buffer overflow protection - with the introduction of garbage collection into C++, Managed C++ is less prone to the common buffer overflow errors caused by the absence of data type checking in standard C++. The garbage collector helps to reduce (not completely though) the frequency of these errors.

Managed and unmanaged code can be mixed together in the same CLI assembly seamlessly. This allows the programmer to keep unmanaged code that cannot be ported over to the .NET Framework without re-writing it completely. Some ramifications of using this hybrid convention are present though.

Managed C++ is the only language that can contain unmanaged code and natively communicate with all other .NET languages. Managed C++ is thus very convenient for interoperability between programmers who use different languages, including those in the .NET theater and those who use standard C++.

Managed C++ introduces a lot of new keywords and syntactic conventions that can impair the readability of code, especially if C++ code is included directly and interacts directly with Managed C++ code in the same assembly.

Managed C++ is superseded by C++/CLI and thus obsolete as C++/CLI has been standardized.

Managed C++ requires a slightly longer development time than other .NET languages that could be applied to projects that still produce the same results. The use of pointers may or may not be a requirement, because managed C++ has both value types (__value struct and __value class) and reference types (__gc struct and __gc class).

Managed C++ fully support ASP.NET web applications, even though the development is more difficult than with other .NET languages, including some other third party languages.

Managed C++ includes only support for templates (for interoperability with native C++) but no support for generics (for interoperability with all the other .NET languages). C++/CLI supports both templates (at compile time) and generics (at run time).

The preceding code can be compiled and executed without any fear of memory leaks. Because class gc is managed under the garbage collector, there is no need to call the delete operator. To achieve the same with unmanaged code, the delete keyword is required:

C++ does not require an installation of an associated compiler and managed runtime environment on the target system

C++ supports generic programming. Until the final release of C++/CLI however, Managed C++ programmers must revert for workarounds for using generics in their solutions.

C++ supports the keyword "const" and const correctness. Managed C++, like Java and C#, does not contain this feature. An alternative is to make a managed class immutable, or restricting set accessors on public interfaces.

C++ code is not constricted by the CLR's restrictions. For example, the CLR does not allow classes to inherit other classes privately nor protected, thus the following will produce a compiler error:

Java provides a documentation on the source code, while Managed C++ does not.

Java has many other development tools and solutions available for Java programmers to use, while Managed C++ is only available under Visual Studio .NET.

Advantages

Managed C++ can access the computer system on a low level interface much more easily than Java. Java programmers must use the JNI (Java Native Interface) to use low level services of the host operating system.