Home > Software engineering >  What is the difference between an "enumeration type" and an "enumerated type" in
What is the difference between an "enumeration type" and an "enumerated type" in

Time:11-03

The Standard "apparently" defines "enumeration type" in [dcl.enum]/1. The term "enumerated type" is defined here.

CodePudding user response:

An actual enumeration consists of two parts: the type and the enumerators defined by that type.

An "enumerated type" consists of three parts: the enumeration type, the enumerators of that enumeration type, and the elements of the enumerated type. The elements are names which are of the enumeration type and resolve to one of the enumerators within that enumeration. Now, you may notice that every enumeration is an "enumerated type", so long as its elements match the names required by the enumerated type's requirements.

But they don't have to actually be one of the enumerators. You can see that in the example you linked to:

enum enumerated { V0, V1, V2, V3, … };

inline const enumerated C0(V0);
inline const enumerated C1(V1);
inline const enumerated C2(V2);
inline const enumerated C3(V3);
...

This is one way to implement enumerated types. The standard would require the elements of the enumerated type to be C0, C1, etc., and the enumerated type could hide the actual enumerators of enumerated (ie V0, V1, etc).

The main reason for this concept is the next section: the definition of a "bitmask type". Bitmasks are enumerated types (as defined above), but they have extra functionality to perform bit-wise operations (which basic enumerations don't have by default).

CodePudding user response:

The first link (http://eel.is/c draft/dcl.enum#1) explains what an enumeration type in C is. This is where C officially defines it.

The other link (http://eel.is/c draft/enumerated.types) is about the C standard library. This link explains how you have to read the description of the standard library, that comes in the subsequent chapters.

For example in chapter 29 there is the description of a type ios_base::seekdir:

The type seekdir is an enumerated type (16.3.3.3.3) that contains the elements indicated in Table 120.

And then they explain that there are three possible values beg, cur end. This may be intuitive enough for most people who want to use that library of chapter 29. But as this enumeration type is part of the public interface of that library, others may think, that just saying there are three possible values is not precise enough. The reader must precisely know what comes out of a function that returns an seekdir.

And therefore your second link clearly explains what they mean, when saying "it is an enumerated type that contains these elements."

The wording "synonym of enumeration" means that exactly the same thing (the same bits and bytes), that is provided by an enumeration type, can be also be provided by other types. If for example you have a function that returns an enum with one=1, two=2, three=3, you are free to see the returned value as an element of that enum type or just as an integer. At library level there is no difference between both interpretations of the bits that are coming out of such a function. The interpretation comes only when you include a header file where, the function is precisely defined together with its return type.

  • Related