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

source code of libcxx/test/std/ranges/range.adaptors/range.zip/iterator/subscript.pass.cpp