Skip to content

Commit 9c0564a

Browse files
huixie90var-const
authored andcommitted
[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
1 parent 94e6d26 commit 9c0564a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

libcxx/include/__algorithm/search_n.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ _ForwardIterator search_n(_ForwardIterator __first, _ForwardIterator __last,
163163
_Size __count,
164164
const _Tp& __value,
165165
_BinaryPredicate __pred) {
166-
static_assert(__is_callable<_BinaryPredicate, decltype(*__first), decltype(*__last)>::value,
166+
static_assert(__is_callable<_BinaryPredicate, decltype(*__first), const _Tp&>::value,
167167
"BinaryPredicate has to be callable");
168168
auto __proj = __identity();
169169
return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;

libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,35 @@ test()
158158
count_equal::count = 0;
159159
}
160160

161+
class A {
162+
public:
163+
A(int x, int y) : x_(x), y_(y) {}
164+
int x() const { return x_; }
165+
int y() const { return y_; }
166+
167+
private:
168+
int x_;
169+
int y_;
170+
};
171+
172+
struct Pred {
173+
bool operator()(const A& l, int r) const { return l.x() == r; }
174+
};
175+
161176
int main(int, char**)
162177
{
163178
test<forward_iterator<const int*> >();
164179
test<bidirectional_iterator<const int*> >();
165180
test<random_access_iterator<const int*> >();
166181

182+
// test bug reported in https://reviews.llvm.org/D124079?#3661721
183+
{
184+
A a[] = {A(1, 2), A(2, 3), A(2, 4)};
185+
int value = 2;
186+
auto result = std::search_n(a, a + 3, 1, value, Pred());
187+
assert(result == a + 1);
188+
}
189+
167190
#if TEST_STD_VER > 17
168191
static_assert(test_constexpr());
169192
#endif

0 commit comments

Comments
 (0)