diff --git a/include/algorithm b/include/algorithm index 9ce6aa011..beee6b5b8 100644 --- a/include/algorithm +++ b/include/algorithm @@ -4088,14 +4088,8 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) { -#ifdef _LIBCPP_DEBUG - typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; - __debug_less<_Compare> __c(__comp); - return __lower_bound<_Comp_ref>(__first, __last, __value_, __c); -#else // _LIBCPP_DEBUG typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp); -#endif // _LIBCPP_DEBUG } template @@ -4136,14 +4130,8 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) { -#ifdef _LIBCPP_DEBUG - typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; - __debug_less<_Compare> __c(__comp); - return __upper_bound<_Comp_ref>(__first, __last, __value_, __c); -#else // _LIBCPP_DEBUG typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp); -#endif // _LIBCPP_DEBUG } template diff --git a/test/libcxx/algorithms/debug_less.pass.cpp b/test/libcxx/algorithms/debug_less.pass.cpp index e030f64e5..302c5a837 100644 --- a/test/libcxx/algorithms/debug_less.pass.cpp +++ b/test/libcxx/algorithms/debug_less.pass.cpp @@ -161,7 +161,58 @@ void test_failing() { } } +template +struct Tag { + explicit Tag(int v) : value(v) {} + int value; +}; + +template +struct FooImp { + explicit FooImp(int x) : x_(x) {} + int x_; +}; + +template +inline bool operator<(FooImp const& x, Tag<0> y) { + return x.x_ < y.value; +} + +template +inline bool operator<(Tag<0>, FooImp const&) { + static_assert(sizeof(FooImp) != sizeof(FooImp), "should not be instantiated"); +} + +template +inline bool operator<(Tag<1> x, FooImp const& y) { + return x.value < y.x_; +} + +template +inline bool operator<(FooImp const&, Tag<1>) { + static_assert(sizeof(FooImp) != sizeof(FooImp), "should not be instantiated"); +} + +typedef FooImp<> Foo; + +// Test that we don't attempt to call the comparator with the arguments reversed +// for upper_bound and lower_bound since the comparator or type is not required +// to support it, nor does it require the range to have a strict weak ordering. +// See llvm.org/PR39458 +void test_upper_and_lower_bound() { + Foo table[] = {Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)}; + { + Foo* iter = std::lower_bound(std::begin(table), std::end(table), Tag<0>(3)); + assert(iter == (table + 2)); + } + { + Foo* iter = std::upper_bound(std::begin(table), std::end(table), Tag<1>(3)); + assert(iter == (table + 3)); + } +} + int main() { test_passing(); test_failing(); + test_upper_and_lower_bound(); }