Skip to content

Commit 23deda3

Browse files
committed
address code review
1 parent 0ab15ce commit 23deda3

File tree

3 files changed

+23
-19
lines changed
  • libcxx

3 files changed

+23
-19
lines changed

libcxx/include/__iterator/prev.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n)
3939
return __x;
4040
}
4141

42-
template <class _BidirectionalIterator,
43-
__enable_if_t<__has_bidirectional_iterator_category<_BidirectionalIterator>::value, int> = 0>
44-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _BidirectionalIterator
45-
prev(_BidirectionalIterator __it) {
42+
template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
43+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter prev(_InputIter __it) {
44+
static_assert(__has_bidirectional_iterator_category<_InputIter>::value,
45+
"Attempt to prev(it) with a non-bidirectional iterator");
4646
return std::prev(std::move(__it), 1);
4747
}
4848

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// std::prev
10+
11+
#include <iterator>
12+
#include "test_iterators.h"
13+
14+
void test() {
15+
int arr[] = {1, 2};
16+
cpp17_input_iterator<int*> it(&arr[0]);
17+
it = std::prev(it);
18+
// expected-error-re@*:* {{static assertion failed due to requirement {{.*}}: Attempt to prev(it) with a non-bidirectional iterator}}
19+
}

libcxx/test/std/iterators/iterator.primitives/iterator.operations/prev.pass.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@
1818
#include "test_macros.h"
1919
#include "test_iterators.h"
2020

21-
template <class Iter>
22-
std::false_type prev_test(...);
23-
24-
template <class Iter>
25-
decltype((void)std::prev(std::declval<Iter>()), std::true_type()) prev_test(int);
26-
27-
template <class Iter>
28-
using CanPrev = decltype(prev_test<Iter>(0));
29-
30-
static_assert(!CanPrev<cpp17_input_iterator<int*> >::value, "");
31-
static_assert(CanPrev<bidirectional_iterator<int*> >::value, "");
32-
#if TEST_STD_VER >= 20
33-
static_assert(!CanPrev<cpp20_random_access_iterator<int*> >::value);
34-
#endif
35-
3621
template <class It>
3722
TEST_CONSTEXPR_CXX17 void
3823
check_prev_n(It it, typename std::iterator_traits<It>::difference_type n, It result)

0 commit comments

Comments
 (0)