Udostępnij za pośrednictwem


Static Const Int Linkage Is No Longer Literal

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Static Const Int Linkage Is No Longer Literal.

Declaration of a constant member of a class has changed from Managed Extensions for C++ to Visual C++.

Although static const integral members are still supported, their linkage attribute has changed. Their former linkage attribute is now carried in a literal integral member. For example, consider the following Managed Extensions class:

public __gc class Constants {  
public:  
   static const int LOG_DEBUG = 4;  
};  

This generates the following underlying CIL attributes for the field (note the literal attribute):

.field public static literal int32   
modopt
([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier) STANDARD_CLIENT_PRX = int32
(0x00000004)  

While this still compiles under the new syntax:

public ref class Constants {  
public:  
   static const int LOG_DEBUG = 4;  
};  

it no longer emits the literal attribute, and therefore is not viewed as a constant by the CLR runtime:

.field public static int32 modopt
([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier) STANDARD_CLIENT_PRX = int32
(0x00000004)  

In order to have the same inter-language literal attribute, the declaration should be changed to the newly supported literal data member, as follows,

public ref class Constants {  
public:  
   literal int LOG_DEBUG = 4;  
};  

See Also

Member Declarations within a Class or Interface (C++/CLI)
literal