File: requirements.compile.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (272 lines) | stat: -rw-r--r-- 10,730 bytes parent folder | download | duplicates (7)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <algorithm>

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// template<input_iterator I, sentinel_for<I> S, class T,
//          indirectly-binary-left-foldable<T, I> F>
//   constexpr see below ranges::fold_left_with_iter(I first, S last, T init, F f);
//
// template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
//   constexpr see below ranges::fold_left_with_iter(R&& r, T init, F f);

// template<input_iterator I, sentinel_for<I> S, class T,
//          indirectly-binary-left-foldable<T, I> F>
//   constexpr see below ranges::fold_left(I first, S last, T init, F f);
//
// template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
//   constexpr see below ranges::fold_left(R&& r, T init, F f);

// Checks that the algorithm requirements reject parameters that don't meet the overloads' constraints.

#include <algorithm>
#include <concepts>
#include <cstddef>
#include <functional>
#include <iterator>
#include <ranges>

#include "test_iterators.h"

// FIXME(cjdb): deduplicate
struct bad_iterator_category {
  using value_type        = int;
  using difference_type   = std::ptrdiff_t;
  using iterator_category = void;

  value_type operator*() const;

  bad_iterator_category& operator++();
  void operator++(int);
};

// Covers indirectly_readable<I> too
template <std::input_or_output_iterator T>
  requires(!std::input_iterator<T>)
void requires_input_iterator() {
  struct bad_range {
    T begin();
    std::unreachable_sentinel_t end();
  };

  static_assert(!requires(bad_range r) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), std::unreachable_sentinel, 0, std::plus());
  });
  static_assert(!requires(bad_range r) { std::ranges::fold_left_with_iter(r, 0, std::plus()); });

  static_assert(!requires(bad_range r) {
    std::ranges::fold_left(r.begin(), r.end(), std::unreachable_sentinel, 0, std::plus());
  });

  static_assert(!requires(bad_range r) { std::ranges::fold_left(r, 0, std::plus()); });
}

template <std::equality_comparable S>
  requires(!std::sentinel_for<int*, S>)
void requires_sentinel() {
  static_assert(!requires(S first, S last) { std::ranges::fold_left_with_iter(first, last, 0, std::plus()); });
  static_assert(!requires(S first, S last) { std::ranges::fold_left(first, last, 0, std::plus()); });
}

struct non_copy_constructible_callable {
  non_copy_constructible_callable(non_copy_constructible_callable&&)      = default;
  non_copy_constructible_callable(non_copy_constructible_callable const&) = delete;

  int operator()(int, int) const;
};

template <class F>
  requires(!std::copy_constructible<F>)
void requires_copy_constructible_F() {
  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), 0, std::move(f));
  });
  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
    std::ranges::fold_left_with_iter(r, 0, std::move(f));
  });

  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
    std::ranges::fold_left(r.begin(), r.end(), 0, std::move(f));
  });
  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) { std::ranges::fold_left(r, 0, std::move(f)); });
}

struct not_invocable_with_lvalue_rhs {
  int operator()(int, int&&);
};

template <class F>
  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
void requires_raw_invocable() {
  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), 0, f);
  });
  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) { std::ranges::fold_left_with_iter(r, 0, f); });

  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
    std::ranges::fold_left(r.begin(), r.end(), 0, f);
  });
  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) { std::ranges::fold_left(r, 0, f); });
}

struct S {};

struct non_decayable_result {
  S volatile& operator()(S, S) const;
};

template <std::invocable<S, std::iter_reference_t<S*>> F>
  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
void requires_decaying_invoke_result() {
  static_assert(!requires(std::ranges::subrange<S*, S*> r, S init, F f) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, f);
  });
  static_assert(!requires(std::ranges::subrange<S*, S*> r, S init, F f) {
    std::ranges::fold_left_with_iter(r, init, f);
  });

  static_assert(!requires(std::ranges::subrange<S*, S*> r, S init, F f) {
    std::ranges::fold_left(r.begin(), r.end(), init, f);
  });
  static_assert(!requires(std::ranges::subrange<S*, S*> r, S init, F f) { std::ranges::fold_left(r, init, f); });
}

struct non_movable {
  non_movable(int);
  non_movable(non_movable&&) = delete;

  int apply(non_movable const&) const;
};

template <class T>
  requires(!std::movable<T>)
