AUTOSAR C++14 Rule A4-5-1
Expressions with type enum or enum class shall not be used as operands to built-in and overloaded operators other than the subscript operator [], the assignment operator =, the equality operators == and !=, the unary & operator, and the relational operators <, <=, >, >=
Since R2020a
Description
Rule Definition
Expressions with type enum or enum class shall not be used as operands to built-in and overloaded operators other than the subscript operator [], the assignment operator =, the equality operators == and !=, the unary & operator, and the relational operators <, <=, >, >=.
Rationale
In C++, enumerations such as enum
or enum class
have implementations defined behavior. For instance, their underlying type can be any
integral type, including short
or char
. If you use
enumerations as operands to arithmetic operators such as +
or
-
, they are converted to their underlying type. Because the underlying
type of an enumeration is implementation dependent, outcome of arithmetic operations using
enumerations as operands is unpredictable. To avoid unpredictable and non-portable code, use
enumerations as operands to only these operators:
Subscript operator
[]
Assignment operator
=
Equality operators
==
and!=
The Unary
&
operatorThe relational operators
<, <=,>,>=
You can use enumerations as operands to the built in or overloaded instances of only the above operators. Note that Bitmask type enumerations are an exception to this rule. That is, you can use Bitmask type enumerations as operands to any operators.
Polyspace Implementation
Enumerations are valid operands to only the operators listed above. Polyspace® flags enumerations when they are used as operands to any other operators. Note that Polyspace makes no exception for BitmaskType enumerations.
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: Standard conversions |
Category: Required, Automated |
Version History
Introduced in R2020a