AUTOSAR C++14 Rule A25-4-1
Ordering predicates used with associative containers and STL sorting and related algorithms shall adhere to a strict weak ordering relation
Since R2022a
Description
Rule Definition
Ordering predicates used with associative containers and STL sorting and related algorithms shall adhere to a strict weak ordering relation.
Rationale
Algorithms and containers of the standard template library (STL) use predicates to sort and compare their elements. The predicates must adhere to strict weak ordering. That is, the predicate must adhere to these requirements:
Irreflexivity: For all
x, comparingxto itself must always evaluate tofalse.Asymmetry: For all
x, y:if comparingxtoyevaluates totrue, then comparingytoxmust evaluate tofalse.Transitivity: For all
x, y, z:if comparingxtoyandytozboth evaluate totrue, then comparingxtozmust also evaluate totrue.
These compare type methods violate at least one of these requirements:
std::less_equalstd::greater_equalstd::equal_tostd::not_equal_tostd::logical_orstd::logical_and
Using the preceding functions as ordering predicates for algorithms and
containers from the STL might result in infinite loops, erratic behavior, and bugs that are
difficult to diagnose. Instead, use functions that adhere to the strict weak ordering
relation. For instance, use functions such as std::less or
std::greater.
Polyspace Implementation
Polyspace® raises a violation of this rule when you use one of these compare types as predicates in standard library algorithms and containers:
std::less_equalstd::greater_equalstd::equal_tostd::not_equal_tostd::logical_orstd::logical_and
For a list of standard library algorithms that expect a predicate with strict weak ordering, see Compare.
If you use a user-defined predicate function, Polyspace does not check if the custom predicate adheres to strict weak ordering.
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: Algorithms library |
| Category: Required, Non-automated |
Version History
Introduced in R2022a