Main Content

MISRA C++:2008 Rule 9-6-3

Bit-fields shall not have enum type

Description

Rule Definition

Bit-fields shall not have enum type.

Rationale

Using bit fields requires that their underlying bit representations are not implementation-defined. The ISO/IEC 14882:2003 does not explicitly define the signedness of the underlying bit representation of enum types. Because the sign of an enum type depends on the implementation, the exact number of bits that is required to represent the values in the enum is implementation-defined.

To avoid code that behaves differently in different implementations and bugs that are difficult to diagnose, do not use enum types as bit fields.

Polyspace Implementation

Polyspace® reports a violation of this rule when you use enum types as bit fields.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

enum Spin {CW, CCW};
void foo(){
	struct DataStruct{
		Spin electron:2; //Noncompliant
	};
}

In this example, Polyspace flags the declaration of the object electron as a bit field because electron is a enum type.

Check Information

Group: Classes
Category: Required

Version History

Introduced in R2013b