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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
//===----------------------------------------------------------------------===//
//
// 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 decltype(auto) operator*() const;
#include <ranges>
#include <array>
#include <cassert>
#include <cstddef>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "../types.h"
struct ProxyRef {
int& val;
};
class CommonProxyRef {
public:
constexpr CommonProxyRef(ProxyRef i) : val(i.val) {}
constexpr CommonProxyRef(int i) : val(i) {}
constexpr int get() const { return val; }
private:
int val;
};
template <template <class> class TQual, template <class> class UQual>
struct std::basic_common_reference<ProxyRef, int, TQual, UQual> {
using type = CommonProxyRef;
};
template <template <class> class TQual, template <class> class UQual>
struct std::basic_common_reference<int, ProxyRef, TQual, UQual> {
using type = CommonProxyRef;
};
static_assert(std::common_reference_with<int&, ProxyRef>);
static_assert(std::common_reference_with<int&, CommonProxyRef>);
class ProxyIter {
public:
using value_type = int;
using difference_type = std::ptrdiff_t;
constexpr ProxyIter() : ptr_(nullptr) {}
constexpr explicit ProxyIter(int* p) : ptr_(p) {}
constexpr ProxyRef operator*() const { return ProxyRef{*ptr_}; }
constexpr ProxyIter& operator++() {
++ptr_;
return *this;
}
constexpr ProxyIter operator++(int) {
ProxyIter tmp = *this;
++ptr_;
return tmp;
}
constexpr bool operator==(const ProxyIter& other) const { return ptr_ == other.ptr_; }
private:
int* ptr_;
};
static_assert(std::forward_iterator<ProxyIter>);
constexpr bool test() {
{ // Result of `operator*` is (maybe const) lvalue reference
using V = std::ranges::owning_view<std::vector<std::string>>;
using Pattern = std::ranges::owning_view<std::string>;
using JWV = std::ranges::join_with_view<V, Pattern>;
JWV jwv(V{{"ab", "cd", "ef"}}, Pattern{"><"});
{
auto it = jwv.begin();
std::same_as<char&> decltype(auto) v_ref = *std::as_const(it);
assert(v_ref == 'a');
std::ranges::advance(it, 2);
std::same_as<char&> decltype(auto) pattern_ref = *it;
assert(pattern_ref == '>');
}
{
auto cit = std::as_const(jwv).begin();
std::same_as<const char&> decltype(auto) cv_ref = *cit;
assert(cv_ref == 'a');
std::ranges::advance(cit, 3);
std::same_as<const char&> decltype(auto) cpattern_ref = *std::as_const(cit);
assert(cpattern_ref == '<');
}
}
{ // Result of `operator*` is const lvalue reference
using V = std::ranges::owning_view<std::vector<std::string_view>>;
using Pattern = std::string_view;
using JWV = std::ranges::join_with_view<V, Pattern>;
JWV jwv(V{{"123", "456", "789"}}, Pattern{"._."});
{
auto it = jwv.begin();
std::same_as<const char&> decltype(auto) v_ref = *it;
assert(v_ref == '1');
std::ranges::advance(it, 3);
std::same_as<const char&> decltype(auto) pattern_ref = *std::as_const(it);
assert(pattern_ref == '.');
}
{
auto cit = std::as_const(jwv).begin();
std::same_as<const char&> decltype(auto) cv_ref = *std::as_const(cit);
assert(cv_ref == '1');
std::ranges::advance(cit, 4);
std::same_as<const char&> decltype(auto) cpattern_ref = *cit;
assert(cpattern_ref == '_');
}
}
{ // Result of `operator*` is prvalue
using V = std::vector<std::string_view>;
using Pattern = RvalueVector<char>;
using JWV = std::ranges::join_with_view<std::ranges::owning_view<V>, std::ranges::owning_view<Pattern>>;
JWV jwv(V{"x^2", "y^2", "z^2"}, Pattern{{' ', '+', ' '}});
{
auto it = jwv.begin();
std::same_as<char> decltype(auto) v_ref = *std::as_const(it);
assert(v_ref == 'x');
std::ranges::advance(it, 3);
std::same_as<char> decltype(auto) pattern_ref = *it;
assert(pattern_ref == ' ');
}
{
auto cit = std::as_const(jwv).begin();
std::same_as<char> decltype(auto) cv_ref = *cit;
assert(cv_ref == 'x');
std::ranges::advance(cit, 4);
std::same_as<char> decltype(auto) cpattern_ref = *std::as_const(cit);
assert(cpattern_ref == '+');
}
}
{ // Result of `operator*` is (maybe const) rvalue reference
using Inner = std::ranges::as_rvalue_view<std::ranges::owning_view<std::string>>;
using V = std::ranges::owning_view<std::vector<Inner>>;
using Pattern = std::ranges::as_rvalue_view<std::ranges::owning_view<std::array<char, 2>>>;
using JWV = std::ranges::join_with_view<V, Pattern>;
std::vector<Inner> vec;
vec.emplace_back(Inner{{"x*y"}});
vec.emplace_back(Inner{{"y*z"}});
vec.emplace_back(Inner{{"z*x"}});
JWV jwv(V(std::move(vec)), Pattern(std::array{',', ' '}));
{
auto it = jwv.begin();
std::same_as<char&&> decltype(auto) v_ref = *it;
assert(v_ref == 'x');
std::ranges::advance(it, 3);
std::same_as<char&&> decltype(auto) pattern_ref = *std::as_const(it);
assert(pattern_ref == ',');
}
{
auto cit = std::as_const(jwv).begin();
std::same_as<const char&&> decltype(auto) cv_ref = *std::as_const(cit);
assert(cv_ref == 'x');
std::ranges::advance(cit, 4);
std::same_as<const char&&> decltype(auto) cpattern_ref = *cit;
assert(cpattern_ref == ' ');
}
}
{ // Result of `operator*` is type different from range_reference_t<InnerRng> and range_reference_t<Pattern>
using Inner = std::vector<int>;
using V = std::vector<Inner>;
using Pattern = std::ranges::subrange<ProxyIter, ProxyIter>;
using JWV = std::ranges::join_with_view<std::ranges::owning_view<V>, Pattern>;
static_assert(!std::same_as<std::ranges::range_reference_t<V>, std::ranges::range_reference_t<JWV>>);
static_assert(!std::same_as<std::ranges::range_reference_t<Pattern>, std::ranges::range_reference_t<JWV>>);
std::array<int, 2> pattern = {-1, -1};
Pattern pattern_as_subrange(ProxyIter{pattern.data()}, ProxyIter{pattern.data() + pattern.size()});
JWV jwv(V{Inner{1, 1}, Inner{2, 2}, Inner{3, 3}}, pattern_as_subrange);
auto it = jwv.begin();
std::same_as<CommonProxyRef> decltype(auto) v_ref = *it;
assert(v_ref.get() == 1);
std::ranges::advance(it, 7);
std::same_as<CommonProxyRef> decltype(auto) pattern_ref = *std::as_const(it);
assert(pattern_ref.get() == -1);
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|