[libcxx-commits] [libcxx] 9c0564a - [libc++][ranges] fix `std::search_n` incorrect `static_assert`

Konstantin Varlamov via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 19 17:24:55 PDT 2022


Author: Hui Xie
Date: 2022-07-19T17:24:47-07:00
New Revision: 9c0564a3a7df1c518e4f5b7eda54a84fc7e49776

URL: https://github.com/llvm/llvm-project/commit/9c0564a3a7df1c518e4f5b7eda54a84fc7e49776
DIFF: https://github.com/llvm/llvm-project/commit/9c0564a3a7df1c518e4f5b7eda54a84fc7e49776.diff

LOG: [libc++][ranges] fix `std::search_n` incorrect `static_assert`

[libc++][ranges] fix `std::search_n` incorrect `static_assert`
see more detail in https://reviews.llvm.org/D124079?#3661721

Differential Revision: https://reviews.llvm.org/D130124

Added: 
    

Modified: 
    libcxx/include/__algorithm/search_n.h
    libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__algorithm/search_n.h b/libcxx/include/__algorithm/search_n.h
index 2a0547565ee97..ccb8e845f5b1f 100644
--- a/libcxx/include/__algorithm/search_n.h
+++ b/libcxx/include/__algorithm/search_n.h
@@ -163,7 +163,7 @@ _ForwardIterator search_n(_ForwardIterator __first, _ForwardIterator __last,
                           _Size __count,
                           const _Tp& __value,
                           _BinaryPredicate __pred) {
-  static_assert(__is_callable<_BinaryPredicate, decltype(*__first), decltype(*__last)>::value,
+  static_assert(__is_callable<_BinaryPredicate, decltype(*__first), const _Tp&>::value,
                 "BinaryPredicate has to be callable");
   auto __proj = __identity();
   return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;

diff  --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
index 6a3ab75758dd7..3bf72e3fbf57b 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
@@ -158,12 +158,35 @@ test()
     count_equal::count = 0;
 }
 
+class A {
+public:
+  A(int x, int y) : x_(x), y_(y) {}
+  int x() const { return x_; }
+  int y() const { return y_; }
+
+private:
+  int x_;
+  int y_;
+};
+
+struct Pred {
+  bool operator()(const A& l, int r) const { return l.x() == r; }
+};
+
 int main(int, char**)
 {
     test<forward_iterator<const int*> >();
     test<bidirectional_iterator<const int*> >();
     test<random_access_iterator<const int*> >();
 
+    // test bug reported in https://reviews.llvm.org/D124079?#3661721
+    {
+        A a[]       = {A(1, 2), A(2, 3), A(2, 4)};
+        int value   = 2;
+        auto result = std::search_n(a, a + 3, 1, value, Pred());
+        assert(result == a + 1);
+    }
+
 #if TEST_STD_VER > 17
     static_assert(test_constexpr());
 #endif


        


More information about the libcxx-commits mailing list