void requires_movable_init() {
  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, &T::apply);
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
    std::ranges::fold_left_with_iter(r, init, &T::apply);
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
    std::ranges::fold_left(r.begin(), r.end(), init, &T::apply);
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) { std::ranges::fold_left(r, init, &T::apply); });
}

struct result_not_movable_after_decay {
  result_not_movable_after_decay(int);
  result_not_movable_after_decay(result_not_movable_after_decay&&) = delete;
  result_not_movable_after_decay(result_not_movable_after_decay const&);

  friend result_not_movable_after_decay const& operator+(int, result_not_movable_after_decay const&);
  friend result_not_movable_after_decay const& operator+(result_not_movable_after_decay const&, int);
  friend result_not_movable_after_decay const&
  operator+(result_not_movable_after_decay const&, result_not_movable_after_decay const&);
};

template <class T>
  requires(!std::movable<T>)
void requires_movable_decayed() {
  static_assert(!requires(std::ranges::subrange<T*, T*> r) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), 0, std::plus());
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r) { std::ranges::fold_left_with_iter(r, 0, std::plus()); });

  static_assert(!requires(std::ranges::subrange<T*, T*> r) {
    std::ranges::fold_left(r.begin(), r.end(), 0, T::apply);
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r) { std::ranges::fold_left(r, 0, std::plus()); });
}

struct not_convertible_to_int {
  friend int operator+(not_convertible_to_int, not_convertible_to_int);
  friend int operator+(not_convertible_to_int, int);
  friend int operator+(int, not_convertible_to_int);
};

template <class T>
  requires(!std::convertible_to<T, int>)
void requires_init_is_convertible_to_decayed() {
  static_assert(!requires(std::ranges::subrange<int*, int*> r, T init) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, std::plus());
  });
  static_assert(!requires(std::ranges::subrange<int*, int*> r, T init) {
    std::ranges::fold_left_with_iter(r, init, std::plus());
  });

  static_assert(!requires(std::ranges::subrange<int*, int*> r, T init) {
    std::ranges::fold_left(r.begin(), r.end(), init, std::plus());
  });
  static_assert(!requires(std::ranges::subrange<int*, int*> r, T init) {
    std::ranges::fold_left(r, init, std::plus());
  });
}

struct not_invocable_with_decayed {
  not_invocable_with_decayed(int);
  friend not_invocable_with_decayed& operator+(int, not_invocable_with_decayed&);
  friend not_invocable_with_decayed& operator+(not_invocable_with_decayed&, int);
  friend not_invocable_with_decayed& operator+(not_invocable_with_decayed volatile&, not_invocable_with_decayed&);
};

template <class T>
  requires(!std::invocable<std::plus<>&, T, T&>)
void requires_invocable_with_decayed() {
  static_assert(!requires(std::ranges::subrange<T*, T*> r, int init) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, std::plus());
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r, int init) {
    std::ranges::fold_left_with_iter(r, init, std::plus());
  });

  static_assert(!requires(std::ranges::subrange<T*, T*> r, int init) {
    std::ranges::fold_left(r.begin(), r.end(), init, std::plus());
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r, int init) { std::ranges::fold_left(r, init, std::plus()); });
}

struct not_assignable_to_decayed {
  not_assignable_to_decayed();
  not_assignable_to_decayed(not_assignable_to_decayed&);
  not_assignable_to_decayed(not_assignable_to_decayed const&);
  not_assignable_to_decayed(not_assignable_to_decayed volatile&);
  not_assignable_to_decayed(not_assignable_to_decayed const volatile&);
  friend not_assignable_to_decayed volatile& operator+(not_assignable_to_decayed, not_assignable_to_decayed);
};

template <class T>
  requires(!std::assignable_from<T&, T volatile&>)
void requires_assignable_from_invoke_result() {
  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, std::plus());
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
    std::ranges::fold_left_with_iter(r, init, std::plus());
  });

  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
    std::ranges::fold_left(r.begin(), r.end(), init, std::plus());
  });
  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) { std::ranges::fold_left(r, init, std::plus()); });
}

void test() {
  requires_input_iterator<bad_iterator_category>();
  requires_sentinel<cpp17_input_iterator<int*>>();
  requires_copy_constructible_F<non_copy_constructible_callable>();
  requires_raw_invocable<not_invocable_with_lvalue_rhs>();
  requires_decaying_invoke_result<non_decayable_result>();
  requires_movable_init<non_movable>();
  requires_movable_decayed<result_not_movable_after_decay>();
  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
  requires_invocable_with_decayed<not_invocable_with_decayed>();
  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
}