| 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 | |
| 19 | constexpr 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 | static_assert(std::is_same_v<decltype(it[2]), std::tuple<int&, int>>); |
| 31 | } |
| 32 | |
| 33 | { |
| 34 | // contiguous_range |
| 35 | std::ranges::zip_view v(ContiguousCommonView{buffer}, ContiguousCommonView{buffer}); |
| 36 | auto it = v.begin(); |
| 37 | assert(it[0] == *it); |
| 38 | assert(it[2] == *(it + 2)); |
| 39 | assert(it[4] == *(it + 4)); |
| 40 | |
| 41 | static_assert(std::is_same_v<decltype(it[2]), std::tuple<int&, int&>>); |
| 42 | } |
| 43 | |
| 44 | { |
| 45 | // non random_access_range |
| 46 | std::ranges::zip_view v(BidiCommonView{buffer}); |
| 47 | auto iter = v.begin(); |
| 48 | const auto canSubscript = [](auto&& it) { return requires { it[0]; }; }; |
| 49 | static_assert(!canSubscript(iter)); |
| 50 | } |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | int main(int, char**) { |
| 56 | test(); |
| 57 | static_assert(test()); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |