AUTOSAR C++14 Rule A13-5-4
If two opposite operators are defined, one shall be defined in terms of the other
Since R2022a
Description
Rule Definition
If two opposite operators are defined, one shall be defined in terms of the other.
Rationale
Opposite operators have the same argument and return types but exactly complement each
other. For instance, operator==
checks if its arguments are equal and
operator!=
checks for the complementary relation, that is,
inequality.
Defining opposite operators in terms of each other makes your code easier to maintain:
If you define opposite operators independently of each other, each time you update one operator, you have to remember to update the other. For instance, if you update the definition of
operator==
. you have to also explicitly update the definition ofoperator!=
.If you define opposite operators in terms of each other, updating only one operator is sufficient since that change implicitly updates the other operator.
Polyspace Implementation
The checker raises a violation if one of the operators in the following pairs is not defined in terms of the other:
operator==
,operator!=
operator<
,operator>=
operator>
,operator<=
The checker flags one of the operators in the pair. The event list below the checker result shows the location of the other operator.
The checker considers two opposite operators as part of a pair only if they have the same argument and return types.
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: Overloading |
Category: Required, Automated |
Version History
Introduced in R2022a