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
, comparingx
to itself must always evaluate tofalse
.Assymetry: For all
x, y:
if comparingx
toy
evaluates totrue
, then comparingy
tox
must evaluate tofalse
.Transitivity: For all
x, y, z:
if comparingx
toy
andy
toz
both evaluate totrue
, then comparingx
toz
must also evaluate totrue
.
These compare type methods violate at least one of these requirements:
std::less_equal
std::greater_equal
std::equal_to
std::not_equal_to
std::logical_or
std::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_equal
std::greater_equal
std::equal_to
std::not_equal_to
std::logical_or
std::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