Rule A7-2-2 (required, implementation, automated)
Enumeration underlying base type shall be explicitly defined.
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 [9]: 7.2.1 Use an explicit enumeration base and ensure that it is large enough to store all enumerators