Main Content

MISRA C++:2008 Rule 4-5-2

Expressions with type enum shall not be used as operands to built- in operators other than the subscript operator [ ], the assignment operator =, the equality operators == and !=, the unary & operator, and the relational operators <, <=, >, >=

Description

Rule Definition

Expressions with type enum shall not be used as operands to built- in operators other than the subscript operator [ ], the assignment operator =, the equality operators == and !=, the unary & operator, and the relational operators <, <=, >, >=.

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

In this example, the first and second if statements compare enumerations, which does not violate this rule. Because the third if statement performs an arithmetic operation on enumerated types, Polyspace reports a rule violation.

enum day
{
    monday,
    tuesday,
    wednesday,
    thursday,
    friday,
    saturday,
    sunday
};

day reschedule (day d) {
    if (d == sunday) { //Compliant
        return monday;
    }
    else if (d >= thursday) { //Compliant
        return wednesday;
    }
    else if ( (tuesday + d) >= saturday) { //Noncompliant
        return friday;
    }
}

Check Information

Group: Standard Conversions
Category: Required

Version History

Introduced in R2013b