| 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 explicit zip_view(Views...) |
| 12 |
|
| 13 |
#include <ranges> |
| 14 |
#include <tuple> |
| 15 |
|
| 16 |
#include "types.h" |
| 17 |
|
| 18 |
template <class T> |
| 19 |
void conversion_test(T); |
| 20 |
|
| 21 |
template <class T, class... Args> |
| 22 |
concept implicitly_constructible_from = requires(Args&&... args) { conversion_test<T>({std::move(args)...}); }; |
| 23 |
|
| 24 |
// test constructor is explicit |
| 25 |
static_assert(std::constructible_from<std::ranges::zip_view<SimpleCommon>, SimpleCommon>); |
| 26 |
static_assert(!implicitly_constructible_from<std::ranges::zip_view<SimpleCommon>, SimpleCommon>); |
| 27 |
|
| 28 |
static_assert(std::constructible_from<std::ranges::zip_view<SimpleCommon, SimpleCommon>, SimpleCommon, SimpleCommon>); |
| 29 |
static_assert( |
| 30 |
!implicitly_constructible_from<std::ranges::zip_view<SimpleCommon, SimpleCommon>, SimpleCommon, SimpleCommon>); |
| 31 |
|
| 32 |
struct MoveAwareView : std::ranges::view_base { |
| 33 |
int moves = 0; |
| 34 |
constexpr MoveAwareView() = default; |
| 35 |
constexpr MoveAwareView(MoveAwareView&& other) : moves(other.moves + 1) { other.moves = 1; } |
| 36 |
constexpr MoveAwareView& operator=(MoveAwareView&& other) { |
| 37 |
moves = other.moves + 1; |
| 38 |
other.moves = 0; |
| 39 |
return *this; |
| 40 |
} |
| 41 |
constexpr const int* begin() const { return &moves; } |
| 42 |
constexpr const int* end() const { return &moves + 1; } |
| 43 |
}; |
| 44 |
|
| 45 |
template <class View1, class View2> |
| 46 |
constexpr void constructorTest(auto&& buffer1, auto&& buffer2) { |
| 47 |
std::ranges::zip_view v{View1{buffer1}, View2{buffer2}}; |
| 48 |
auto [i, j] = *v.begin(); |
| 49 |
assert(i == buffer1[0]); |
| 50 |
assert(j == buffer2[0]); |
| 51 |
}; |
| 52 |
|
| 53 |
constexpr bool test() { |
| 54 |
|
| 55 |
int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 56 |
int buffer2[4] = {9, 8, 7, 6}; |
| 57 |
|
| 58 |
{ |
| 59 |
// constructor from views |
| 60 |
std::ranges::zip_view v(SizedRandomAccessView{buffer}, std::views::iota(0), std::ranges::single_view(2.)); |
| 61 |
auto [i, j, k] = *v.begin(); |
| 62 |
assert(i == 1); |
| 63 |
assert(j == 0); |
| 64 |
assert(k == 2.0); |
| 65 |
} |
| 66 |
|
| 67 |
{ |
| 68 |
// arguments are moved once |
| 69 |
MoveAwareView mv; |
| 70 |
std::ranges::zip_view v{std::move(mv), MoveAwareView{}}; |
| 71 |
auto [numMoves1, numMoves2] = *v.begin(); |
| 72 |
assert(numMoves1 == 2); // one move from the local variable to parameter, one move from parameter to member |
| 73 |
assert(numMoves2 == 1); |
| 74 |
} |
| 75 |
|
| 76 |
// input and forward |
| 77 |
{ |
| 78 |
constructorTest<InputCommonView, ForwardSizedView>(buffer, buffer2); |
| 79 |
} |
| 80 |
|
| 81 |
// bidi and random_access |
| 82 |
{ |
| 83 |
constructorTest<BidiCommonView, SizedRandomAccessView>(buffer, buffer2); |
| 84 |
} |
| 85 |
|
| 86 |
// contiguous |
| 87 |
{ |
| 88 |
constructorTest<ContiguousCommonView, ContiguousCommonView>(buffer, buffer2); |
| 89 |
} |
| 90 |
|
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
int main(int, char**) { |
| 95 |
test(); |
| 96 |
static_assert(test()); |
| 97 |
|
| 98 |
return 0; |
| 99 |
} |