[LLVMbugs] [Bug 19547] New: min_element doesn't produce the first minimum element in some cases

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Apr 24 12:11:08 PDT 2014


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: unassignedclangbugs at nondot.org
          Reporter: kremenek at apple.com
                CC: llvmbugs at cs.uiuc.edu, mclow.lists at gmail.com
    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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140424/bbdf2d24/attachment.html>


More information about the llvm-bugs mailing list