[libcxx-commits] [PATCH] D130124: [libc++][ranges] fix `std::search_n` incorrect `static_assert`

Hui via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 19 14:00:57 PDT 2022


huixie90 created this revision.
huixie90 added reviewers: bgraur, philnik, var-const, Mordante.
Herald added a project: All.
huixie90 requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130124

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


Index: libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
===================================================================
--- libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
+++ libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
@@ -158,12 +158,35 @@
     count_equal::count = 0;
 }
 
+// test bug reported in https://reviews.llvm.org/D124079?#3661721
+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; }
+};
+
+void testStaticAssertBug() {
+  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);
+}
+
 int main(int, char**)
 {
     test<forward_iterator<const int*> >();
     test<bidirectional_iterator<const int*> >();
     test<random_access_iterator<const int*> >();
-
+    testStaticAssertBug();
 #if TEST_STD_VER > 17
     static_assert(test_constexpr());
 #endif
Index: libcxx/include/__algorithm/search_n.h
===================================================================
--- libcxx/include/__algorithm/search_n.h
+++ libcxx/include/__algorithm/search_n.h
@@ -163,7 +163,7 @@
                           _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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130124.445936.patch
Type: text/x-patch
Size: 1896 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220719/d3b3de0a/attachment.bin>


More information about the libcxx-commits mailing list