What is an enumeration in C.

Strictly typed enumerations (C ++ 11)

Go Up to C ++ 11 Features Index

BCC32 introduces area-dependent enumerations. In addition, existing enumerations have been expanded with the underlying types and explicit scope assignment. This C ++ 11 feature was added to BCC32.

Area-dependent lists are generally characterized as follows:

  • Enumerators are in the scope of their enumeration.
  • Enumerators and enumerations are not implicitly converted to int (as with "simple" enumerators and enumerations).
  • Enumerations and their enumerators can be of an underlying type.

declaration

You declare a range-dependent enumeration by specifying the enumeration class (enum class) or the enumeration structure (enum struct). For example:

enum class A {A1, A2, A3 = 50, A4 / * = 51 * /};

No implicit cast

With range-dependent enumerations, there is no implicit conversion to or from integer values.

Underlying type

In the case of area-dependent enumerations, the underlying type is clearly specified (the default is int). You can specify the underlying type of the enumeration and all enumerators as follows: After the name of the enumeration, specify the type (you can specify all integral types except wchar_t). The following example declares an enumeration with the underlying type unsigned long:

enum class A: unsigned long {A1 = 1, A2 = 2, Abig = 0xFFFFFFFOU};

Scope mapping

Scope-dependent enumeration introduces its own scope. The names of the enumerators are in the scope of the enumeration and are not included in the surrounding scope. For example:

enum class A {A1, A2, A3 = 100, A4 / * = 101 * /}; A a1 = A1; // error A a2 = A :: A2; // OK-scope specified

Changes to existing lists

Existing lists have been expanded as follows:

  • You can now specify the underlying type of each enumeration, just like with range-dependent enumerations (by adding: type to the declaration).
  • Existing enumerations, just like area-dependent enumerations, now introduce their own areas of validity. The names of the enumerators are defined in the scope of the enumeration and are also included in the surrounding scope.

For example:

enum B {B1, B2, B3 = 100, B4 / * = 101 * /}; B b1 = B1; // ok B b2 = B :: B2; // OK

Examples

The following examples demonstrate the following:

  • The method of calling scope-dependent enumerators
  • The fact that enum class (and enum struct) cannot be specified with extended type identifiers
enum class E {a, b}; enum E x1 = E :: a; // OK enum class E x2 = E :: a; // illegal

See also