Three Microsoft languages use C++ to write code for a runtime. They are separate languages with separate compiler options, and their files share the same extensions. C++/CLI targets the .NET common language runtime; C++/CX targets the Windows Runtime (WinRT, the runtime of UWP apps); C++/WinRT also targets the Windows Runtime, with ordinary C++ and no extensions.
Quick reference
| C++/CLI | C++/CX | C++/WinRT | |
|---|---|---|---|
| runtime | .NET (CLR) | Windows Runtime | Windows Runtime |
| option | /clr | /ZW | none: standard C++ (C++17 or later) with a header library |
| code produced | MSIL, with native code beside it | native machine code | native machine code |
| extensions | the whole set on these pages | a subset of the same syntax: ref class, ^, %, property, event, delegate, interface class, enum class, generic, for each, safe_cast, array | none: no new keywords |
| allocation | gcnew | ref new | winrt::make, or the constructors of a projected type |
| memory management | garbage collector | reference counting | reference counting through smart pointers |
| library | the .NET class library; namespace System:: | the Windows Runtime types, with Platform:: and Windows:: namespaces | the winrt:: namespace, which is generated from the Windows metadata |
| status | maintained for interop | Microsoft supports it but recommends C++/WinRT for new applications | the recommended language |
C++/CX
The C++/CX extensions are a subset of C++/CLI. The syntax is the same in most cases, but the code that is generated depends on the option: /ZW targets the Windows Runtime and /clr targets .NET. A handle in C++/CX is a reference-counted pointer to a COM object: the count goes up when the handle is copied and down when it is set to nullptr or leaves its scope, and the object is deleted when it reaches zero, at once, not at some later collection.
// compile with /ZW: C++/CX
using namespace Platform;
public ref class Greeter sealed {
public:
property String^ Name;
Greeter(String^ name) { Name = name; }
};
auto g = ref new Greeter("world");
The differences that show in a file: ref new instead of gcnew, the namespaces Platform:: and Windows:: (Platform::String^, Windows::Foundation), a public ref class that must be sealed to be visible outside the component, and no #using of a .NET assembly. The lines [Windows::Foundation::Metadata::WebHostHidden] and using namespace Windows::UI::Xaml; are typical of a C++/CX app, and the project files are .vcxproj with WindowsTargetPlatformMinVersion settings. The template-like words array, interior_ptr and pin_ptr of C++/CLI live in the lang namespace in C++/CX (cli in C++/CLI).
C++/WinRT
C++/WinRT is a language projection written as standard C++17 header files: there is nothing to add to the language, so its files are ordinary C++ with winrt:: in them, and its components are declared in IDL files (.idl, MIDL 3.0) and not in the C++ source. It cannot be told from plain C++ by its keywords, and the viewer shows it as plain C++.
How the viewer treats each
- C++/CLI: a file with a C++/CLI mark (the Overview page lists them) is a C++/CLI source file.
- C++/CX: a file with
ref new,Platform::orWindows::and none of the marks of the CLR (#using,gcnew,System::,for each,safe_cast,pin_ptr,interior_ptr,cli::,#pragma managed) is C++/CX, and is shown as plain C++, with no colour for the extensions. A file that has both is C++/CLI. - C++/WinRT: plain C++.
Not described
Known, not yet covered
- Reference: C++/CX and C++/WinRT have no pages of their own on this site: this page only says how they relate to C++/CLI. The C++/CX language reference (
/ZW, thePlatformnamespace, the restrictions of public types, the XAML tooling) and the C++/WinRT reference were not read in full. The statements here come from the Microsoft page on the component extensions and from knowledge of the languages. - Viewer: C++/CX files have no colouring for their extensions (
ref new,Platform::String^): the plain C++ scanner reads them. The.cxextension named in some tools was not found on this site and is not read.