Std :: sort - is the erroneous behavior of the comparator undefined passed?

Consider this code:

std::sort(vec.begin(), vec.end(),
    [](const Foo& lhs, const Foo& rhs) { return !(lhs < rhs); }
);

If lhs == rhs, then lambda (lhs, rhs) and lambda (rhs, lhs) will return true, which violates the requirement to ensure a strict weak order. However, does the standard indicate that such a comparator indicates undefined behavior?

+4
source share
3 answers

Warning: extreme legal practice appears.

The text of the most recent draft of the standard puts it this way in [alg.sorting] p3:

, Compare, , operator<. comp(*i, *j) != false *i < *j != false. , 25.4.3, comp .

"", , undefined .

, SWO , , . , , , , , .

operator< SWO float s, NaN.

+4

[alg.sorting]/3 , 25.4.3, , comp .

[alg.sorting]/4 "" (!comp(x, x) x)...

.

+1

std:: sort crash, <?.

, , , , .

, , . " " " undefined".

25.3 [alg.sorting]

  • 25.3 : , Compare , <.
  • Compare , true, , false . comp , . , comp - .
  • , Compare, , < . comp (* i, * j) != false * < * j!= false. , 25.3.3 , , comp .
  • "" (! comp (x, x) x), , , , , , . define equiv (a, b) as! comp (a, b) & &! comp (b, a), , comp equiv :
    • comp (a, b) && & comp (b, c) , comp (a, c)
    • equiv (a, b) && equiv (b, c) means equiv (a, c) [Note: in these conditions it can be shown that
      • equiv - equivalence relation
      • comp induces a well-defined relation to equivalence classes defined by equiv
      • Induced attitude is a strict complete ordering. -end note]
+1
source

Source: https://habr.com/ru/post/1624631/


All Articles