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 uses operator< instead. is, comp(*i, *j) != false defaults *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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -