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// UNSUPPORTED: c++03, c++11, c++14, c++17
10
11// constexpr counted_iterator& operator--()
12// requires bidirectional_iterator<I>;
13// constexpr counted_iterator operator--(int)
14// requires bidirectional_iterator<I>;
15
16#include <iterator>
17
18#include "test_macros.h"
19#include "test_iterators.h"
20
21template<class Iter>
22concept MinusEnabled = requires(Iter& iter) {
23 iter--;
24 --iter;
25};
26
27constexpr bool test() {
28 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
29
30 {
31 using Counted = std::counted_iterator<bidirectional_iterator<int*>>;
32 std::counted_iterator iter(bidirectional_iterator<int*>{buffer + 2}, 6);
33 assert(iter-- == Counted(bidirectional_iterator<int*>{buffer + 2}, 6));
34 assert(--iter == Counted(bidirectional_iterator<int*>{buffer}, 8));
35 assert(iter.count() == 8);
36
37 ASSERT_SAME_TYPE(decltype(iter--), Counted);
38 ASSERT_SAME_TYPE(decltype(--iter), Counted&);
39 }
40 {
41 using Counted = std::counted_iterator<random_access_iterator<int*>>;
42 Counted iter(random_access_iterator<int*>{buffer + 2}, 6);
43 assert(iter-- == Counted(random_access_iterator<int*>{buffer + 2}, 6));
44 assert(--iter == Counted(random_access_iterator<int*>{buffer}, 8));
45 assert(iter.count() == 8);
46
47 ASSERT_SAME_TYPE(decltype(iter--), Counted);
48 ASSERT_SAME_TYPE(decltype(--iter), Counted&);
49 }
50 {
51 using Counted = std::counted_iterator<contiguous_iterator<int*>>;
52 std::counted_iterator iter(contiguous_iterator<int*>{buffer + 2}, 6);
53 assert(iter-- == Counted(contiguous_iterator<int*>{buffer + 2}, 6));
54 assert(--iter == Counted(contiguous_iterator<int*>{buffer}, 8));
55 assert(iter.count() == 8);
56
57 ASSERT_SAME_TYPE(decltype(iter--), Counted);
58 ASSERT_SAME_TYPE(decltype(--iter), Counted&);
59 }
60
61 {
62 static_assert( MinusEnabled<std::counted_iterator<contiguous_iterator<int*>>>);
63 static_assert(!MinusEnabled<const std::counted_iterator<contiguous_iterator<int*>>>);
64 static_assert(!MinusEnabled<std::counted_iterator<forward_iterator<int*>>>);
65 }
66
67 return true;
68}
69
70int main(int, char**) {
71 test();
72 static_assert(test());
73
74 return 0;
75}
76

source code of libcxx/test/std/iterators/predef.iterators/counted.iterator/decrement.pass.cpp