1 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-linux -Wno-c++11-narrowing -Wno-literal-conversion -std=c++20 -verify %s
13 template <typename X
, typename Y
>
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
>;
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));
30 template <typename T
, class>
32 // test with default arguments.
33 container(T a
, T b
= T());
37 using vector
= container
<T
, int>;
42 // Explicit deduction guide.
56 static_assert(__is_same(decltype(s
.t
), double)); // explicit one is picked.
62 // Template parameter pack
69 // non-type template argument.
70 template <typename T
, bool B
= false>
94 template <typename T
, int N
>
99 template <typename X
, int Y
>
100 using Bar
= Foo
<X
, Y
>;
106 template <typename T
, int N
>
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, Foo<X, sizeof(X)>) Bar(Foo<X, sizeof(X)>) -> Foo<X, sizeof(X)>'}} \
114 // expected-note {{implicit deduction guide declared as 'template <typename X> requires __is_deducible(test9::Bar, Foo<X, sizeof(X)>) Bar(const X (&)[sizeof(X)]) -> Foo<X, sizeof(X)>'}} \
115 // expected-note {{candidate template ignored: constraints not satisfied [with X = int]}} \
116 // expected-note {{cannot deduce template arguments for 'Bar' from 'Foo<int, 4UL>'}}
119 Bar s
= {{1}}; // expected-error {{no viable constructor or deduction guide }}
123 template <typename T
>
125 template <typename U
>
129 template <typename U
>
132 template <typename K
>
135 } // namespace test10
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 'Foo<Y>' against 'int'}} \
142 // expected-note {{implicit deduction guide declared as 'template <class Y = A> requires __is_deducible(test11::AFoo, Foo<Y>) AFoo(Foo<Y>) -> Foo<Y>'}} \
143 // expected-note {{candidate template ignored: constraints not satisfied [with Y = int]}} \
144 // expected-note {{cannot deduce template arguments for 'AFoo' from 'Foo<int>'}} \
145 // expected-note {{implicit deduction guide declared as 'template <class Y = A> requires __is_deducible(test11::AFoo, Foo<Y>) AFoo(Y) -> 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, Foo<Y>) AFoo() -> Foo<Y>'}}
149 AFoo s
= {1}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'AFoo'}}
150 } // namespace test11
153 // no crash on null access attribute
163 void test() { ABar k
= 2; }
166 void func(Foo
<int> s
) {
169 } // namespace test12
172 template <typename
... Ts
>
177 template <typename
... Ts
>
178 using AFoo
= Foo
<Ts
...>;
183 template <typename T
>
184 using BFoo
= Foo
<T
, T
>;
186 } // namespace test13
190 concept IsInt
= __is_same(decltype(T()), int);
192 template<IsInt T
, int N
>
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}}
200 // expected-note@-2 {{implicit deduction guide declared as 'template <int K> requires __is_deducible(test14::Bar, Foo<double, K>) Bar(Foo<double, K>) -> Foo<double, K>'}}
201 // expected-note@-3 {{implicit deduction guide declared as 'template <int K> requires __is_deducible(test14::Bar, Foo<double, K>) Bar(const double (&)[K]) -> Foo<double, K>'}}
203 Bar s2
= {abc
}; // expected-error {{no viable constructor or deduction guide for deduction }}
204 } // namespace test14
207 template <class T
> struct Foo
{ Foo(T
); };
209 template<class V
> using AFoo
= Foo
<V
*>;
210 template<typename
> concept False
= false;
212 using BFoo
= AFoo
<W
>; // expected-note {{candidate template ignored: constraints not satisfied [with V = int]}} \
213 // expected-note {{cannot deduce template arguments for 'BFoo' from 'Foo<int *>'}} \
214 // expected-note {{implicit deduction guide declared as 'template <class V> requires __is_deducible(AFoo, Foo<V *>) && __is_deducible(test15::BFoo, Foo<V *>) BFoo(V *) -> Foo<V *>}} \
215 // expected-note {{candidate template ignored: could not match 'Foo<V *>' against 'int *'}} \
216 // expected-note {{template <class V> requires __is_deducible(AFoo, Foo<V *>) && __is_deducible(test15::BFoo, Foo<V *>) BFoo(Foo<V *>) -> Foo<V *>}}
218 AFoo
a1(&i
); // OK, deduce Foo<int *>
220 // the W is not deduced from the deduced type Foo<int *>.
221 BFoo
b2(&i
); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'BFoo'}}
222 } // namespace test15
225 struct X
{ X(int); X(const X
&); };
235 static_assert(__is_same(decltype(s
.t
), int));
237 // explicit deduction guide.
240 // FIXME: the type should be X because of the above explicit deduction guide.
241 static_assert(__is_same(decltype(s2
.t
), int));
242 } // namespace test16
245 template <typename T
>
248 // CTAD for alias templates only works for the RHS of the alias of form of
249 // [typename] [nested-name-specifier] [template] simple-template-id
250 template <typename U
>
251 using AFoo
= Foo
<U
>*; // expected-note {{template is declared here}}
253 AFoo s
= {1}; // expected-error {{alias template 'AFoo' requires template arguments; argument deduction only allowed for}}
254 } // namespace test17
258 concept False
= false; // expected-note {{because 'false' evaluated to false}}
260 template <typename T
> struct Foo
{ T t
; };
262 template<typename T
> requires False
<T
> // expected-note {{because 'int' does not satisfy 'False'}}
265 template <typename U
>
266 using Bar
= Foo
<U
>; // expected-note {{could not match 'Foo<U>' against 'int'}} \
267 // expected-note {{implicit deduction guide declared as 'template <typename U> requires __is_deducible(test18::Bar, Foo<U>) Bar(Foo<U>) -> Foo<U>'}} \
268 // expected-note {{candidate template ignored: constraints not satisfied}} \
269 // expected-note {{implicit deduction guide declared as 'template <typename T> requires False<T> && __is_deducible(test18::Bar, Foo<int>) Bar(T) -> Foo<int>'}} \
270 // expected-note {{candidate function template not viable}} \
271 // expected-note {{implicit deduction guide declared as 'template <typename U> requires __is_deducible(test18::Bar, Foo<U>) Bar() -> Foo<U>'}}
273 Bar s
= {1}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}
274 } // namespace test18
276 // GH85406, verify no crash on invalid alias templates.
278 template <typename T
>
281 template <typename T
>
282 template <typename K
>
283 using Bar2
= Foo
<K
>; // expected-error {{extraneous template parameter list in alias template declaration}}
285 Bar2 b
= 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}
286 } // namespace test19
290 template <template <typename
> typename T
>
293 template <typename U
>
296 // Verify that template template type parameter TTP is referenced/used in the
297 // template arguments of the RHS.
298 template <template<typename
> typename TTP
>
299 using Bar
= Foo
<K
<TTP
>>; // expected-note {{candidate template ignored: could not match 'Foo<K<TTP>>' against 'int'}} \
300 // expected-note {{implicit deduction guide declared as 'template <template <typename> typename TTP> requires __is_deducible(test20::Bar, Foo<K<TTP>>) Bar(Foo<K<TTP>>) -> Foo<K<TTP>>'}}
304 Bar t
= Foo
<K
<Container
>>();
306 Bar s
= 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of}}
307 } // namespace test20
310 template <typename T
, unsigned N
>
311 struct Array
{ const T member
[N
]; };
312 template <unsigned N
>
313 using String
= Array
<char, N
>;
315 // Verify no crash on constructing the aggregate deduction guides.
317 } // namespace test21
322 template <typename T
>
323 class Derived final
: public Base
{};
325 template <typename T
, typename D
>
326 requires
__is_base_of(Base
, D
)
331 template <typename U
>
332 using AFoo
= Foo
<int, Derived
<U
>>;
334 AFoo
a(Derived
<int>{});
335 } // namespace test22
338 // We have an aggregate deduction guide "G(T) -> G<T>".
342 template<typename X
= int>
346 // Verify that the aggregate deduction guide "AG(int) -> AG<int>" is built and
348 static_assert(__is_same(decltype(ag
.t1
), int));
349 } // namespace test23
352 // verify that the transformed require-clause of the alias deduction gudie has
353 // the right depth info.
358 template <typename D
>
359 constexpr bool C
= sizeof(D
);
361 // Case1: the alias template and the underlying deduction guide are in the same
363 template <typename T
>
365 template <typename U
>
370 template <typename V
>
374 template <typename Y
>
375 using Alias
= Foo
<Y
>;
377 // The require-clause should be evaluated on the type Key.
378 Case1
<Forward
>::Alias t2
= Key();
381 // Case2: the alias template and underlying deduction guide are in different
383 template <typename T
>
387 template <typename U
>
391 template <typename T
>
393 template <typename Y
>
394 using Alias
= Foo
<Y
>;
396 // The require-caluse should be evaluated on the type Key.
397 Case2
<Forward
>::Alias t1
= Key();
399 // Case3: crashes on the constexpr evaluator due to the mixed-up depth in
414 template <typename U
>
415 using AFoo
= A1
<int>::A2
<int>::Foo
<U
>;
418 // Case4: crashes on the constexpr evaluator due to the mixed-up index for the
419 // template parameters `V`.
420 template<class T
, typename T2
>
422 template<class V
> requires C
<V
>
427 using ACase4
= Case4
<T2
, T2
>;
430 } // namespace test24
433 template<typename T
, typename
...Us
>
435 template<typename V
> requires
__is_same(V
, int)
439 template<typename
...TS
>
440 using AA
= A
<int, TS
...>;
445 template <typename T
>
451 template <typename U
>
453 B1
b1(100); // deduce to A<char>;
454 static_assert(__is_same(decltype(b1
), A
<char>));
456 template <typename U
>
457 requires (!__is_same(U
, char)) // filter out the explicit deduction guide.
459 template <typename V
>
462 B2
b2(100); // deduced to A<int>;
463 static_assert(__is_same(decltype(b2
), A
<int>));
464 B3
b3(100); // decuded to A<int>;
465 static_assert(__is_same(decltype(b3
), A
<int>));
469 template <typename T1
>
471 template <typename T2
>
477 template <typename T3
>
481 Out
<float>::B
out(100); // deduced to Out<float>::A<float>;
482 static_assert(__is_same(decltype(out
), Out
<float>::A
<float>));
487 template <typename V
> struct S
{
492 template <typename V
> using Alias
= S
<V
>;
496 } // namespace GH111508
500 template <class T
, unsigned N
> struct array
{
504 template <typename Tp
, typename
... Up
>
505 array(Tp
, Up
...) -> array
<Tp
, 1 + sizeof...(Up
)>;
507 template <typename T
> struct ArrayType
{
508 template <unsigned size
> using Array
= array
<T
, size
>;
511 template <ArrayType
<int>::Array array
> void test() {}
513 void foo() { test
<{1, 2, 3}>(); }
515 } // namespace GH113518