Main Content

AUTOSAR C++14 Rule A7-2-2

Enumeration underlying type shall be explicitly defined

Description

Rule Definition

Enumeration underlying type shall be explicitly defined.

Rationale

In an unscoped enumeration declaration such as:

enum someEnum : type { ... }

if : type is omitted, the underlying type is implementation-defined (with the only requirement that the type must accommodate all the enumeration values). Not declaring an underlying type explicitly results in implementation-defined behavior.

In a scoped enumeration declaration such as:

enum class someEnum : type { ... }

if : type is omitted, the underlying type is int. If an enumeration value exceeds the values allowed for int, you see compilation errors.

For both unscoped and scoped enumerations, declare the underlying type explicitly to avoid implementation-defined behavior or compilation errors.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstdint>

enum E1 { //Noncompliant unscoped enum
    E10,
    E11,
    E12
};

enum E2 : std::uint8_t { //Compliant unscoped enum
    E20,
    E21,
    E22
};

enum class E3 { //Noncompliant scoped enum
    E30,
    E31,
    E32
};

enum class E4 : std::uint8_t { //Compliant scoped enum
    E40,
    E41,
    E42
};

In this example, the code is noncompliant when the underlying types of the enumerations are omitted.

Check Information

Group: Declaration
Category: Required, Automated

Version History

Introduced in R2019b