C++/CX

C++/CX (component extensions) is a language extension for C++ compilers from Microsoft that enables C++ programmers to write programs for the new Windows Runtime platform, or WinRT.

The language extensions borrow syntax from C++/CLI but target the Windows Runtime and native code instead of the Common Language Runtime and managed code. It brings a set of syntax and library abstractions that interface with the COM-based WinRT programming model in a way that is natural to native C++ programmers.

It is possible to call the Windows Runtime from Standard C++ via the Windows Runtime C++ Template Library (WRL).

Extension syntax

C++/CX introduces syntax extensions for programming for the Windows Runtime. The overall non platform-specific syntax is compatible with the C++11 standard.

Objects

WinRT objects are created, or activated, using ref new and assigned to variables declared with the ^ (hat) notation inherited from C++/CLI.

Foo^ foo = ref new Foo();

A WinRT variable is simply a pair of a pointer to virtual method table and pointer to the object's internal data.

Reference counting

A WinRT object is reference counted and thus handles similarly to ordinary C++ objects enclosed in shared_ptrs. An object will be deleted when there are no remaining references that lead to it.

There is no garbage collection involved. Nevertheless, the keyword gcnew has been reserved for possible future use.

Classes

Runtime classes

There are special kinds of runtime classes that may contain component extension constructs. These are simply referred to as ref classes because they are declared using ref class.

public ref class MyClass
{

};
Partial classes

C++/CX introduces the concept of partial classes. The feature allows a single class to be split across multiple files, mainly to enable the XAML graphical user interface design tools to auto-generate code in a separate file in order not to break the logic written by the developer. The parts are later merged at compilation.

.NET languages like C# have had this feature for many years. Partial classes have not yet made it into the C++ standard and cannot therefore be used in pure C++11.

A file that is generated and updated by the GUI-designer, and thus should not be modified by the programmer. Note the keyword partial.

// foo.private.h
#pragma once

partial ref class foo
{
private:
   int id_;
   Platform::String^ name_;
};

The file where the programmer writes user-interface logic. The header in which the compiler-generated part of the class is defined is imported. Note that the keyword partial is not necessary.

// foo.public.h
#pragma once
#include "foo.private.h"

ref class foo
{
public:
   int GetId();
   Platform::String^ GetName();
};

This is the file in which the members of the partial class are implemented.

// foo.cpp
#include "pch.h"
#include "foo.public.h"

int foo::GetId() {return id_;}
Platform::String^ foo::GetName {return name_;}

Generics

Windows Runtime and thus C++/CX supports runtime-based generics. Generic type information is contained in the metadata and instantiated at runtime, unlike C++ templates which are compile-time constructs. Both are supported by the compiler and can be combined.

generic<typename T> 
public ref class bag 
{
     property T Item;
};

Metadata

See also: Metadata (CLI)

All WinRT programs expose their declared classes and members through metadata. The format is the same that was standardized as part of the Common Language Infrastructure (CLI), the standard created from the .NET Framework. Because of this, code can be shared across C++/CX, CLI languages and Javascript that target Windows Runtime.

Runtime library

The C++/CX has a set of libraries that target the Windows Runtime. These help bridge the functionality of the C++ Standard Library and WinRT.

Preprocessor-based detection

You can detect if C++/CX extension is turned on by testing existence of __cplusplus_winrt preprocessor symbol.

#ifdef __cplusplus_winrt
// C++/CX specific code goes here...
#endif

See also

External links

This article is issued from Wikipedia - version of the Friday, September 18, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.