<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - min_element doesn't produce the first minimum element in some cases"
   href="http://llvm.org/bugs/show_bug.cgi?id=19547">19547</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>min_element doesn't produce the first minimum element in some cases
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.4
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kremenek@apple.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu, mclow.lists@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>