AutosarC++17_03-A7.2.2

Enumeration underlying base type shall be explicitly defined

Required inputs: IR

Rationale

The enumeration underlying type is implementation-defined, with the only restriction that the type must be able to represent the enumeration values. Although scoped enum will implicitly define an underlying type of int, the underlying base type of enumeration should always be explicitly defined with a type that will be large enough to store all enumerators.

Example
// $Id: A7-2-2.cpp 271715 2017-03-23 10:13:51Z piotr.tanski $
#include <cstdint>
enum class E1 // Non-compliant
{ E10,
E11,
E12 };
enum class E2 : std::uint8_t // Compliant
{ E20,
E21,
E22 };
enum E3 // Non-compliant
{ E30,
E31,
E32 };
enum E4 : std::uint8_t // Compliant - violating another rule
{ E40,
E41,
E42 };
enum class E5 : std::uint8_t // Non-compliant - will not compile
{ E50 = 255,
// E5_1, // E5_1 = 256 which is outside of range of underlying type
// std::uint8_t
// - compilation error
// E5_2 // E5_2 = 257 which is outside of range of underlying type
// std::uint8_t
// - compilation error
};
See Also
  • HIC++ v4.0 [8]: 7.2.1 Use an explicit enumeration base and ensure that it is large enough to store all enumerators

Possible Messages

Key

Text

Severity

Disabled

unbased_enum

Enumeration underlying base type shall be explicitly defined.

None

False

Options

allow_enumerations_inside_extern_c

allow_enumerations_inside_extern_c : bool = False

Whether enumerations defined inside an extern C block should be reported.
 

allow_enumerations_only_using_defaults

allow_enumerations_only_using_defaults : bool = False

Whether enumeration that only use default values as their enumerators should be reported.