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+(iter_difference_t<I> n) const
12// requires random_access_iterator<I>;
13// friend constexpr counted_iterator operator+(
14// iter_difference_t<I> n, const counted_iterator& x)
15// requires random_access_iterator<I>;
16// constexpr counted_iterator& operator+=(iter_difference_t<I> n)
17// requires random_access_iterator<I>;
18
19#include <iterator>
20
21#include "test_macros.h"
22#include "test_iterators.h"
23
24template<class Iter>
25concept PlusEnabled = requires(Iter& iter) {
26 iter + 1;
27};
28
29template<class Iter>
30concept PlusEqEnabled = requires(Iter& iter) {
31 iter += 1;
32};
33
34constexpr bool test() {
35 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
36
37 {
38 {
39 using Counted = std::counted_iterator<random_access_iterator<int*>>;
40 std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
41 assert(iter + 2 == Counted(random_access_iterator<int*>{buffer + 2}, 6));
42 assert(iter + 0 == Counted(random_access_iterator<int*>{buffer}, 8));
43
44 ASSERT_SAME_TYPE(decltype(iter + 2), Counted);
45 }
46 {
47 using Counted = const std::counted_iterator<random_access_iterator<int*>>;
48 const std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
49 assert(iter + 8 == Counted(random_access_iterator<int*>{buffer + 8}, 0));
50 assert(iter + 0 == Counted(random_access_iterator<int*>{buffer}, 8));
51
52 ASSERT_SAME_TYPE(decltype(iter + 2), std::remove_const_t<Counted>);
53 }
54 }
55
56 {
57 {
58 using Counted = std::counted_iterator<random_access_iterator<int*>>;
59 std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
60 assert(2 + iter == Counted(random_access_iterator<int*>{buffer + 2}, 6));
61 assert(0 + iter == Counted(random_access_iterator<int*>{buffer}, 8));
62
63 ASSERT_SAME_TYPE(decltype(iter + 2), Counted);
64 }
65 {
66 using Counted = const std::counted_iterator<random_access_iterator<int*>>;
67 const std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
68 assert(8 + iter == Counted(random_access_iterator<int*>{buffer + 8}, 0));
69 assert(0 + iter == Counted(random_access_iterator<int*>{buffer}, 8));
70
71 ASSERT_SAME_TYPE(decltype(iter + 2), std::remove_const_t<Counted>);
72 }
73 }
74
75 {
76 {
77 using Counted = std::counted_iterator<random_access_iterator<int*>>;
78 std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
79 assert((iter += 2) == Counted(random_access_iterator<int*>{buffer + 2}, 6));
80 assert((iter += 0) == Counted(random_access_iterator<int*>{buffer + 2}, 6));
81
82 ASSERT_SAME_TYPE(decltype(iter += 2), Counted&);
83 }
84 {
85 using Counted = std::counted_iterator<contiguous_iterator<int*>>;
86 std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
87 assert((iter += 8) == Counted(contiguous_iterator<int*>{buffer + 8}, 0));
88 assert((iter += 0) == Counted(contiguous_iterator<int*>{buffer + 8}, 0));
89
90 ASSERT_SAME_TYPE(decltype(iter += 2), Counted&);
91 }
92 {
93 static_assert( PlusEnabled<std::counted_iterator<random_access_iterator<int*>>>);
94 static_assert(!PlusEnabled<std::counted_iterator<bidirectional_iterator<int*>>>);
95
96 static_assert( PlusEqEnabled< std::counted_iterator<random_access_iterator<int*>>>);
97 static_assert(!PlusEqEnabled<const std::counted_iterator<random_access_iterator<int*>>>);
98 }
99 }
100
101 return true;
102}
103
104int main(int, char**) {
105 test();
106 static_assert(test());
107
108 return 0;
109}
110

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