| 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*() const; |
| 12 | |
| 13 | #include <array> |
| 14 | #include <cassert> |
| 15 | #include <ranges> |
| 16 | #include <tuple> |
| 17 | |
| 18 | #include "../types.h" |
| 19 | |
| 20 | constexpr bool test() { |
| 21 | std::array a{1, 2, 3, 4}; |
| 22 | std::array b{4.1, 3.2, 4.3}; |
| 23 | { |
| 24 | // single range |
| 25 | std::ranges::zip_view v(a); |
| 26 | auto it = v.begin(); |
| 27 | assert(&(std::get<0>(*it)) == &(a[0])); |
| 28 | static_assert(std::is_same_v<decltype(*it), std::tuple<int&>>); |
| 29 | } |
| 30 | |
| 31 | { |
| 32 | // operator* is const |
| 33 | std::ranges::zip_view v(a); |
| 34 | const auto it = v.begin(); |
| 35 | assert(&(std::get<0>(*it)) == &(a[0])); |
| 36 | } |
| 37 | |
| 38 | { |
| 39 | // two ranges with different types |
| 40 | std::ranges::zip_view v(a, b); |
| 41 | auto it = v.begin(); |
| 42 | auto [x, y] = *it; |
| 43 | assert(&x == &(a[0])); |
| 44 | assert(&y == &(b[0])); |
| 45 | static_assert(std::is_same_v<decltype(*it), std::tuple<int&, double&>>); |
| 46 | |
| 47 | x = 5; |
| 48 | y = 0.1; |
| 49 | assert(a[0] == 5); |
| 50 | assert(b[0] == 0.1); |
| 51 | } |
| 52 | |
| 53 | { |
| 54 | // underlying range with prvalue range_reference_t |
| 55 | std::ranges::zip_view v(a, b, std::views::iota(0, 5)); |
| 56 | auto it = v.begin(); |
| 57 | assert(&(std::get<0>(*it)) == &(a[0])); |
| 58 | assert(&(std::get<1>(*it)) == &(b[0])); |
| 59 | assert(std::get<2>(*it) == 0); |
| 60 | static_assert(std::is_same_v<decltype(*it), std::tuple<int&, double&, int>>); |
| 61 | } |
| 62 | |
| 63 | { |
| 64 | // const-correctness |
| 65 | std::ranges::zip_view v(a, std::as_const(a)); |
| 66 | auto it = v.begin(); |
| 67 | assert(&(std::get<0>(*it)) == &(a[0])); |
| 68 | assert(&(std::get<1>(*it)) == &(a[0])); |
| 69 | static_assert(std::is_same_v<decltype(*it), std::tuple<int&, int const&>>); |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | int main(int, char**) { |
| 75 | test(); |
| 76 | static_assert(test()); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |