1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// REQUIRES: std-at-least-c++23
// <ranges>
// constexpr explicit join_with_view(V base, Pattern pattern);
#include <ranges>
#include <algorithm>
#include <array>
#include <cassert>
#include <utility>
#include "../types.h"
class View : public std::ranges::view_base {
using OuterRange = std::array<std::array<int, 2>, 3>;
static constexpr OuterRange default_range = {{{1, 2}, {3, 4}, {5, 6}}};
static constexpr OuterRange range_on_move = {{{6, 5}, {4, 3}, {2, 1}}};
const OuterRange* r_ = &default_range;
public:
View() = default;
constexpr View(const View&) : r_(&default_range) {}
constexpr View(View&&) : r_(&range_on_move) {}
constexpr View& operator=(View) {
r_ = &default_range;
return *this;
}
constexpr auto begin() { return r_->begin(); }
constexpr auto end() { return r_->end(); }
};
class Pattern : public std::ranges::view_base {
using PatternRange = std::array<int, 2>;
static constexpr PatternRange default_range = {0, 0};
static constexpr PatternRange range_on_move = {7, 7};
const PatternRange* val_ = &default_range;
public:
Pattern() = default;
constexpr Pattern(const Pattern&) : val_(&default_range) {}
constexpr Pattern(Pattern&&) : val_(&range_on_move) {}
constexpr Pattern& operator=(Pattern) {
val_ = &default_range;
return *this;
}
constexpr auto begin() { return val_->begin(); }
constexpr auto end() { return val_->end(); }
};
constexpr bool test() {
{ // Check construction from `view` and `pattern`
{ // `view` and `pattern` are glvalues
View v;
Pattern p;
std::ranges::join_with_view<View, Pattern> jwv(v, p);
assert(std::ranges::equal(jwv, std::array{6, 5, 7, 7, 4, 3, 7, 7, 2, 1}));
}
{ // `view` and `pattern` are const glvalues
const View v;
const Pattern p;
std::ranges::join_with_view<View, Pattern> jwv(v, p);
assert(std::ranges::equal(jwv, std::array{6, 5, 7, 7, 4, 3, 7, 7, 2, 1}));
}
{ // `view` and `pattern` are prvalues
std::ranges::join_with_view<View, Pattern> jwv(View{}, Pattern{});
assert(std::ranges::equal(jwv, std::array{6, 5, 7, 7, 4, 3, 7, 7, 2, 1}));
}
{ // `view` and `pattern` are xvalues
View v;
Pattern p;
std::ranges::join_with_view<View, Pattern> jwv(std::move(v), std::move(p));
assert(std::ranges::equal(jwv, std::array{6, 5, 7, 7, 4, 3, 7, 7, 2, 1}));
}
}
// Check explicitness
static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, View, Pattern>);
static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, View&, Pattern&>);
static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, const View, const Pattern>);
static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, const View&, const Pattern&>);
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|