Debian Codegraph @

clang/test/SemaCXX/cxx20-ctad-type-alias.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-linux -Wno-c++11-narrowing -Wno-literal-conversion -std=c++20 -verify %s
2
3 namespace test1 {
4 template <typename T>
5 struct Foo { T t; };
6 template <typename U>
7 using Bar = Foo<U>;
8
9 Bar s = {1};
10 } // namespace test1
11
12 namespace test2 {
13 template <typename X, typename Y>
14 struct XYpair {
15 X x;
16 Y y;
17 };
18 // A tricky explicit deduction guide that swapping X and Y.
19 template <typename X, typename Y>
20 XYpair(X, Y) -> XYpair<Y, X>;
21 template <typename U, typename V>
22 using AliasXYpair = XYpair<U, V>;
23
24 AliasXYpair xy = {1.1, 2}; // XYpair<int, double>
25 static_assert(__is_same(decltype(xy.x), int));
26 static_assert(__is_same(decltype(xy.y), double));
27 } // namespace test2
28
29 namespace test3 {
30 template <typename T, class>
31 struct container {
32 // test with default arguments.
33 container(T a, T b = T());
34 };
35
36 template <class T>
37 using vector = container<T, int>;
38 vector v(0, 0);
39 } // namespace test3
40
41 namespace test4 {
42 // Explicit deduction guide.
43 template <class T>
44 struct X {
45 T t;
46 X(T);
47 };
48
49 template <class T>
50 X(T) -> X<double>;
51
52 template <class T>
53 using AX = X<T>;
54
55 AX s = {1};
56 static_assert(__is_same(decltype(s.t), double)); // explicit one is picked.
57 } // namespace test4
58
59 namespace test5 {
60 template <int B>
61 struct Foo {};
62 // Template parameter pack
63 template <int... C>
64 using AF = Foo<1>;
65 auto a = AF{};
66 } // namespace test5
67
68 namespace test6 {
69 // non-type template argument.
70 template <typename T, bool B = false>
71 struct Foo {
72 Foo(T);
73 };
74 template <typename T>
75 using AF = Foo<T, 1>;
76
77 AF b{0};
78 } // namespace test6
79
80 namespace test7 {
81 template <typename T>
82 struct Foo {
83 Foo(T);
84 };
85 // using alias chain.
86 template <typename U>
87 using AF1 = Foo<U>;
88 template <typename K>
89 using AF2 = AF1<K>;
90 AF2 b = 1;
91 } // namespace test7
92
93 namespace test8 {
94 template <typename T, int N>
95 struct Foo {
96 Foo(T const (&)[N]);
97 };
98
99 template <typename X, int Y>
100 using Bar = Foo<X, Y>;
101
102 Bar s = {{1}};
103 } // namespace test8
104
105 namespace test9 {
106 template <typename T, int N>
107 struct Foo {
108 Foo(T const (&)[N]);
109 };
110
111 template <typename X, int Y>
112 using Bar = Foo<X, sizeof(X)>; // expected-note {{candidate template ignored: couldn't infer template argument 'X'}} \
113 // expected-note {{implicit deduction guide declared as 'template <typename X> requires __is_deducible(test9::Bar, test9::Foo<X, sizeof(X)>) Bar(test9::Foo<X, sizeof(X)>) -> test9::Foo<X, sizeof(X)>'}} \
114 // expected-note {{implicit deduction guide declared as 'template <typename X> requires __is_deducible(test9::Bar, test9::Foo<X, sizeof(X)>) Bar(const X (&)[sizeof(X)]) -> test9::Foo<X, sizeof(X)>'}} \
115 // expected-note {{candidate template ignored: constraints not satisfied [with X = int]}} \
116 // expected-note {{cannot deduce template arguments for 'test9::Bar' from 'test9::Foo<int, sizeof(int)>'}}
117
118
119 Bar s = {{1}}; // expected-error {{no viable constructor or deduction guide }}
120 } // namespace test9
121
122 namespace test10 {
123 template <typename T>
124 struct Foo {
125 template <typename U>
126 Foo(U);
127 };
128
129 template <typename U>
130 Foo(U) -> Foo<U*>;
131
132 template <typename K>
133 using A = Foo<K>;
134 A a(2); // Foo<int*>
135 } // namespace test10
136
137 namespace test11 {
138 struct A {};
139 template<class T> struct Foo { T c; };
140 template<class X, class Y=A>
141 using AFoo = Foo<Y>; // expected-note {{candidate template ignored: could not match 'test11::Foo<Y>' against 'int'}} \
142 // expected-note {{implicit deduction guide declared as 'template <class Y = A> requires __is_deducible(test11::AFoo, test11::Foo<Y>) AFoo(test11::Foo<Y>) -> test11::Foo<Y>'}} \
143 // expected-note {{candidate template ignored: constraints not satisfied [with Y = int]}} \
144 // expected-note {{cannot deduce template arguments for 'test11::AFoo' from 'test11::Foo<int>'}} \
145 // expected-note {{implicit deduction guide declared as 'template <class Y = A> requires __is_deducible(test11::AFoo, test11::Foo<Y>) AFoo(Y) -> test11::Foo<Y>'}} \
146 // expected-note {{candidate function template not viable: requires 0 arguments, but 1 was provided}} \
147 // expected-note {{implicit deduction guide declared as 'template <class Y = A> requires __is_deducible(test11::AFoo, test11::Foo<Y>) AFoo() -> test11::Foo<Y>'}}
148
149 AFoo s = {1}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'AFoo'}}
150 } // namespace test11
151
152 namespace test12 {
153 // no crash on null access attribute
154 template<typename X>
155 struct Foo {
156 template<typename K>
157 struct Bar {
158 Bar(K);
159 };
160
161 template<typename U>
162 using ABar = Bar<U>;
163 void test() { ABar k = 2; }
164 };
165
166 void func(Foo<int> s) {
167 s.test();
168 }
169 } // namespace test12
170
171 namespace test13 {
172 template <typename... Ts>
173 struct Foo {
174 Foo(Ts...);
175 };
176
177 template <typename... Ts>
178 using AFoo = Foo<Ts...>;
179
180 auto b = AFoo{};
181 AFoo a(1, 2);
182
183 template <typename T>
184 using BFoo = Foo<T, T>;
185 BFoo b2(1.0, 2.0);
186 } // namespace test13
187
188 namespace test14 {
189 template<typename T>
190 concept IsInt = __is_same(decltype(T()), int);
191
192 template<IsInt T, int N>
193 struct Foo {
194 Foo(T const (&)[N]);
195 };
196
197 template <int K>
198 using Bar = Foo<double, K>; // expected-note {{constraints not satisfied for class template 'Foo'}}
199 // expected-note@-1 {{candidate template ignored: could not match}} expected-note@-1 {{candidate template ignored: constraints not satisfied}}
200 // expected-note@-2 {{implicit deduction guide declared as 'template <int K> requires __is_deducible(test14::Bar, test14::Foo<double, K>) Bar(test14::Foo<double, K>) -> test14::Foo<double, K>'}}
201 // expected-note@-3 {{implicit deduction guide declared as 'template <int K> requires __is_deducible(test14::Bar, test14::Foo<double, K>) Bar(const double (&)[K]) -> test14::Foo<double, K>'}}
202 double abc[3];
203 Bar s2 = {abc}; // expected-error {{no viable constructor or deduction guide for deduction }}
204 } // namespace test14
205
206 namespace test15 {
207 template <class T> struct Foo { Foo(T); };
208
209 template<class V> using AFoo = Foo<V *>;
210 template<typename> concept False = false; // #test15_False
211 template<False W>
212 using BFoo = AFoo<W>; // expected-note {{candidate template ignored: constraints not satisfied [with W = int]}} \
213 // expected-note@-1 {{because 'int' does not satisfy 'False'}} \
214 // expected-note@#test15_False {{because 'false' evaluated to false}} \
215 // expected-note {{implicit deduction guide declared as 'template <False<> W> requires __is_deducible(test15::AFoo, test15::Foo<W *>) && __is_deducible(test15::BFoo, test15::Foo<W *>) BFoo(W *) -> test15::Foo<W *>}} \
216 // expected-note {{candidate template ignored: could not match 'test15::Foo<W *>' against 'int *'}} \
217 // expected-note {{template <False<> W> requires __is_deducible(test15::AFoo, test15::Foo<W *>) && __is_deducible(test15::BFoo, test15::Foo<W *>) BFoo(test15::Foo<W *>) -> test15::Foo<W *>}}
218 int i = 0;
219 AFoo a1(&i); // OK, deduce Foo<int *>
220
221 // the W is not deduced from the deduced type Foo<int *>.
222 BFoo b2(&i); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'BFoo'}}
223 } // namespace test15
224
225 namespace test16 {
226 struct X { X(int); X(const X&); };
227 template<class T>
228 struct Foo {
229 T t;
230 Foo(T t) : t(t) {}
231 };
232 template<class T>
233 using AFoo = Foo<T>;
234 int i = 0;
235 AFoo s{i};
236 static_assert(__is_same(decltype(s.t), int));
237
238 template<class T>
239 using BFoo = AFoo<T>;
240
241 // template explicit deduction guide.
242 template<class T>
243 Foo(T) -> Foo<float>;
244 static_assert(__is_same(decltype(AFoo(i).t), float));
245 static_assert(__is_same(decltype(BFoo(i).t), float));
246
247 // explicit deduction guide.
248 Foo(int) -> Foo<X>;
249 static_assert(__is_same(decltype(AFoo(i).t), X));
250 static_assert(__is_same(decltype(BFoo(i).t), X));
251
252 Foo(double) -> Foo<int>;
253 static_assert(__is_same(decltype(AFoo(1.0).t), int));
254 static_assert(__is_same(decltype(BFoo(1.0).t), int));
255 } // namespace test16
256
257 namespace test17 {
258 template <typename T>
259 struct Foo { T t; };
260
261 // CTAD for alias templates only works for the RHS of the alias of form of
262 // [typename] [nested-name-specifier] [template] simple-template-id
263 template <typename U>
264 using AFoo = Foo<U>*; // expected-note {{template is declared here}}
265
266 AFoo s = {1}; // expected-error {{alias template 'AFoo' requires template arguments; argument deduction only allowed for}}
267 } // namespace test17
268
269 namespace test18 {
270 template<typename T>
271 concept False = false; // expected-note {{because 'false' evaluated to false}}
272
273 template <typename T> struct Foo { T t; };
274
275 template<typename T> requires False<T> // expected-note {{because 'int' does not satisfy 'False'}}
276 Foo(T) -> Foo<int>;
277
278 template <typename U>
279 using Bar = Foo<U>; // expected-note {{could not match 'test18::Foo<U>' against 'int'}} \
280 // expected-note {{implicit deduction guide declared as 'template <typename U> requires __is_deducible(test18::Bar, test18::Foo<U>) Bar(test18::Foo<U>) -> test18::Foo<U>'}} \
281 // expected-note {{candidate template ignored: constraints not satisfied}} \
282 // expected-note {{implicit deduction guide declared as 'template <typename T> requires False<T> && __is_deducible(test18::Bar, Foo<int>) Bar(T) -> Foo<int>'}} \
283 // expected-note {{candidate function template not viable}} \
284 // expected-note {{implicit deduction guide declared as 'template <typename U> requires __is_deducible(test18::Bar, test18::Foo<U>) Bar() -> test18::Foo<U>'}}
285
286 Bar s = {1}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}
287 } // namespace test18
288
289 // GH85406, verify no crash on invalid alias templates.
290 namespace test19 {
291 template <typename T>
292 class Foo {};
293
294 template <typename T>
295 template <typename K>
296 using Bar2 = Foo<K>; // expected-error {{extraneous template parameter list in alias template declaration}}
297
298 Bar2 b = 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}
299 } // namespace test19
300
301 // GH85385
302 namespace test20 {
303 template <template <typename> typename T>
304 struct K {};
305
306 template <typename U>
307 class Foo {};
308
309 // Verify that template template type parameter TTP is referenced/used in the
310 // template arguments of the RHS.
311 template <template<typename> typename TTP>
312 using Bar = Foo<K<TTP>>; // expected-note {{candidate template ignored: could not match 'test20::Foo<K<TTP>>' against 'int'}} \
313 // expected-note {{implicit deduction guide declared as 'template <template <typename> typename TTP> requires __is_deducible(test20::Bar, test20::Foo<K<TTP>>) Bar(test20::Foo<K<TTP>>) -> test20::Foo<K<TTP>>'}}
314
315 template <class T>
316 class Container {};
317 Bar t = Foo<K<Container>>();
318
319 Bar s = 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of}}
320 } // namespace test20
321
322 namespace test21 {
323 template <typename T, unsigned N>
324 struct Array { const T member[N]; };
325 template <unsigned N>
326 using String = Array<char, N>;
327
328 // Verify no crash on constructing the aggregate deduction guides.
329 String s("hello");
330 } // namespace test21
331
332 // GH89013
333 namespace test22 {
334 class Base {};
335 template <typename T>
336 class Derived final : public Base {};
337
338 template <typename T, typename D>
339 requires __is_base_of(Base, D)
340 struct Foo {
341 explicit Foo(D) {}
342 };
343
344 template <typename U>
345 using AFoo = Foo<int, Derived<U>>;
346
347 AFoo a(Derived<int>{});
348 } // namespace test22
349
350 namespace test23 {
351 // We have an aggregate deduction guide "G(T) -> G<T>".
352 template<typename T>
353 struct G { T t1; };
354
355 template<typename X = int>
356 using AG = G<int>;
357
358 AG ag(1.0);
359 // Verify that the aggregate deduction guide "AG(int) -> AG<int>" is built and
360 // choosen.
361 static_assert(__is_same(decltype(ag.t1), int));
362 } // namespace test23
363
364 // GH90177
365 // verify that the transformed require-clause of the alias deduction gudie has
366 // the right depth info.
367 namespace test24 {
368 class Forward;
369 class Key {};
370
371 template <typename D>
372 constexpr bool C = sizeof(D);
373
374 // Case1: the alias template and the underlying deduction guide are in the same
375 // scope.
376 template <typename T>
377 struct Case1 {
378 template <typename U>
379 struct Foo {
380 Foo(U);
381 };
382
383 template <typename V>
384 requires (C<V>)
385 Foo(V) -> Foo<V>;
386
387 template <typename Y>
388 using Alias = Foo<Y>;
389 };
390 // The require-clause should be evaluated on the type Key.
391 Case1<Forward>::Alias t2 = Key();
392
393
394 // Case2: the alias template and underlying deduction guide are in different
395 // scope.
396 template <typename T>
397 struct Foo {
398 Foo(T);
399 };
400 template <typename U>
401 requires (C<U>)
402 Foo(U) -> Foo<U>;
403
404 template <typename T>
405 struct Case2 {
406 template <typename Y>
407 using Alias = Foo<Y>;
408 };
409 // The require-caluse should be evaluated on the type Key.
410 Case2<Forward>::Alias t1 = Key();
411
412 // Case3: crashes on the constexpr evaluator due to the mixed-up depth in
413 // require-expr.
414 template <class T1>
415 struct A1 {
416 template<class T2>
417 struct A2 {
418 template <class T3>
419 struct Foo {
420 Foo(T3);
421 };
422 template <class T3>
423 requires C<T3>
424 Foo(T3) -> Foo<T3>;
425 };
426 };
427 template <typename U>
428 using AFoo = A1<int>::A2<int>::Foo<U>;
429 AFoo case3(1);
430
431 // Case4: crashes on the constexpr evaluator due to the mixed-up index for the
432 // template parameters `V`.
433 template<class T, typename T2>
434 struct Case4 {
435 template<class V> requires C<V>
436 Case4(V, T);
437 };
438
439 template<class T2>
440 using ACase4 = Case4<T2, T2>;
441 ACase4 case4{0, 1};
442
443 } // namespace test24
444
445 namespace test25 {
446
447 template<typename T, typename...Us>
448 struct A{
449 template<typename V> requires __is_same(V, int)
450 A(V);
451 };
452
453 template<typename...TS>
454 using AA = A<int, TS...>;
455
456 template<typename...US>
457 using BB = AA<US...>; // #test25_BB
458
459 BB a{0};
460 static_assert(__is_same(decltype(a), A<int>));
461 // FIXME: The template parameter list of generated deduction guide is not strictly conforming,
462 // as the pack occurs prior to the non-packs.
463 BB b{0, 1};
464 // expected-error@-1 {{no viable}}
465 // expected-note@#test25_BB 2{{not viable}}
466 // expected-note@#test25_BB {{template <typename ...US, typename V> requires __is_same(V, int) && __is_deducible(test25::AA, test25::A<int, US...>) && __is_deducible(test25::BB, test25::A<int, US...>) BB(V) -> test25::A<int, US...>}}
467 // expected-note@#test25_BB {{implicit deduction guide}}
468
469 }
470
471 namespace GH92212 {
472 template<typename T, typename...Us>
473 struct A{
474 template<typename V> requires __is_same(V, int)
475 A(V);
476 };
477
478 template<typename...TS>
479 using AA = A<int, TS...>;
480 AA a{0};
481 }
482
483 namespace GH94927 {
484 template <typename T>
485 struct A {
486 A(T);
487 };
488 A(int) -> A<char>;
489
490 template <typename U>
491 using B1 = A<U>;
492 B1 b1(100); // deduce to A<char>;
493 static_assert(__is_same(decltype(b1), A<char>));
494
495 template <typename U>
496 requires (!__is_same(U, char)) // filter out the explicit deduction guide.
497 using B2 = A<U>;
498 template <typename V>
499 using B3 = B2<V>;
500
501 B2 b2(100); // deduced to A<int>;
502 static_assert(__is_same(decltype(b2), A<int>));
503 B3 b3(100); // decuded to A<int>;
504 static_assert(__is_same(decltype(b3), A<int>));
505
506
507 // the nested case
508 template <typename T1>
509 struct Out {
510 template <typename T2>
511 struct A {
512 A(T2);
513 };
514 A(int) -> A<T1>;
515
516 template <typename T3>
517 using B = A<T3>;
518 };
519
520 Out<float>::B out(100); // deduced to Out<float>::A<float>;
521 static_assert(__is_same(decltype(out), Out<float>::A<float>));
522 }
523
524 namespace GH111508 {
525
526 template <typename V> struct S {
527 using T = V;
528 T Data;
529 };
530
531 template <typename V> using Alias = S<V>;
532
533 Alias A(42);
534
535 } // namespace GH111508
536
537 namespace GH113518 {
538
539 template <class T, unsigned N> struct array {
540 T value[N];
541 };
542
543 template <typename Tp, typename... Up>
544 array(Tp, Up...) -> array<Tp, 1 + sizeof...(Up)>;
545
546 template <typename T> struct ArrayType {
547 template <unsigned size> using Array = array<T, size>;
548 };
549
550 template <ArrayType<int>::Array array> void test() {}
551
552 void foo() { test<{1, 2, 3}>(); }
553
554 } // namespace GH113518
555
556 // FIXME: This is accepted by GCC: https://gcc.godbolt.org/z/f3rMfbacz
557 namespace GH125821 {
558 template<typename T>
559 struct A { A(T){} };
560
561 template<typename T>
562 using Proxy = T;
563
564 template<typename T>
565 using C = Proxy< A<T> >;
566
567 C test{ 42 }; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}
568
569 } // namespace GH125821
570
571 namespace GH133132 {
572
573 template <class T>
574 struct A {};
575
576 template <class T>
577 using Foo = A<A<T>>;
578
579 template <class T>
580 using Bar = Foo<T>;
581
582 template <class T = int>
583 using Baz = Bar<T>;
584
585 Baz a{};
586 static_assert(__is_same(decltype(a), A<A<int>>));
587
588 } // namespace GH133132
589
590 namespace GH131408 {
591
592 struct Node {};
593
594 template <class T, Node>
595 struct A {
596 A(T) {}
597 };
598
599 template <class T>
600 using AA = A<T, {}>;
601
602 AA a{0};
603
604 static_assert(__is_same(decltype(a), A<int, Node{}>));
605 }
606
607 namespace GH130604 {
608 template <typename T> struct A {
609 A(T);
610 };
611
612 template <typename T, template <typename> class TT = A> using Alias = TT<T>; // #gh130604-alias
613 template <typename T> using Alias2 = Alias<T>;
614
615 Alias2 a(42);
616 // expected-error@-1 {{no viable constructor or deduction guide for deduction of template arguments of 'Alias2'}}
617 Alias b(42);
618 // expected-error@-1 {{alias template 'Alias' requires template arguments; argument deduction only allowed for class templates or alias template}}
619 // expected-note@#gh130604-alias {{template is declared here}}
620 }