Share via

Facebook x.com LinkedIn Email

literal (Visual C++)

A variable (data member) marked as literal in a /clr compilation is the native equivalent of a static const variable.

Remarks

A data member marked as literalmust be initialized when declared and the value must be a constant integral, enum, or string type. Conversion from the type of the initialization expression to the type of the static const data-member must not require a user-defined conversion.

No memory is allocated for the literal field at runtime; the compiler only inserts its value in the metadata for the class.

A variable marked static const will not be available in metadata to other compilers.

For more information, see Static (C++) and const (C++).

literal is a context-sensitive keyword. See Context-Sensitive Keywords for more information.

Example

This example shows that a literal variable implies static.

// mcppv2_literal.cpp
// compile with: /clr
ref struct X {
 literal int i = 4;
};
int main() {
 int value = X::i;
}

The following sample shows the affect of literal in metadata:

// mcppv2_literal2.cpp
// compile with: /clr /LD
public ref struct A {
 literal int lit = 0;
 static const int sc = 1;
};

Notice the difference in the metadata for sc and lit: the modopt directive is applied to sc, meaning it can be ignored by other compilers.

.field public static int32 modopt([mscorlib]System.Runtime.CompilerServices.IsConst) sc = int32(0x0000000A)
.field public static literal int32 lit = int32(0x0000000A)

The following sample, authored in C#, references the metadata created in the previous sample and shows the affect of literal and static const variables:

// mcppv2_literal3.cs
// compile with: /reference:mcppv2_literal2.dll
// A C# program
class B {
 public static void Main() {
 // OK
 System.Console.WriteLine(A.lit);
 System.Console.WriteLine(A.sc);
 // C# does not enforce C++ const
 A.sc = 9;
 System.Console.WriteLine(A.sc);
 // C# enforces const for a literal
 A.lit = 9; // CS0131
 // you can assign a C++ literal variable to a C# const variable
 const int i = A.lit;
 System.Console.WriteLine(i);
 // but you cannot assign a C++ static const variable
 // to a C# const variable
 const int j = A.sc; // CS0133
 System.Console.WriteLine(j);
 }
}

Requirements

Compiler option: /clr

See Also

Concepts

Language Features for Targeting the CLR


  • Last updated on 2013年02月01日