c++ - std::sort - is passing a faulty comparator undefined behavior? -
consider code:
std::sort(vec.begin(), vec.end(), [](const foo& lhs, const foo& rhs) { return !(lhs < rhs); } ); if lhs == rhs, both lambda(lhs, rhs) , lambda(rhs, lhs) return true, violates requirement provide strict weak ordering. however, standard explicitly mark passing such comparator undefined behavior?
warning: extreme language-lawyering follows.
the wording of the recent draft of standard puts way in [alg.sorting]p3:
for algorithms take
compare, there version usesoperator<instead. is,comp(*i, *j) != falsedefaults*i < *j != false. algorithms other described in 25.4.3, comp shall induce strict weak ordering on values.
by using word "shall", standard implicitly stating violating leads undefined behavior.
whether requires given function impose swo possible values or values given algorithm not clear standard. however, since restriction stated in paragraph talking specific algorithms, it's not unreasonable assume refering range of values provided algorithm.
otherwise, default operator< not impose swo floats, nan.
Comments
Post a Comment