Skip to content

Latest commit

 

History

History
52 lines (37 loc) · 1.41 KB

enumerations.md

File metadata and controls

52 lines (37 loc) · 1.41 KB

<<< Table of contents

Enumerations

  1. Enumeration types, as well as the contained enumerators, shall begin with an uppercase letter. Avoid writing the enumerators in all-uppercase notation.

    Example:

    enum class Tide
    {
        low,
        high
    };

    Not:

    enum class Tide
    {
        LOW,
        HIGH
    };
  2. Use scoped enums (enum class) instead of old-style enums (enum), since enum classes do not pollute the surrounding scope. Convert existing code using enum to make use of enum class instead.

    • Scoped enums are not visible from the surrounding scope, greatly reducing the possibility of name clashes.

    • Scoped enums do not implicitly convert to int, whereas conventional enums do (which is often unwanted). Scoped enums can still be explicitly cast to an integer type.

    • The underlying type of a scoped enum can be explicitly specified. This also enables forward declaration of scoped enums.

  3. Consider providing an explicit type specification for an enum class, in order to guarantee the size of the enumeration, and to enable forward declaration. By default, the underlying type is int.

    Example:

    enum class Difficulty : unsigned char
    {
        easy,
        medium,
        hard
    }

<<< Table of contents