C++/CX
   HOME

TheInfoList



OR:

C++/CX ''(C++ component extensions)'' is a language projection for
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washin ...
's
Windows Runtime Windows Runtime (WinRT) is a platform-agnostic component and application architecture first introduced in Windows 8 and Windows Server 2012 in 2012. It is implemented in C++ and officially supports development in C++ (via C++/WinRT, C++/CX or ...
platform. It takes the form of a language extension for
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
s, and it enables C++ programmers to write programs that call
Windows Runtime Windows Runtime (WinRT) is a platform-agnostic component and application architecture first introduced in Windows 8 and Windows Server 2012 in 2012. It is implemented in C++ and officially supports development in C++ (via C++/WinRT, C++/CX or ...
(WinRT)
API An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how ...
s. C++/CX is superseded by the
C++/WinRT C++/WinRT is a C++ library for Microsoft's Windows Runtime platform, designed to provide access to modern Windows APIs. C++/WinRT is provided as a standard C++17 header file library, unlike C++/CX, which is an extension to C++ and requires a recen ...
language projection, which is ''not'' an extension to the C++ language; rather, it's an entirely standard modern ISO C++17 header-file-based library.Introduction to C++/WinRT
docs.microsoft.com
The language extensions borrow syntax from
C++/CLI C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI ...
but target the Windows Runtime
Universal Windows Platform Universal Windows Platform (UWP) is a computing platform created by Microsoft and first introduced in Windows 10. The purpose of this platform is to help develop universal apps that run on Windows 10, Windows 10 Mobile (discontinued), Windo ...
native code In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a ver ...
instead of the Common Language Runtime and
managed code Managed code is computer program code that requires and will execute only under the management of a Common Language Infrastructure (CLI); Virtual Execution System (VES); virtual machine, e.g. .NET, CoreFX, or .NET Framework; Common Language Runt ...
. It brings a set of syntax and library abstractions that project COM's WRL subset-based WinRT
programming model A programming model is an execution model coupled to an API or a particular pattern of code. In this style, there are actually two execution models in play: the execution model of the base programming language and the execution model of the prog ...
in a way that is intuitive to C++/CLI managed extensions' coders. It is possible to call the Windows Runtime from native ISO
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
via the lower leve
Windows Runtime C++ Template Library
(WRL). However, WRL is also superseded by
C++/WinRT C++/WinRT is a C++ library for Microsoft's Windows Runtime platform, designed to provide access to modern Windows APIs. C++/WinRT is provided as a standard C++17 header file library, unlike C++/CX, which is an extension to C++ and requires a recen ...
.


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 C++11 is a version of the ISO/ IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions b ...
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 In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding). Whe ...
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 Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of treatment or landfill. Waste collection also includes the curbside collection of recyclabl ...
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 definition to be split across multiple files, mainly to enable the
XAML Extensible Application Markup Language (XAML ) is a declarative XML-based language that Microsoft developed for initializing structured values and objects. It is available under Microsoft's Open Specification Promise. XAML is used extensively ...
graphical user interface The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inst ...
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 language The domain name net is a generic top-level domain (gTLD) used in the Domain Name System of the Internet. The name is derived from the word ''network'', indicating it was originally intended for organizations involved in networking technologies ...
s like C# have had this feature for many years. Partial classes have not yet made it into the
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
standard and cannot therefore be used, even in
C++20 C20 or C-20 may refer to: Science and technology * Carbon-20 (C-20 or 20C), an isotope of carbon * C20, the smallest possible fullerene (a carbon molecule) * C20 (engineering), a mix of concrete that has a compressive strength of 20 newtons per sq ...
. 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 ; 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 ; 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() Platform::String^ foo::GetName


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++ template Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. The C++ Standa ...
s which are compile-time constructs. Both are supported by the compiler and can be combined. generic public ref class bag ;


Metadata

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 The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
. Because of this, code can be shared across C++/CX,
CLI languages CLI languages are computer programming languages that are used to produce libraries and programs that conform to the Common Language Infrastructure (CLI) specifications. With some notable exceptions, most CLI languages compile entirely to the Comm ...
, 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 The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. ISO/IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C ยง7'' Starting from the original ANSI C standard, it was ...
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

*
Windows Runtime Windows Runtime (WinRT) is a platform-agnostic component and application architecture first introduced in Windows 8 and Windows Server 2012 in 2012. It is implemented in C++ and officially supports development in C++ (via C++/WinRT, C++/CX or ...


References


External links


Inside the C++/CX Design - Visual C++ Team Blog

GoingNative 3: The C++/CX Episode with Marian Luparu - Channel 9

WinRT and winmd Files - Marius Bancila's Blog

Visual C++ Language Reference (C++/CX)
{{DEFAULTSORT:C++ CX C++ C++ programming language family C++ compilers