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 // UNSUPPORTED: libcpp-no-concepts
11 // UNSUPPORTED: gcc-10
12 
13 // constexpr decltype(auto) operator[](iter_difference_t<I> n) const
14 //   requires random_access_iterator<I>;
15 
16 #include <iterator>
17 
18 #include "test_macros.h"
19 #include "test_iterators.h"
20 
21 template<class Iter>
22 concept SubscriptEnabled = requires(Iter& iter) {
23   iter[1];
24 };
25 
26 constexpr bool test() {
27   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
28 
29   {
30     std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
31     for (int i = 1; i < 9; ++i)
32       assert(iter[i - 1] == i);
33   }
34   {
35     std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
36     for (int i = 1; i < 9; ++i)
37       assert(iter[i - 1] == i);
38   }
39 
40   {
41     const std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
42     assert(iter[0] == 1);
43   }
44   {
45     const std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
46     assert(iter[7] == 8);
47   }
48 
49   {
50       static_assert( SubscriptEnabled<std::counted_iterator<random_access_iterator<int*>>>);
51       static_assert(!SubscriptEnabled<std::counted_iterator<bidirectional_iterator<int*>>>);
52   }
53 
54   return true;
55 }
56 
57 int main(int, char**) {
58   test();
59   static_assert(test());
60 
61   return 0;
62 }
63