http://llvm.org/bugs/show_bug.cgi?id=19547

            Bug ID: 19547
           Summary: min_element doesn't produce the first minimum element
                    in some cases
           Product: libc++
           Version: 3.4
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected]
    Classification: Unclassified

min_element is defined as calling it's comparator with the current min as the
first argument & the current iteration element as the second one.  However, it
does the reverse which leads to an unstable result for equal elements.

$ cat test.cpp
#include <algorithm>
#include <array>
#include <iostream>

struct X {
    X(int x) : _x(x) {}

    bool operator<=(const X& x) const
    {
        return _x <= x._x;
    }

    int _x;
};

int main()
{
    std::array<X, 5> x = {1, 1, 2, 4, 4};

    auto min = std::min_element(x.begin(), x.end(), [](const X& x1, const X&
x2) {
        return x1 <= x2;
    });

    if (min == x.begin()) {
        std::cerr << "min_element OK\n";
    } else {
          std::cerr << "min_element isn't stable\n";
        }
}

$ clang++ -stdlib=libc++ test.cpp -std=c++11

$ ./a.out
min_element isn't stable

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs

Reply via email to