Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSVC const enum type

Tags:

c++

const enum Alpha{
    X=9,
    Y=5,
    Z=2
}p;
int main(){
    enum Alpha a,b;
    a= X;
    b= Z;

    p = X;
    p = Y;

    printf("%d",a+b-p); 
    return 0; 
}

Why is p = X and p = Y allowed in MSVC compiler? This code outputs 6. Shouldn't a const value be assigned at initialization and never again?

like image 515
Johnny Pauling Avatar asked Feb 17 '13 16:02

Johnny Pauling


People also ask

What is an Unscoped enum type?

In an unscoped enum, the scope is the surrounding scope; in a scoped enum, the scope is the enum-list itself. In a scoped enum, the list may be empty, which in effect defines a new integral type. class. By using this keyword in the declaration, you specify the enum is scoped, and an identifier must be provided.

What type is enum C++?

In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants.

Is enum constant in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What are enum types?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


Video Answer


2 Answers

That is a bug in the compiler itself. End of the story.

In fact, your little code shows two bugs in the compiler. The first bug is here itself:

const enum Alpha{
    X=9,
    Y=5,
    Z=2
}p;    //declaration of p is ill-formed!

The declaration of p is ill-formed, and thus the compiler should reject this code, because p is declared const but left uninitialized. A const scalar (and pod) type must be initialized in order to be well-formed:

const Alpha q;      //ill-formed (same case is with p in your code)
const Alpha r = X;  //well-formed

For detailed and extensive explanation, see this:

  • Why do const variables have to be initialized right away?
like image 176
Nawaz Avatar answered Sep 30 '22 09:09

Nawaz


Looks like a bug, indeed.

First of all, global const objects must be initialized when defined, and default-initialization is not an option for enumeration types. According to Paragraph 8.5/6 of the C++11 Standard:

To default-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

— if T is an array type, each element is default-initialized;

— otherwise, no initialization is performed.

If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

Secondly, a const object cannot be assigned after initialization.

like image 43
Andy Prowl Avatar answered Sep 30 '22 08:09

Andy Prowl