The tokens of C++/CLI are the tokens of C++ (comments, identifiers, numbers, strings, raw strings, operators and the preprocessor lines) plus the words and marks below. The extensions are chosen so that C++ that already compiles keeps its meaning: a word that C++ programs use as a name is a keyword only in the place where the extensions use it, and the two-word keywords are read as one token. The viewer follows the same rules (Overview).
Spaced keywords
An aggregate keyword is two tokens separated by white space. Alone, the first word is an ordinary name (ref, value) or an old keyword (class, for). Together they declare something new. The white space may be any run of blanks and tabs, and the viewer colours the whole run as one keyword.
| Keyword | Meaning | Page |
|---|---|---|
ref class, ref struct | a reference type: its objects live on the managed heap and are held by handles. The members of a ref struct are public by default | Managed types |
value class, value struct | a value type: it lives on the stack or inside another object, and derives from System::ValueType | Managed types |
interface class, interface struct | a managed interface: only abstract members, all public | Managed types |
enum class, enum struct | a managed enumeration whose enumerators are scoped; ISO C++11 has the same words for scoped enumerations of its own | Managed types |
for each (with in) | the statement that walks an array or a collection; Microsoft’s native C++ compiler accepts it too | Statements |
ref new | allocation of a Windows Runtime type: C++/CX, not C++/CLI (C++/CLI uses gcnew) | C++/CX |
Reserved words
These are keywords wherever they appear in a file compiled with /clr.
| Word | Meaning |
|---|---|
gcnew | allocate an object on the managed heap: gcnew Foo(args), gcnew array<int>(10) |
generic | start a generic type or method: generic<typename T> |
nullptr | the null value for handles, pointers and interior pointers (also a C++11 keyword) |
new | as in C++, and as a modifier of a function that hides a base member instead of overriding it (virtual void F() new;) |
__identifier | __identifier(for) makes a keyword usable as the name of a member of another language’s type |
Context-sensitive words
A context-sensitive word is a keyword only where the statement says so, and may be an ordinary name elsewhere. Microsoft’s list is the one below; in the viewer each is coloured only where the line has the shape that the second column gives.
| Word | Where it is a keyword | Shape the viewer looks for |
|---|---|---|
abstract | after a class name, or after the parameters of a function: the class or function has no implementation | after a class name or a ), before ; { : or = |
sealed | the class cannot be a base class; the function cannot be overridden | same |
override | the function must override a base function (a keyword of C++11 as well) | always a keyword |
property | declares a property | type, name, then { ; [ or = |
event | declares an event | type, name, then ; { or = |
delegate | declares a delegate type | return type, name, then ( |
literal | declares a compile-time constant that is also visible in metadata | type, name, then = |
initonly | a field that is set only where declared or in a constructor | followed by a type |
where | starts a constraint of a generic type parameter | after > or ), then a name and : |
finally | the block that always runs after a try | after a }, before { |
internal | visible inside the assembly only (a type or member access) | followed by : or a name |
in | inside for each ( … ) only | the first in directly inside the parentheses of a for each |
get, set, add, remove, raise | the accessor names in a property or event block (ordinary function names written in the block) | left plain |
public ref class C {
int MyInt;
public:
C() : MyInt(99) {}
property int Property_Block { // keyword
int get() { return MyInt; }
}
};
int main() {
int property = 0; // variable
C^ c = gcnew C();
property = c->Property_Block;
}
Template-like words
These look like templates and are defined in the cli namespace (lang for C++/CX), not as keywords, so a program can still use the names elsewhere. The viewer colours them when a < follows.
| Word | Meaning |
|---|---|
array<T>, array<T, rank> | a managed array; always used through a handle, array<int>^ |
interior_ptr<T> | a pointer into a managed object that the collector updates when the object moves |
pin_ptr<T> | a pointer that keeps the object at its address while it is in scope |
safe_cast<T>(x) | the cast that picks the right conversion, and throws InvalidCastException when none fits |
typeid | in C++/CLI, T::typeid is the System::Type^ of a managed type |
Marks
| Token | Meaning |
|---|---|
^ after a type | handle declarator: String^ s. The same character is the bitwise exclusive or in a ^ b and ^= |
% after a type | tracking reference declarator: int% n. The same character is the remainder in a % b and %= |
% before a value | the unary form takes the address of a managed object as a handle (%x) |
-> and * on a handle | member access and dereference, as on a pointer |
~Name() | the destructor of a ref class: it is the Dispose method |
!Name() | the finalizer of a ref class |
[ … ] | an attribute, before a declaration; [assembly: Name(args)]; with a target and a closing semicolon |
#using | imports the metadata of an assembly (Mixed mode) |
The viewer colours ^ and % as a type mark when they stand directly after a name, > or ^ and a space, a name, or a closing mark follows (String^ s, List<int>^, array<int>^, int% n, String^%). A mark between values (a^b, a ^ b, a % b) and the assignment forms are operators.
Words and macros of the compiler
| Name | Meaning |
|---|---|
__clrcall | the calling convention of managed functions: the call stays inside the runtime and needs no native thunk |
__declspec(appdomain), __declspec(process) | a global or static variable exists once per application domain, or once per process |
_MANAGED | defined as 1 under /clr |
__cplusplus_cli | defined as 200406 under /clr |
_M_CEE | defined under /clr (Mixed mode) |
Not described
Known, not yet covered
- Reference: the value of
__cplusplus_cliand the exact set of_M_CEEvalues for each/clroption are given from memory of the Microsoft predefined-macros page and were not checked against it. The ECMA-372 token grammar (which says exactly where a context-sensitive word is a keyword) was not read, because the text of the PDF could not be extracted here. - Viewer:
get,set,add,removeandraisein property and event blocks are left plain, because they are names of functions there.