AutosarC++17_03-A9.6.1

Bit-fields shall be either unsigned integral, or enumeration (with underlying type of unsigned integral type)

Required inputs: IR

Rationale

Explicitly declaring a bit-field unsigned prevents unexpected sign extension, overflows and implementation-defined behavior.

Note that if a bit-field has enumeration type, then the enumeration base needs to be declared of an explicitly unsigned type.

Example
// $Id: A9-6-1.cpp 271715 2017-03-23 10:13:51Z piotr.tanski $
#include <cstdint>
enum class E1 : std::uint8_t { E11, E12, E13 };
enum class E2 : std::int16_t { E21, E22, E23 };
enum class E3 { E31, E32, E33 };
enum E4 { E41, E42, E43 };
class C {
public:
std::int32_t a : 2;  // Non-compliant - signed integral type
std::uint8_t b : 2U; // Compliant
bool c : 1;    // Non-compliant - it is implementation-defined whether bool is
         // signed or unsigned
char d : 2;    // Non-compliant
wchar_t e : 2; // Non-compliant
E1 f1 : 2;     // Compliant
E2 f2 : 2;     // Non-compliant - E2 enum class underlying type is signed
         // int
E3 f3 : 2;     // Non-compliant - E3 enum class does not explicitly define
         // underlying type
E4 f4 : 2; // Non-compliant - E4 enum does not explicitly define underlying
     // type
};
void fn() noexcept {
C c;
c.f1 = E1::E11;
}
See Also
  • JSF December 2005 [7]: AV Rule 154 Bit-fields shall have explicitly unsigned integral or enumeration types only.
  • HIC++ v4.0 [8]: 9.2.1 Declare bit-fields with an explicitly unsigned integral or enumeration type.

Possible Messages

Key

Text

Severity

Disabled

invalid_bitfield

Bit-fields shall be either unsigned integral, or enumeration (with underlying type of unsigned integral type).

None

False

Options

allow_bool_bitfields

allow_bool_bitfields : bool = False

Although a bool is not guaranteed to be of sizeof == 1, the C++ standard guarantees, that a bool value can be stored in a bit-field of any size and that a true/false value stored in a bit-field of any size will compare equal with that original value.

Setting this option to true will not report bit-fields of type bool.