[llvm-bugs] [Bug 34536] New: libc++ and libstdc++ sometimes choose different overloads for algorithms

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Sep 8 16:43:19 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=34536

            Bug ID: 34536
           Summary: libc++ and libstdc++ sometimes choose different
                    overloads for algorithms
           Product: libc++
           Version: 5.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: All Bugs
          Assignee: richard-llvm at metafoo.co.uk
          Reporter: dlj at google.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

Example:

=====
#include <algorithm>
#include <iostream>
#include <vector>

struct Base {
    Base(int value) : v(value) {}
    friend bool operator<(const Base& l, const Base& r) { return l.v < r.v; }
    int v;
};

struct Derived : public Base {
    using Base::Base;
    bool operator<(const Derived& o) { return v > o.v; }
};

int main(void) {
    std::vector<Base> b = {{1}, {5}, {0}, {3}};
    std::vector<Derived> d = {{0}, {1}, {3}, {5}};

    std::cout << std::lower_bound(d.begin(), d.end(), 4)->v << std::endl;

    std::sort(b.begin(), b.end());
    for (const auto &x : b) std::cout << x.v << " ";
    std::cout << std::endl;

    std::sort(d.begin(), d.end());
    for (const auto &x : d) std::cout << x.v << " ";
    std::cout << std::endl;

}
=====

libc++:
=====
$ bin/clang++ -std=c++11 -stdlib=libc++ tmp/ex.cc && ./a.out
5
0 1 3 5 
0 1 3 5 
=====

libstdc++:
=====
$ bin/clang++ -std=c++11 -stdlib=libstdc++ tmp/ex.cc && ./a.out
0
0 1 3 5 
5 3 1 0 
=====

This appears to be due to libc++'s requirement that comparison operators be
const. (In some cases, this is diagnosed; but this example specifically ignores
the diagnostic.)

I believe the libc++ behaviour is correct (and libstdc++ has a bug), but I'm
filing this just to double-check.

-- 
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/20170908/6f8a794f/attachment.html>


More information about the llvm-bugs mailing list