| 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 |
#ifdef _LIBCPP_VERSION // libc++ doesn't implement P2165R4 yet |
| 46 |
static_assert(std::is_same_v<decltype(*it), std::pair<int&, double&>>); |
| 47 |
#else |
| 48 |
static_assert(std::is_same_v<decltype(*it), std::tuple<int&, double&>>); |
| 49 |
#endif |
| 50 |
|
| 51 |
x = 5; |
| 52 |
y = 0.1; |
| 53 |
assert(a[0] == 5); |
| 54 |
assert(b[0] == 0.1); |
| 55 |
} |
| 56 |
|
| 57 |
{ |
| 58 |
// underlying range with prvalue range_reference_t |
| 59 |
std::ranges::zip_view v(a, b, std::views::iota(0, 5)); |
| 60 |
auto it = v.begin(); |
| 61 |
assert(&(std::get<0>(*it)) == &(a[0])); |
| 62 |
assert(&(std::get<1>(*it)) == &(b[0])); |
| 63 |
assert(std::get<2>(*it) == 0); |
| 64 |
static_assert(std::is_same_v<decltype(*it), std::tuple<int&, double&, int>>); |
| 65 |
} |
| 66 |
|
| 67 |
{ |
| 68 |
// const-correctness |
| 69 |
std::ranges::zip_view v(a, std::as_const(a)); |
| 70 |
auto it = v.begin(); |
| 71 |
assert(&(std::get<0>(*it)) == &(a[0])); |
| 72 |
assert(&(std::get<1>(*it)) == &(a[0])); |
| 73 |
#ifdef _LIBCPP_VERSION // libc++ doesn't implement P2165R4 yet |
| 74 |
static_assert(std::is_same_v<decltype(*it), std::pair<int&, int const&>>); |
| 75 |
#else |
| 76 |
static_assert(std::is_same_v<decltype(*it), std::tuple<int&, int const&>>); |
| 77 |
#endif |
| 78 |
} |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
int main(int, char**) { |
| 83 |
test(); |
| 84 |
static_assert(test()); |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |