Debian Codegraph @

clang/test/SemaCXX/cxx2c-fold-exprs.cpp

raw
1 // RUN: %clang_cc1 -std=c++2c -verify %s
2
3 template <class T> concept A = (T(), true);
4 template <class T> concept C = A<T> && true; // #C
5 template <class T> concept D = A<T> && __is_same(T, int);
6
7
8 template <class T> requires (A<T>)
9 constexpr int f(T) { return 0; };
10 template <class... T> requires (C<T> && ...)
11 constexpr int f(T...) { return 1; };
12
13 static_assert(f(0) == 0);
14 static_assert(f(1) == 0);
15
16
17 template <class... T> requires (A<T> && ...)
18 constexpr int g(T...) { return 0; };
19 template <class... T> requires (C<T> && ...)
20 constexpr int g(T...) { return 1; };
21
22 static_assert(g(0) == 1);
23 static_assert(g() == 1);
24 static_assert(g(1, 2) == 1);
25
26
27
28 template <class... T> requires (A<T> && ...)
29 constexpr int h(T...) { return 0; }; // expected-note {{candidate}}
30 template <class... T> requires (C<T> || ...)
31 constexpr int h(T...) { return 1; }; // expected-note {{candidate}}
32
33 static_assert(h(0) == 1); // expected-error {{call to 'h' is ambiguous}}
34
35 template <class... T> requires (A<T> || ...)
36 constexpr int i(T...) { return 0; }; // expected-note {{candidate}}
37 template <class... T> requires (C<T> && ...)
38 constexpr int i(T...) { return 1; }; // expected-note {{candidate}}
39
40 static_assert(i(0) == 1); // expected-error {{call to 'i' is ambiguous}}
41
42
43 template <class... T> requires (A<T> || ... || true) constexpr int j(T...) { return 0; }; // #j1
44 template <class... T> requires (C<T> && ... && true) constexpr int j(T...) { return 1; }; // #j2
45
46 static_assert(j(0) == 1);
47 // expected-error@-1 {{call to 'j' is ambiguous}}
48 // expected-note@#j1 {{candidate function [with T = <int>]}}
49 // expected-note@#j2 {{candidate function [with T = <int>]}}
50 // expected-note@#j2 {{imilar constraint expressions not considered equivalent}}
51 // expected-note@#j1 {{similar constraint expression here}}
52
53
54 static_assert(j() == 1);
55 // expected-error@-1 {{call to 'j' is ambiguous}}
56 // expected-note@#j1 {{candidate function [with T = <>]}}
57 // expected-note@#j2 {{candidate function [with T = <>]}}
58 // expected-note@#j2 {{imilar constraint expressions not considered equivalent}}
59 // expected-note@#j1 {{similar constraint expression here}}
60
61
62
63 template <class... T> requires (A<T> || ...)
64 constexpr int k(T...) { return 0; }; // expected-note {{candidate template ignored: constraints not satisfied [with T = <>]}}
65 template <class... T> requires (C<T> || ...)
66 constexpr int k(T...) { return 1; }; // expected-note {{candidate template ignored: constraints not satisfied [with T = <>]}}
67
68 static_assert(k(0) == 1);
69 static_assert(k() == 0); // expected-error {{no matching function for call to 'k'}}
70 static_assert(k(1, 2) == 1);
71
72
73 consteval int terse(A auto...) {return 1;}
74 consteval int terse(D auto...) {return 2;}
75
76 static_assert(terse() == 2);
77 static_assert(terse(0, 0) == 2);
78 static_assert(terse(0L, 0) == 1);
79
80 template <A... T>
81 consteval int tpl_head(A auto...) {return 1;}
82 template <D... T>
83 consteval int tpl_head(D auto...) {return 2;}
84
85 static_assert(tpl_head() == 2);
86 static_assert(tpl_head(0, 0) == 2);
87 static_assert(tpl_head(0L, 0) == 1);
88
89
90 namespace equivalence {
91
92 template <typename... T>
93 struct S {
94 template <typename... U>
95 void f() requires (A<U> && ...);
96 template <typename... U>
97 void f() requires (C<U> && ...);
98
99 template <typename... U>
100 void g() requires (A<T> && ...);
101 template <typename... U>
102 void g() requires (C<T> && ...);
103
104 template <typename... U>
105 void h() requires (A<U> && ...); // expected-note {{candidate}}
106 template <typename... U>
107 void h() requires (C<T> && ...); // expected-note {{candidate}}
108 };
109
110 void test() {
111 S<int>{}.f<int>();
112 S<int>{}.g<int>();
113 S<int>{}.h<int>(); // expected-error {{call to member function 'h' is ambiguous}}
114 }
115
116
117 }
118
119 namespace substitution {
120 struct S {
121 using type = int;
122 };
123
124 template <typename... T>
125 consteval int And1() requires (C<typename T::type> && ...) { // #and1
126 return 1;
127 }
128
129 template <typename T, typename... U>
130 consteval int And2() requires (C<typename U::type> && ... && C<typename T::type>) { // #and2
131 return 2;
132 }
133
134 template <typename T, typename... U>
135 consteval int And3() requires (C<typename T::type> && ... && C<typename U::type>) { // #and3
136 return 3;
137 }
138
139 template <typename... T>
140 consteval int Or1() requires (C<typename T::type> || ...) { // #or1
141 return 1;
142 }
143
144 template <typename T, typename... U>
145 consteval int Or2() requires (C<typename U::type> || ... || C<typename T::type>) { // #or2
146 return 2;
147 }
148
149 template <typename T, typename... U>
150 consteval int Or3() requires (C<typename T::type> || ... || C<typename U::type>) { // #or3
151 return 3;
152 }
153
154 static_assert(And1<>() == 1);
155 static_assert(And1<S>() == 1);
156 static_assert(And1<S, S>() == 1);
157 // FIXME: The diagnostics are not so great
158 static_assert(And1<int>() == 1); // expected-error {{no matching function for call to 'And1'}}
159 // expected-note@#and1 {{candidate template ignored: constraints not satisfied [with T = <int>]}}
160 // expected-note@#and1 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
161
162 static_assert(And1<S, int>() == 1); // expected-error {{no matching function for call to 'And1'}}
163 // expected-note@#and1 {{candidate template ignored: constraints not satisfied [with T = <S, int>]}}
164 // expected-note@#and1 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
165
166 static_assert(And1<int, S>() == 1); // expected-error {{no matching function for call to 'And1'}}
167 // expected-note@#and1 {{candidate template ignored: constraints not satisfied [with T = <int, S>]}}
168 // expected-note@#and1 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
169
170 static_assert(And2<S>() == 2);
171 static_assert(And2<S, S>() == 2);
172 static_assert(And2<int>() == 2); // expected-error {{no matching function for call to 'And2'}}
173 // expected-note@#and2 {{candidate template ignored: constraints not satisfied [with T = int, U = <>]}}
174 // expected-note@#and2 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
175
176
177 static_assert(And2<int, int>() == 2); // expected-error {{no matching function for call to 'And2'}}
178 // expected-note@#and2 {{candidate template ignored: constraints not satisfied [with T = S, U = <int>]}} \
179 // expected-note@#and2 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
180
181 static_assert(And2<S, int>() == 2); // expected-error {{no matching function for call to 'And2'}}
182 // expected-note@#and2 {{candidate template ignored: constraints not satisfied [with T = int, U = <S>]}}
183 // expected-note@#and2 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
184
185 static_assert(And2<int, S>() == 2); // expected-error {{no matching function for call to 'And2'}}
186 // expected-note@#and2 {{candidate template ignored: constraints not satisfied [with T = int, U = <int>]}}
187 // expected-note@#and2 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
188
189 static_assert(And3<S>() == 3);
190 static_assert(And3<S, S>() == 3);
191 static_assert(And3<int>() == 3); // expected-error {{no matching function for call to 'And3'}}
192 // expected-note@#and3 {{candidate template ignored: constraints not satisfied [with T = int, U = <>]}}
193 // expected-note@#and3 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
194
195
196 static_assert(And3<int, int>() == 3); // expected-error {{no matching function for call to 'And3'}}
197 // expected-note@#and3 {{candidate template ignored: constraints not satisfied [with T = int, U = <int>]}}
198 // expected-note@#and3 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
199
200
201 static_assert(And3<S, int>() == 3); // expected-error {{no matching function for call to 'And3'}}
202 // expected-note@#and3 {{candidate template ignored: constraints not satisfied [with T = S, U = <int>]}}
203 // expected-note@#and3 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
204
205
206 static_assert(And3<int, S>() == 3); // expected-error {{no matching function for call to 'And3'}}
207 // expected-note@#and3 {{candidate template ignored: constraints not satisfied [with T = int, U = <S>]}}
208 // expected-note@#and3 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
209
210
211 static_assert(Or1<>() == 1); // expected-error {{no matching function for call to 'Or1'}}
212 // expected-note@#or1 {{candidate template ignored: constraints not satisfied}}
213 static_assert(Or1<S>() == 1);
214 static_assert(Or1<int, S>() == 1);
215 static_assert(Or1<S, int>() == 1);
216 static_assert(Or1<S, S>() == 1);
217 static_assert(Or1<int>() == 1); // expected-error {{no matching function for call to 'Or1'}}
218 // expected-note@#or1 {{candidate template ignored: constraints not satisfied}}
219 // expected-note@#or1 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
220
221 static_assert(Or2<S>() == 2);
222 static_assert(Or2<int, S>() == 2);
223 static_assert(Or2<S, int>() == 2);
224 static_assert(Or2<S, S>() == 2);
225 static_assert(Or2<int>() == 2); // expected-error {{no matching function for call to 'Or2'}}
226 // expected-note@#or2 {{candidate template ignored: constraints not satisfied [with T = int, U = <>]}}
227 // expected-note@#or2 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
228 static_assert(Or3<S>() == 3);
229 static_assert(Or3<int, S>() == 3);
230 static_assert(Or3<S, int>() == 3);
231 static_assert(Or3<S, S>() == 3);
232 static_assert(Or3<int>() == 3); // expected-error {{no matching function for call to 'Or3'}}
233 // expected-note@#or3 {{candidate template ignored: constraints not satisfied}}
234 // expected-note@#or3 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
235 }
236
237 namespace bool_conversion_break {
238
239 template <typename ...V> struct A;
240 struct Thingy {
241 static constexpr int compare(const Thingy&) {return 1;}
242 };
243 template <typename ...T, typename ...U>
244 void f(A<T ...> *, A<U ...> *) // expected-note {{candidate template ignored: constraints not satisfied}}
245 requires (T::compare(U{}) && ...); // expected-error {{atomic constraint must be of type 'bool' (found 'int')}}
246
247 void g() {
248 A<Thingy, Thingy> *ap;
249 f(ap, ap); // expected-error{{no matching function for call to 'f'}} \
250 // expected-note {{while checking constraint satisfaction}} \
251 // expected-note {{while substituting deduced template arguments}}
252 }
253
254 }
255
256 namespace nested {
257
258 template <typename... T>
259 struct S {
260 template <typename... U>
261 consteval static int f()
262 requires ((A<T> && ...) && ... && A<U> ) {
263 return 1;
264 }
265
266 template <typename... U>
267 consteval static int f()
268 requires ((C<T> && ...) && ... && C<U> ) {
269 return 2;
270 }
271
272 template <typename... U>
273 consteval static int g() // #nested-ambiguous-g1
274 requires ((A<T> && ...) && ... && A<U> ) {
275 return 1;
276 }
277
278 template <typename... U>
279 consteval static int g() // #nested-ambiguous-g2
280 requires ((C<U> && ...) && ... && C<T> ) {
281 return 2;
282 }
283 };
284
285 static_assert(S<int>::f<int>() == 2);
286
287 static_assert(S<int>::g<int>() == 2);
288
289
290 }
291
292 namespace GH99430 {
293
294 template <class _Ty1, class _Ty2>
295 using _Synth_three_way_result = int;
296
297 template <class... _Types>
298 class tuple;
299
300 template <int _Index>
301 struct tuple_element;
302
303 template <class, int...>
304 struct _Three_way_comparison_result_with_tuple_like {
305 using type = int;
306 };
307
308 template <class... _TTypes, int... _Indices>
309 requires(requires {
310 typename _Synth_three_way_result<_TTypes, tuple_element<_Indices>>;
311 } && ...)
312
313 struct _Three_way_comparison_result_with_tuple_like<tuple<_TTypes...>, _Indices...>{
314 using type = long;
315 };
316
317 static_assert(__is_same_as(_Three_way_comparison_result_with_tuple_like<tuple<int>, 0, 1>::type, int));
318 static_assert(__is_same_as(_Three_way_comparison_result_with_tuple_like<tuple<int>, 0>::type, long));
319
320 }
321
322 namespace GH88866 {
323
324 template <typename...Ts> struct index_by;
325
326 template <typename T, typename Indices>
327 concept InitFunc = true;
328
329 namespace ExpandsBoth {
330
331 template <typename Indices, InitFunc<Indices> auto... init>
332 struct LazyLitMatrix; // expected-note {{here}}
333
334 template <
335 typename...Indices,
336 InitFunc<index_by<Indices>> auto... init
337 >
338 struct LazyLitMatrix<index_by<Indices...>, init...> {
339 };
340
341 // FIXME: Explain why we didn't pick up the partial specialization - pack sizes don't match.
342 template struct LazyLitMatrix<index_by<int, char>, 42>;
343 // expected-error@-1 {{instantiation of undefined template}}
344 template struct LazyLitMatrix<index_by<int, char>, 42, 43>;
345
346 }
347
348 namespace ExpandsRespectively {
349
350 template <typename Indices, InitFunc<Indices> auto... init>
351 struct LazyLitMatrix;
352
353 template <
354 typename...Indices,
355 InitFunc<index_by<Indices...>> auto... init
356 >
357 struct LazyLitMatrix<index_by<Indices...>, init...> {
358 };
359
360 template struct LazyLitMatrix<index_by<int, char>, 42>;
361 template struct LazyLitMatrix<index_by<int, char>, 42, 43>;
362
363 }
364
365 namespace TypeParameter {
366
367 template <typename Indices, InitFunc<Indices>... init>
368 struct LazyLitMatrix; // expected-note {{here}}
369
370 template <
371 typename...Indices,
372 InitFunc<index_by<Indices>>... init
373 >
374 struct LazyLitMatrix<index_by<Indices...>, init...> {
375 };
376
377 // FIXME: Explain why we didn't pick up the partial specialization - pack sizes don't match.
378 template struct LazyLitMatrix<index_by<int, char>, float>;
379 // expected-error@-1 {{instantiation of undefined template}}
380 template struct LazyLitMatrix<index_by<int, char>, unsigned, float>;
381
382 }
383
384 namespace Invalid {
385
386 template <typename Indices, InitFunc<Indices>... init>
387 struct LazyLitMatrix;
388
389 template <
390 typename...Indices,
391 InitFunc<index_by<Indices>> init
392 // expected-error@-1 {{unexpanded parameter pack 'Indices'}}
393 >
394 struct LazyLitMatrix<index_by<Indices...>, init> {
395 };
396
397 }
398
399 }
400
401 namespace GH135190 {
402 template <typename T>
403 concept A = __is_same_as(T, int) || __is_same_as(T, double) ;
404
405 template <typename T>
406 concept B = A<T> && __is_same_as(T, double);
407
408 template <class... Ts>
409 requires(A<Ts> && ...)
410 constexpr int g() {
411 return 1;
412 }
413
414 template <class... Ts>
415 requires(B<Ts> && ...)
416 constexpr int g() {
417 return 2;
418 }
419
420 static_assert(g<double>() == 2);
421
422
423 template <class... Ts>
424 concept all_A = (A<Ts> && ...);
425
426 template <class... Ts>
427 concept all_B = (B<Ts> && ...);
428
429 template <class... Ts>
430 requires all_A<Ts...>
431 constexpr int h() {
432 return 1;
433 }
434
435 template <class... Ts>
436 requires all_B<Ts...>
437 constexpr int h() {
438 return 2;
439 }
440
441 static_assert(h<double>() == 2);
442 }
443
444
445 namespace parameter_mapping_regressions {
446
447 namespace case1 {
448 namespace std {
449 template <class _Tp, class... _Args>
450 constexpr bool is_constructible_v = __is_constructible(_Tp, _Args...);
451 template <class _Tp, class... _Args>
452 concept constructible_from = is_constructible_v<_Tp, _Args...>;
453 template <class _Tp>
454 concept default_initializable = true;
455 template <class> using iterator_t = int;
456 template <class _Tp>
457 concept view = constructible_from<_Tp, _Tp>;
458 template <class... _Views>
459 requires(view<_Views> && ...)
460 class zip_transform_view;
461 } // namespace std
462 struct IterDefaultCtrView {};
463 template <class... Views>
464 using Iter = std::iterator_t<std::zip_transform_view<Views...>>;
465 static_assert(
466 std::default_initializable<Iter<IterDefaultCtrView, IterDefaultCtrView>>);
467
468 }
469
470 namespace case2 {
471
472 template <class _Bp>
473 constexpr bool False = false;
474
475 template <class... _Views>
476 concept __zip_all_random_access = (False<_Views> && ...);
477 // expected-note@-1 {{evaluated to false}}
478
479 template <typename... _Views>
480 struct zip_view {
481 void f() requires __zip_all_random_access<_Views...>{};
482 // expected-note@-1 {{because 'int' does not satisfy}}
483 };
484
485 zip_view<int> test_v;
486 static_assert(!__zip_all_random_access<int>);
487
488 void test() {
489 test_v.f(); // expected-error {{invalid reference to function 'f'}}
490 }
491
492 }
493
494 namespace GH177245 {
495
496 template <class _Fun, class... _As>
497 concept __callable = requires (_Fun __fun, _As...) { __fun(); };
498
499 template <class... _Args>
500 struct __mdispatch {
501 template <class... _Ts>
502 requires (__callable<_Args, _Ts...> && ...)
503 void operator()();
504 };
505
506 static_assert(!__callable<__mdispatch<int>>);
507
508 }
509
510 }
511
512 namespace GH190169 {
513
514 namespace _1 {
515
516 template <typename Type, typename... Choices>
517 concept OneOf = (... || __is_same(Type, Choices));
518
519 template <typename F, typename... Ts>
520 using check = decltype((F{}(Ts{}), ...));
521
522 using X = check<decltype([](auto val) {
523 [](OneOf<int, char> auto) {}(val);
524 }), char>;
525
526 }
527
528 namespace _2 {
529
530 template <typename T, typename U>
531 concept Same = __is_same(T, U);
532
533 template <typename Type, typename... Choices>
534 concept OneOf = (... || Same<Type, Choices>);
535
536 template <class... Ts>
537 struct visitor : Ts... { using Ts::operator()...; };
538
539 struct A {};
540 struct B {};
541 struct C {};
542
543 template <typename F, typename T, typename = decltype(F{}(T{}))>
544 struct check {};
545
546 template <typename F, typename... Ts>
547 struct check_all : check<F, Ts>... {};
548
549 using F = decltype([](auto val) {
550 visitor{[](const A&) {},
551 [](const OneOf<B, C> auto&) {}}(val);
552 });
553
554 check_all<F, A, B, C> x;
555
556 }
557
558 }
559
560 namespace GH188505 {
561
562 template <class _Tp, class _Up>
563 concept same_as = __is_same(_Tp, _Up) && __is_same(_Up, _Tp);
564
565 template <typename T, typename... Ts>
566 concept AnyOf = (same_as<T, Ts> || ...);
567
568 struct Constant
569 {
570 template <typename T>
571 using alias = const T;
572 };
573
574 struct Mutable
575 {
576 template <typename T>
577 using alias = __remove_cv(T);
578 };
579
580 template <template <typename> typename M, typename T>
581 concept MutabilityAliasFor = requires {
582 requires AnyOf<M<T>, const T, __remove_cv(T)>;
583 requires same_as<M<M<T>>, M<T>>;
584 };
585
586 template <template <typename> typename M, typename... Ts>
587 concept MutabilityAliasForAllOf = (MutabilityAliasFor<M, Ts> && ...);
588
589 template <template <typename T> typename M>
590 concept MutabilityAlias = MutabilityAliasForAllOf<M, int, char, double>;
591
592 static_assert(AnyOf<char, const char, char>);
593
594 static_assert(MutabilityAliasFor<Constant::alias, int>);
595 static_assert(MutabilityAliasForAllOf<Constant::alias, char, int>);
596
597 static_assert(MutabilityAlias<Constant::alias>);
598 static_assert(MutabilityAlias<Mutable::alias>);
599
600 }