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, c++20
10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11
12 // constexpr auto operator[](difference_type n) const requires
13 // all_random_access<Const, Views...>
14
15 #include <ranges>
16 #include <cassert>
17
18 #include "../types.h"
19
test()20 constexpr bool test() {
21 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
22
23 {
24 // random_access_range
25 std::ranges::zip_view v(SizedRandomAccessView{buffer}, std::views::iota(0));
26 auto it = v.begin();
27 assert(it[0] == *it);
28 assert(it[2] == *(it + 2));
29 assert(it[4] == *(it + 4));
30
31 static_assert(std::is_same_v<decltype(it[2]), std::pair<int&, int>>);
32 }
33
34 {
35 // contiguous_range
36 std::ranges::zip_view v(ContiguousCommonView{buffer}, ContiguousCommonView{buffer});
37 auto it = v.begin();
38 assert(it[0] == *it);
39 assert(it[2] == *(it + 2));
40 assert(it[4] == *(it + 4));
41
42 static_assert(std::is_same_v<decltype(it[2]), std::pair<int&, int&>>);
43 }
44
45 {
46 // non random_access_range
47 std::ranges::zip_view v(BidiCommonView{buffer});
48 auto iter = v.begin();
49 const auto canSubscript = [](auto&& it) { return requires { it[0]; }; };
50 static_assert(!canSubscript(iter));
51 }
52
53 return true;
54 }
55
main(int,char **)56 int main(int, char**) {
57 test();
58 static_assert(test());
59
60 return 0;
61 }
62