Why is this an issue?
Enumerations are used to represent symbolic values, or sometimes bit fields. They are not supposed to be used in arithmetic contexts.
Additionally, even though comparing them with integer numbers can make sense (for instance, to test if an enum lies with a certain range),
comparing them with floating point numbers does not (and is deprecated since C++20).
There are other restrictions related to the use of enums, see for instance S2753.
Noncompliant code example
enum { COLOUR_0, COLOUR_1, COLOUR_2, COLOUR_COUNT } colour;
if ( COLOUR_0 == colour ) { ... }
if ( ( COLOUR_0 + COLOUR_1 ) == colour ) { ... } // Noncompliant, arithmetic used
if ( colour < COLOUR_COUNT ) { ... }
if ( colour > 3.14 ) { ... } // Noncompliant, comparison with float
Resources
- MISRA C++:2008, 4-5-2 - Expressions with type enum shall not be used as operands to builtin operators other than the subscript operator [ ],
the assignment operator =, the equality operators == and !=, the unary & operator, and the relational operators <, <=, >, >=