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