Component Extensions for Runtime Platforms
Visual C++ provides language extensions to help you program against runtime platforms. By using Visual C++ component extensions (C++/CX), you can program Windows Store apps and components that compile to native code. Although you can create Windows Store apps by programming directly against the Windows Runtime COM interfaces, by using C++/CX, you can work with constructors, exceptions, and other modern C++ programming idioms. To enable C++ programming in a managed execution environment on the .NET platform, you can use C++/CLI.
Two runtimes, one set of extensions
C++/CX is a subset of C++/CLI. For extensions that are common to C++/CX and C++/CLI, the semantics depend on whether you are targeting the common language runtime (CLR) or the Windows Runtime. To compile your app to run on the Windows Runtime, specify the /ZW compiler option. To compile it to run on the CLR, specify the /clr compiler option. These switches are set automatically when you use Visual Studio to create a project.
For more information about how to create Windows Store apps in C++, see Roadmap for Windows Store apps using C++.
C++/CLI extends the ISO/ANSI C++ standard, and is defined under the Ecma C++/CLI Standard. For more information, see .NET Programming in Visual C++.
Data Type Keywords
The language extensions include aggregate keywords, which are keywords that consist of two tokens separated by white space. The tokens might have one meaning when they are used separately, and another meaning when they are used together. For example, the word "ref" is an ordinary identifier, and the word "class" is a keyword that declares a native class. But when these words are combined to form ref class, the resulting aggregate keyword declares an entity that is known as a runtime class.
The extensions also include context-sensitive keywords. A keyword is treated as context-sensitive depending on the kind of statement that contains it, and its placement in that statement. For example, the token "property" can be an identifier, or it can declare a special kind of public class member.
The following table lists keywords in the C++ language extension.
Keyword |
Context sensitive |
Purpose |
Reference |
---|---|---|---|
ref class ref struct |
No |
Declares a class. |
|
value class value struct |
No |
Declares a value class. |
|
interface class interface struct |
No |
Declares an interface. |
|
enum class enum struct |
No |
Declares an enumeration. |
|
property |
Yes |
Declares a property. |
|
delegate |
Yes |
Declares a delegate. |
|
event |
Yes |
Declares an event. |
Override Specifiers
You can use the following keywords to qualify override behavior for derivation. Although the new keyword is not an extension of C++, it is listed here because it can be used in an additional context. Some specifiers are also valid for native programming. For more information, see How to: Declare Override Specifiers in Native Compilations (C++/CLI).
Keyword |
Context Sensitive |
Purpose |
Reference |
---|---|---|---|
abstract |
Yes |
Indicates that functions or classes are abstract. |
|
new |
No |
Indicates that a function is not an override of a base class version. |
|
override |
Yes |
Indicates that a method must be an override of a base-class version. |
|
sealed |
Yes |
Prevents classes from being used as base classes. |
Keywords for Generics
The following keywords have been added to support generic types. For more information, see Generics (C++ Component Extensions).
Keyword |
Context sensitive |
Purpose |
---|---|---|
generic |
No |
Declares a generic type. |
where |
Yes |
Specifies the constraints that are applied to a generic type parameter. |
Miscellaneous Keywords
The following keywords have been added to the C++ extensions.
Keyword |
Context sensitive |
Purpose |
Reference |
---|---|---|---|
finally |
Yes |
Indicates default exception handlings behavior. |
|
for each, in |
No |
Enumerates elements of a collection. |
|
gcnew |
No |
Allocates types on the garbage-collected heap. Use instead of new and delete. |
|
ref new |
Yes |
Allocates a Windows Runtime type. Use instead of new and delete. |
|
initonly |
Yes |
Indicates that a member can only be initialized at declaration or in a static constructor. |
|
literal |
Yes |
Creates a literal variable. |
|
nullptr |
No |
Indicates that a handle or pointer does not point at an object. |
Template Constructs
The following language constructs are implemented as templates, instead of as keywords. If you specify the /ZW compiler option, they are defined in the lang namespace. If you specify the /clr compiler option, they are defined in the cli namespace.
Keyword |
Purpose |
Reference |
---|---|---|
array |
Declares an array. |
|
interior_ptr |
(CLR only) Points to data in a reference type. |
|
pin_ptr |
(CLR only) Points to CLR reference types to temporarily suppress the garbage-collection system. |
|
safe_cast |
Determines and executes the optimal casting method for a runtime type. |
|
typeid |
(CLR only) Retrieves a Type object that describes the given type or object. |
Declarators
The following type declarators instruct the runtime to automatically manage the lifetime and deletion of allocated objects.
Operator |
Purpose |
Reference |
---|---|---|
^ |
Declares a handle to an object; that is, a pointer to a Windows Runtime or CLR object that is automatically deleted when it is no longer usable. |
|
% |
Declares a tracking reference; that is, a reference to a Windows Runtime or CLR object that is automatically deleted when it is no longer usable. |
Additional Constructs and Related Topics
This section lists additional programming constructs, and topics that pertain to the CLR.
Topic |
Description |
---|---|
(Windows Runtime and CLR) Enables the use of keywords as identifiers. |
|
(Windows Runtime and CLR) Enables a function to take a variable number of arguments. |
|
Lists the CLR types that are used in place of C++ integral types. |
|
appdomain__declspec modifier |
__declspec modifier that mandates that static and global variables exist per appdomain. |
Describes how C-style casts are interpreted. |
|
__clrcall calling convention |
Indicates the CLR-compliant calling convention. |
__cplusplus_cli |
|
Describes how to define your own CLR attributes. |
|
Provides an overview of exception handling. |
|
Demonstrates how member functions can override arbitrary members. |
|
Discusses how a client assembly can access all types in an assembly component. |
|
Demonstrates the conditions in which values types are boxed. |
|
Discusses how to detect characteristics of types at compile time. |
|
managed, unmanaged pragmas |
Demonstrates how managed and unmanaged functions can co-exist in the same module. |
process__declspec modifier |
__declspec modifier that mandates that static and global variables exist per process. |
Demonstrates the CLR version of run-time type information. |
|
Discusses compiler conversion of string literals to String. |
|
Enables the movement of a type in a shipping assembly to another assembly so that client code does not have to be recompiled. |
|
Demonstrates user-defined attributes. |
|
Imports external assemblies. |
|
Explains XML-based code documentation by using /doc (Process Documentation Comments) (C/C++) |