4

Here is my code:

#include <stdio.h>
int main()
{   
    enum C {green = 5, red};
    enum CC {blue, yellow} cc = green;
    printf("%i\n", cc);
    return 0;
}

It does compile, and produce console output 5.

cppreference.com says that "An enumerated type is a distinct type whose value is restricted to one of several explicitly named constants (enumeration constants)". I am really confused.

By the way, the compiler I'm using is gcc version 4.8.1.

Carousel
  • 728
  • 4
  • 13

4 Answers4

4

You're reading C++ documentation to learn about C? C and C++ are not the same language.

In C, an enum is effectively an integer type and you're allowed to handle it as such. This means that you can assign an integer to it which may not be one of the prescribed values.

However it's considered bad practice to do so and some compilers or static code checkers have options to warn about such practices.

In C++ on the other hand, enums are distinct types in their own right and you are not allowed to do this in C++, at least not implicitly. Compiling this as C++ results in a compilation error.

Jens
  • 69,818
  • 15
  • 125
  • 179
skyking
  • 13,817
  • 1
  • 35
  • 57
  • 2
    Despite the name, the linked web site contains information for both languages. The linked page contains information for C (not C++). – Klas Lindbäck Jan 27 '16 at 09:01
  • The webiste cppreference.com has both c and c++ references. They are completely separated in contents. In C, if an `enum` can have any integer as its value other than one of those declared as its enumerators, it seems that type `enum` is almost identical to integer. From the literal meaning of enumeration, I thought the values of enumeration objects are restricted to an integer set. – Carousel Jan 27 '16 at 09:19
  • @Carousel I think it is slightly underspecified. But if we take the interpretation that only named members are allowed, then the following would not work: `enum flags { FLAG_A = 0x01, FLAG_B = 0x02 };` ... `enum flags f = FLAG_A | FLAG_B;` . However that is a common use of enums and I think the standard committee would have intended that this usage be supported. – M.M Jan 27 '16 at 09:30
3

From std 6.7.2.3 (enumeration content) - the underlying type of enum is implementation-defined, but a variable of type enum can be assign any number of the underlying type (which is at the very least char).

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration. The enumerated type is incomplete until immediately after the } that terminates the list of enumerator declarations, and complete thereafter.

The compiler is NOT required to check that what your store in a enum variable is a valid value for that enumeration or not.

artm
  • 17,291
  • 6
  • 38
  • 54
2

Yes, C compiler allow you to do this.

You also can do this:

#include <stdio.h>

int main()
{   
    enum C {green = 5, red};
    enum C  cc = 7;
    printf("%i\n", cc);

    return 0;
}

CPP is really different with C, so don't apply CPP reference to C.

Van Tr
  • 5,889
  • 2
  • 20
  • 44
1

In C there is a contradiction. All enumerations are different types. However their enumerators are integer constants of type int.

There is no check whether a given integer value corresponds to the set of specified enumerators in an enumeration.

In C++ the contradiction was eliminated. In C++ each enumerator has the same type as its enumeration where the enumerator is declared and moreover there is no implicit conversion from type int to the type of the given enumeration.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335