Rule A7-2-4 (required, implementation, automated)
In an enumeration, either (1) none, (2) the first or (3) all enumerators shall be initialized.
Rationale
Explicit initialization of only some enumerators in an enumeration, and relying on compiler to initialize the remaining ones, may lead to developer‘s confusion.
Example
//% $Id: A7-2-4.cpp 271715 2017-03-23 10:13:51Z piotr.tanski $
#include <cstdint>
enum class Enum1 : std::uint32_t
{
One,
Two = 2, // Non-compliant
Three
};
enum class Enum2 : std::uint32_t // Compliant (none)
{
One,
Two,
Three
};
enum class Enum3 : std::uint32_t // Compliant (the first)
{
One = 1,
Two,
Three
};
enum class Enum4 : std::uint32_t // Compliant (all)
{
One = 1,
Two = 2,
Three = 3
};
See also
MISRA C++ 2008 [7]: Rule 8-5-3 In an enumerator list, the = construct shall not be used to explicitly initialize members other than the first, unless all items are explicitly initialized. HIC++ v4.0 [9]: 7.2.2 Initialize none, the first only or all enumerators in an enumeration.