AUTOSAR C++14 Rule A13-5-5
Comparison operators shall be non-member functions with identical parameter types and noexcept
Since R2020b
Description
Rule Definition
Comparison operators shall be non-member functions with identical parameter types and noexcept.
Rationale
Comparison operators must not compare objects that are of different types. If you pass objects of different types as arguments to a comparison operator, the operator must be able to convert one argument to the data type of the other.
Member functions have the inherent limitation that the implicit object parameter (the
one referred to by the this
pointer) cannot be converted to another data
type. To support data type conversions when required, define comparison operators as
non-member functions.
Comparison expressions are fundamental operations and must be noexcept. The comparison operators covered by this rule are:
==
!=
<
<=
>
>=
Note
Declare comparison operators as friend
to enable them to access
internal data similar to a member function. This practice is allowed by the exception in
rule A11-3-1.
Polyspace Implementation
The checker flags comparison operators that are defined as member functions. The checker also flags non-member comparison operators that:
Compare nonidentical parameter types, such as a
class
type andint
.Are not declared with the
noexcept
specifier.
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 R2020b