MISRA C++:2023 Rule 10.2.3
The numeric value of an unscoped enumeration with no fixed underlying type shall not be used
Since R2024b
Description
Rule Definition
The numeric value of an unscoped enumeration with no fixed underlying type shall not be used.
Rationale
By default, the underlying type of an enumeration is implementation-defined. This can cause unexpected results if any implicit conversion occurs. Code that relies on the size or properties of the underlying type might not work consistently across different systems. When overloading functions, the compiler's choice of the underlying type can affect which overload is selected, potentially leading to surprising results if the function has overloads for different integer types.
Polyspace Implementation
The rule checker reports a violation when the code uses an unscoped enumeration with no
fixed underlying type and you perform an operation on the enum
that
depends on the underlying type. For example, when the enumeration is the operand to a
bitwise operator. This can also happen when the enumeration is the condition of a
switch
statement where the case constants are not from the same
enumeration. For example:
enum A { x, y }; enum B { z }; int main() { switch (x) //Noncompliant { case y: //... return; case z: //... return; } return 0; }
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
Check Information
Group: Declarations |
Category: Required |
Version History
Introduced in R2024b