[libcxx-commits] [libcxx] r355752 - Fix PR41017 - Build failure with _LIBCPP_DEBUG=0 and non-const-ref

Eric Fiselier via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 8 14:59:00 PST 2019


Author: ericwf
Date: Fri Mar  8 14:58:59 2019
New Revision: 355752

URL: http://llvm.org/viewvc/llvm-project?rev=355752&view=rev
Log:
Fix PR41017 - Build failure with _LIBCPP_DEBUG=0 and non-const-ref
comparator for std::sort()

Our debug comparator assumed that the comparator it wraps would always
accepts the values by const ref. This isn't required by the standard.

This patch makes our __debug_less comparator forward the constness.

Modified:
    libcxx/trunk/include/algorithm
    libcxx/trunk/test/libcxx/algorithms/debug_less.pass.cpp

Modified: libcxx/trunk/include/algorithm
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=355752&r1=355751&r2=355752&view=diff
==============================================================================
--- libcxx/trunk/include/algorithm (original)
+++ libcxx/trunk/include/algorithm Fri Mar  8 14:58:59 2019
@@ -784,7 +784,7 @@ struct __debug_less
     __debug_less(_Compare& __c) : __comp_(__c) {}
 
     template <class _Tp, class _Up>
-    bool operator()(const _Tp& __x, const _Up& __y)
+    bool operator()(_Tp& __x,  _Up& __y)
     {
         bool __r = __comp_(__x, __y);
         if (__r)
@@ -795,18 +795,18 @@ struct __debug_less
     template <class _LHS, class _RHS>
     inline _LIBCPP_INLINE_VISIBILITY
     decltype((void)_VSTD::declval<_Compare&>()(
-        _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
-    __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
+        _VSTD::declval<_LHS &>(), _VSTD::declval<_RHS &>()))
+    __do_compare_assert(int, _LHS & __l, _RHS & __r) {
         _LIBCPP_ASSERT(!__comp_(__l, __r),
             "Comparator does not induce a strict weak ordering");
     }
 
     template <class _LHS, class _RHS>
     inline _LIBCPP_INLINE_VISIBILITY
-    void __do_compare_assert(long, _LHS const&, _RHS const&) {}
+    void __do_compare_assert(long, _LHS &, _RHS &) {}
 };
 
-#endif  // _LIBCPP_DEBUG
+#endif // _LIBCPP_DEBUG
 
 // all_of
 

Modified: libcxx/trunk/test/libcxx/algorithms/debug_less.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/algorithms/debug_less.pass.cpp?rev=355752&r1=355751&r2=355752&view=diff
==============================================================================
--- libcxx/trunk/test/libcxx/algorithms/debug_less.pass.cpp (original)
+++ libcxx/trunk/test/libcxx/algorithms/debug_less.pass.cpp Fri Mar  8 14:58:59 2019
@@ -20,8 +20,11 @@ struct DebugException {};
 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : throw ::DebugException())
 
 #include <algorithm>
+#include <iterator>
 #include <cassert>
 
+#include "test_macros.h"
+
 template <int ID>
 struct MyType {
     int value;
@@ -210,10 +213,32 @@ void test_upper_and_lower_bound() {
     }
 }
 
+struct NonConstArgCmp {
+    bool operator()(int& x, int &y) const {
+        return x < y;
+    }
+};
+
+void test_non_const_arg_cmp() {
+    {
+        NonConstArgCmp cmp;
+        __debug_less<NonConstArgCmp> dcmp(cmp);
+        int x = 0, y = 1;
+        assert(dcmp(x, y));
+        assert(!dcmp(y, x));
+    }
+    {
+        NonConstArgCmp cmp;
+        int arr[] = {5, 4, 3, 2, 1};
+        std::sort(std::begin(arr), std::end(arr), cmp);
+        assert(std::is_sorted(std::begin(arr), std::end(arr)));
+    }
+}
+
 int main(int, char**) {
     test_passing();
     test_failing();
     test_upper_and_lower_bound();
-
-  return 0;
+    test_non_const_arg_cmp();
+    return 0;
 }




More information about the libcxx-commits mailing list