9.2 Bit-fields

9.2.1 Declare bit-fields with an explicitly unsigned integral or enumeration type

To avoid reliance on implementation defined behavior, only declare bit-fields of an explicitly unsigned type (uintN_t) or an enumeration type with an enumeration base of explicitly unsigned type.

#include <cstdint>
enum E : uint8_t { ONE, TWO, THREE };
struct S
{
int32_t a : 2; // @@- Non-Compliant -@@
uint8_t b : 2; // @@+ Compliant +@@
bool    c : 1; // @@- Non-Compliant -@@
char    d : 2; // @@- Non-Compliant -@@
wchar_t e : 2; // @@- Non-Compliant -@@
E       f : 2; // @@+ Compliant +@@
};

References

  • JSF AV C++ Rev C – 154
  • MISRA C++:2008 – 9-6-2
  • MISRA C++:2008 – 9-6-3

Click here for references