AutosarC++18_10-A7.2.4¶

In an enumeration, either (1) none, (2) the first or (3) all enumerators shall be initialized

Required inputs: IR

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.

Possible Messages

Key

Text

Severity

Disabled

incomplete_enum_init

Do not initialize enumerators other than the first, or initialize all

None

False

Options