literal (C++ Component Extensions)


The latest version of this topic can be found at literal (C++ Component Extensions).

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

All Platforms


(There are no remarks for this language feature that apply to all runtimes.)

Windows Runtime


(There are no remarks for this language feature that apply to only the Windows Runtime.)


Compiler option: /ZW

Common Language Runtime


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

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


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
.field public static literal int32 lit = int32


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  
      // C# does not enforce C++ const = 9;  
      // 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;  
      // but you cannot assign a C++ static const variable  
      // to a C# const variable  
      const int j =;   // CS0133  


Compiler option: /clr

See Also

Component Extensions for Runtime Platforms