C++/CX

Last updated

C++/CX(C++ component extensions) is a language projection for Microsoft's Windows Runtime platform. It takes the form of a language extension for C++ compilers, and it enables C++ programmers to write programs that call Windows Runtime (WinRT) APIs. C++/CX is superseded by the C++/WinRT 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. [1]

Contents

The language extensions borrow syntax from C++/CLI but target the Windows Runtime Universal Windows Platform native code instead of the Common Language Runtime and managed code. It brings a set of syntax and library abstractions that project COM's WRL subset-based WinRT programming model in a way that is intuitive to C++/CLI managed extensions' coders.

It is possible to call the Windows Runtime from native ISO C++ via the lower level Windows Runtime C++ Template Library (WRL). However, WRL is also superseded by C++/WinRT. [1]

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=refnewFoo();

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.

publicrefclassMyClass{};
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 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, even in C++20.

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 oncepartialrefclassfoo{private:intid_;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"refclassfoo{public:intGetId();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"intfoo::GetId(){returnid_;}Platform::String^foo::GetName{returnname_;}

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<typenameT>publicrefclassbag{propertyTItem;};

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. 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

Related Research Articles

Common Intermediate Language (CIL), formerly called Microsoft Intermediate Language (MSIL) or Intermediate Language (IL), is the intermediate language binary instruction set defined within the Common Language Infrastructure (CLI) specification. CIL instructions are executed by a CIL-compatible runtime environment such as the Common Language Runtime. Languages which target the CLI compile to CIL. CIL is object-oriented, stack-based bytecode. Runtimes typically just-in-time compile CIL instructions into native code.

The C preprocessor is the macro preprocessor for several computer programming languages, such as C, Objective-C, C++, and a variety of Fortran languages. The preprocessor provides inclusion of header files, macro expansions, conditional compilation, and line control.

This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.

In computer programming, a directive or pragma is a language construct that specifies how a compiler should process its input. Depending on the programming language, directives may or may not be part of the grammar of the language and may vary from compiler to compiler. They can be processed by a preprocessor to specify compiler behavior, or function as a form of in-band parameterization.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

In computer programming, thread-local storage (TLS) is a memory management method that uses static or global memory local to a thread. The concept allows storage of data that appears to be global in a system with separate threads.

Managed Extensions for C++ or Managed C++ is a 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.

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 to supersede Managed Extensions for C++. In December 2005, Ecma International published C++/CLI specifications as the ECMA-372 standard.

<span class="mw-page-title-main">C Sharp (programming language)</span> Programming language

C# is a general-purpose high-level programming language supporting multiple paradigms. C# encompasses static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.

C# and Visual Basic (.NET) are the two main programming languages used to program on the .NET framework.

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 by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

<span class="mw-page-title-main">Vala (programming language)</span> Programming language

Vala is an object-oriented programming language with a self-hosting compiler that generates C code and uses the GObject system.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure (.NET/Mono). It offers functional, object-oriented, aspect-oriented, reflective and imperative features. It has a simple C#-like syntax and a powerful metaprogramming system.

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++, Rust/WinRT, Python/WinRT, JavaScript-TypeScript, and the managed code languages C# and Visual Basic (.NET) (VB.NET).

The following outline is provided as an overview of and topical guide to C++:

Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard programming language used, supported, and promoted by Apple for developing macOS and iOS applications until the introduction of the Swift programming language in 2014.

Swift is a high-level general-purpose, multi-paradigm, compiled programming language created by Chris Lattner in 2010 for Apple Inc. and maintained by the open-source community. Swift compiles to machine code and uses an LLVM-based compiler. Swift was first released in June 2014 and the Swift toolchain has shipped in Xcode since version 6, released in 2014.

<span class="mw-page-title-main">Nim (programming language)</span> Programming language

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level system programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.

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 recent version of Microsoft Visual C++.

References

  1. 1 2 Introduction to C++/WinRT docs.microsoft.com