xref: /llvm-project/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp (revision e29c085812e259910a3d8b6c2d2f471d1c3eede4)
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z %s
2 
3 template<typename T, T val> struct A {}; // expected-note 3{{template parameter is declared here}}
4 
5 template<typename T, typename U> constexpr bool is_same = false;
6 template<typename T> constexpr bool is_same<T, T> = true;
7 
8 namespace String {
9   A<const char*, "test"> a; // expected-error {{pointer to subobject of string literal}}
10   A<const char (&)[5], "test"> b; // expected-error {{reference to string literal}}
11 }
12 
13 namespace Array {
14   char arr[3];
15   char x;
16   A<const char*, arr> a;
17   A<const char(&)[3], arr> b;
18   A<const char*, &arr[0]> c;
19   A<const char*, &arr[1]> d; // expected-error {{refers to subobject '&arr[1]'}}
20   A<const char*, (&arr)[0]> e;
21   A<const char*, &x> f;
22   A<const char*, &(&x)[0]> g;
23   A<const char*, &(&x)[1]> h; // expected-error {{refers to subobject '&x + 1'}}
24   A<const char*, 0> i; // expected-error {{not allowed in a converted constant}}
25   A<const char*, nullptr> j;
26 
27   extern char aub[];
28   A<char[], aub> k;
29 }
30 
31 namespace Function {
32   void f();
33   void g() noexcept;
34   void h();
35   void h(int);
36   template<typename...T> void i(T...);
37   typedef A<void (*)(), f> a;
38   typedef A<void (*)(), &f> a;
39   typedef A<void (*)(), g> b;
40   typedef A<void (*)(), &g> b;
41   typedef A<void (*)(), h> c;
42   typedef A<void (*)(), &h> c;
43   typedef A<void (*)(), i> d;
44   typedef A<void (*)(), &i> d;
45   typedef A<void (*)(), i<>> d;
46   typedef A<void (*)(), i<int>> e; // expected-error {{value of type '<overloaded function type>' is not implicitly convertible to 'void (*)()'}}
47 
48   typedef A<void (*)(), 0> x; // expected-error {{not allowed in a converted constant}}
49   typedef A<void (*)(), nullptr> y;
50 }
51 
52 void Func() {
53   A<const char*, __func__> a; // expected-error {{pointer to subobject of predefined '__func__' variable}}
54 }
55 
56 namespace LabelAddrDiff {
57   void f() {
58     a: b: A<int, __builtin_constant_p(true) ? (__INTPTR_TYPE__)&&b - (__INTPTR_TYPE__)&&a : 0> s; // expected-error {{label address difference}}
59   };
60 }
61 
62 namespace Temp {
63   struct S { int n; };
64   constexpr S &addr(S &&s) { return s; }
65   A<S &, addr({})> a; // expected-error {{reference to temporary object}}
66   A<S *, &addr({})> b; // expected-error {{pointer to temporary object}}
67   A<int &, addr({}).n> c; // expected-error {{reference to subobject of temporary object}}
68   A<int *, &addr({}).n> d; // expected-error {{pointer to subobject of temporary object}}
69 }
70 
71 namespace std { struct type_info; }
72 
73 namespace RTTI {
74   A<const std::type_info&, typeid(int)> a; // expected-error {{reference to type_info object}}
75   A<const std::type_info*, &typeid(int)> b; // expected-error {{pointer to type_info object}}
76 }
77 
78 namespace PtrMem {
79   struct B { int b; };
80   struct C : B {};
81   struct D : B {};
82   struct E : C, D { int e; };
83 
84   constexpr int B::*b = &B::b;
85   constexpr int C::*cb = b;
86   constexpr int D::*db = b;
87   constexpr int E::*ecb = cb;
88   constexpr int E::*edb = db;
89 
90   constexpr int E::*e = &E::e;
91   constexpr int D::*de = (int D::*)e;
92   constexpr int C::*ce = (int C::*)e;
93   constexpr int B::*bde = (int B::*)de;
94   constexpr int B::*bce = (int B::*)ce;
95 
96   using Ab = A<int B::*, b>;
97   using Ab = A<int B::*, &B::b>;
98   using Abce = A<int B::*, bce>;
99   using Abde = A<int B::*, bde>;
100   static_assert(!is_same<Ab, Abce>, "");
101   static_assert(!is_same<Ab, Abde>, "");
102   static_assert(!is_same<Abce, Abde>, "");
103   static_assert(is_same<Abce, A<int B::*, (int B::*)(int C::*)&E::e>>, "");
104 
105   using Ae = A<int E::*, e>;
106   using Ae = A<int E::*, &E::e>;
107   using Aecb = A<int E::*, ecb>;
108   using Aedb = A<int E::*, edb>;
109   static_assert(!is_same<Ae, Aecb>, "");
110   static_assert(!is_same<Ae, Aedb>, "");
111   static_assert(!is_same<Aecb, Aedb>, "");
112   static_assert(is_same<Aecb, A<int E::*, (int E::*)(int C::*)&B::b>>, "");
113 
114   using An = A<int E::*, nullptr>;
115   using A0 = A<int E::*, (int E::*)0>;
116   static_assert(is_same<An, A0>);
117 }
118 
119 namespace DeduceDifferentType {
120   template<int N> struct A {};
121   template<long N> int a(A<N>); // expected-note {{does not have the same type}}
122   int a_imp = a(A<3>()); // expected-error {{no matching function}}
123   int a_exp = a<3>(A<3>());
124 
125   template<decltype(nullptr)> struct B {}; // expected-note {{template parameter is declared here}}
126   template<int *P> int b(B<P>); // expected-error {{value of type 'int *' is not implicitly convertible to 'decltype(nullptr)'}}
127   int b_imp = b(B<nullptr>()); // expected-error {{no matching function}}
128   int b_exp = b<nullptr>(B<nullptr>()); // expected-error {{no matching function}}
129 
130   struct X { constexpr operator int() { return 0; } } x;
131   template<X &> struct C {}; // expected-note {{template parameter is declared here}}
132   template<int N> int c(C<N>); // expected-error {{value of type 'int' is not implicitly convertible to 'X &'}}
133   int c_imp = c(C<x>()); // expected-error {{no matching function}}
134   int c_exp = c<x>(C<x>()); // expected-error {{no matching function}}
135 
136   struct Z;
137   struct Y { constexpr operator Z&(); } y;
138   struct Z { constexpr operator Y&() { return y; } } z;
139   constexpr Y::operator Z&() { return z; }
140   template<Y &> struct D {};
141   template<Z &z> int d(D<z>); // expected-note {{couldn't infer template argument 'z'}}
142   int d_imp = d(D<y>()); // expected-error {{no matching function}}
143   int d_exp = d<y>(D<y>());
144 }
145 
146 namespace DeclMatch {
147   template<typename T, T> int f();
148   template<typename T> class X { friend int f<T, 0>(); static int n; };
149   template<typename T, T> int f() { return X<T>::n; }
150   int k = f<int, 0>(); // ok, friend
151 }
152 
153 namespace PR24921 {
154   enum E { e };
155   template<E> void f();
156   template<int> void f(int);
157   template<> void f<e>() {}
158 }
159 
160 namespace Auto {
161   namespace Basic {
162     // simple auto
163     template<auto x> constexpr auto constant = x; // expected-note {{declared here}}
164 
165     auto v1 = constant<5>;
166     auto v2 = constant<true>;
167     auto v3 = constant<'a'>;
168     auto v4 = constant<2.5>;  // expected-error {{cannot have type 'double'}}
169 
170     using T1 = decltype(v1);
171     using T1 = int;
172     using T2 = decltype(v2);
173     using T2 = bool;
174     using T3 = decltype(v3);
175     using T3 = char;
176 
177     // pointers
178     template<auto v>    class B { };
179     template<auto* p>   class B<p> { }; // expected-note {{matches}}
180     template<auto** pp> class B<pp> { };
181     template<auto* p0>   int &f(B<p0> b); // expected-note {{candidate}}
182     template<auto** pp0> float &f(B<pp0> b); // expected-note {{candidate}}
183 
184     int a, *b = &a;
185     int &r = f(B<&a>());
186     float &s = f(B<&b>());
187 
188     void type_affects_identity(B<&a>) {}
189     void type_affects_identity(B<(const int*)&a>) {}
190     void type_affects_identity(B<(void*)&a>) {}
191     void type_affects_identity(B<(const void*)&a>) {}
192 
193     // pointers to members
194     template<typename T, auto *T::*p> struct B<p> {};
195     template<typename T, auto **T::*p> struct B<p> {};
196     template<typename T, auto *T::*p0>   char &f(B<p0> b); // expected-note {{candidate}}
197     template<typename T, auto **T::*pp0> short &f(B<pp0> b); // expected-note {{candidate}}
198 
199     struct X { int n; int *p; int **pp; typedef int a, b; };
200     auto t = f(B<&X::n>()); // expected-error {{no match}}
201     char &u = f(B<&X::p>());
202     short &v = f(B<&X::pp>());
203 
204     struct Y : X {};
205     void type_affects_identity(B<&X::n>) {}
206     void type_affects_identity(B<(int Y::*)&X::n>) {}
207     void type_affects_identity(B<(const int X::*)&X::n>) {}
208     void type_affects_identity(B<(const int Y::*)&X::n>) {}
209 
210     // A case where we need to do auto-deduction, and check whether the
211     // resulting dependent types match during partial ordering. These
212     // templates are not ordered due to the mismatching function parameter.
213     template<typename T, auto *(*f)(T, typename T::a)> struct B<f> {}; // expected-note {{matches}}
214     template<typename T, auto **(*f)(T, typename T::b)> struct B<f> {}; // expected-note {{matches}}
215     int **g(X, int);
216     B<&g> bg; // expected-error {{ambiguous}}
217   }
218 
219   namespace Chained {
220     // chained template argument deduction
221     template<long n> struct C { };
222     template<class T> struct D;
223     template<class T, T n> struct D<C<n>>
224     {
225         using Q = T;
226     };
227     using DQ = long;
228     using DQ = D<C<short(2)>>::Q;
229 
230     // chained template argument deduction from an array bound
231     template<typename T> struct E;
232     template<typename T, T n> struct E<int[n]> {
233         using Q = T;
234     };
235     using EQ = E<int[short(42)]>::Q;
236     using EQ = decltype(sizeof 0);
237 
238     template<int N> struct F;
239     template<typename T, T N> int foo(F<N> *) = delete;  // expected-note {{explicitly deleted}}
240     void foo(void *); // expected-note {{candidate function}}
241     void bar(F<0> *p) {
242         foo(p); // expected-error {{deleted function}}
243     }
244   }
245 
246   namespace ArrayToPointer {
247     constexpr char s[] = "test";
248     template<const auto* p> struct S { };
249     S<s> p;
250 
251     template<typename R, typename P, R F(P)> struct A {};
252     template<typename R, typename P, R F(P)> void x(A<R, P, F> a);
253     void g(int) { x(A<void, int, &g>()); }
254   }
255 
256   namespace DecltypeAuto {
257     template<auto v> struct A { };
258     template<decltype(auto) v> struct DA { };
259     template<auto&> struct R { };
260 
261     auto n = 0; // expected-note + {{declared here}}
262     A<n> a; // expected-error {{not a constant}} expected-note {{non-const variable 'n'}}
263     DA<n> da1;  // expected-error {{not a constant}} expected-note {{non-const variable 'n'}}
264     DA<(n)> da2;
265     R<n> r;
266   }
267 
268   namespace Decomposition {
269     // Types of deduced non-type template arguments must match exactly, so
270     // partial ordering fails in both directions here.
271     template<auto> struct Any;
272     template<int N> struct Any<N> { typedef int Int; }; // expected-note 3{{match}}
273     template<short N> struct Any<N> { typedef int Short; }; // expected-note 3{{match}}
274     Any<0>::Int is_int; // expected-error {{ambiguous}}
275     Any<(short)0>::Short is_short; // expected-error {{ambiguous}}
276     Any<(char)0>::Short is_char; // expected-error {{ambiguous}}
277 
278     template<int, auto> struct NestedAny;
279     template<auto N> struct NestedAny<0, N>; // expected-note 3{{match}}
280     template<int N> struct NestedAny<0, N> { typedef int Int; }; // expected-note 3{{match}}
281     template<short N> struct NestedAny<0, N> { typedef int Short; }; // expected-note 3{{match}}
282     NestedAny<0, 0>::Int nested_int; // expected-error {{ambiguous}}
283     NestedAny<0, (short)0>::Short nested_short; // expected-error {{ambiguous}}
284     NestedAny<0, (char)0>::Short nested_char; // expected-error {{ambiguous}}
285 
286     double foo(int, bool);
287     template<auto& f> struct fn_result_type;
288 
289     template<class R, class... Args, R (& f)(Args...)>
290     struct fn_result_type<f>
291     {
292         using type = R;
293     };
294 
295     using R1 = fn_result_type<foo>::type;
296     using R1 = double;
297 
298     template<int, auto &f> struct fn_result_type_partial_order;
299     template<auto &f> struct fn_result_type_partial_order<0, f>;
300     template<class R, class... Args, R (& f)(Args...)>
301     struct fn_result_type_partial_order<0, f> {};
302     fn_result_type_partial_order<0, foo> frtpo;
303   }
304 
305   namespace Variadic {
306     template<auto... vs> struct value_list { };
307 
308     using size_t = decltype(sizeof 0);
309     template<size_t n, class List> struct nth_element;
310     template<size_t n, class List> constexpr auto nth_element_v = nth_element<n, List>::value;
311 
312     template<size_t n, auto v0, auto... vs>
313     struct nth_element<n, value_list<v0, vs...>>
314     {
315         static constexpr auto value = nth_element<n - 1, value_list<vs...>>::value;
316     };
317     template<auto v0, auto... vs>
318     struct nth_element<0, value_list<v0, vs...>>
319     {
320         static constexpr auto value = v0;
321     };
322 
323     static_assert(nth_element_v<2, value_list<'a', 27U, false>> == false, "value mismatch");
324   }
325 }
326 
327 namespace Nested {
328   template<typename T> struct A {
329     template<auto X> struct B;
330     template<auto *P> struct B<P>;
331     template<auto **P> struct B<P> { using pointee = decltype(+**P); };
332     template<auto (*P)(T)> struct B<P> { using param = T; };
333     template<typename U, auto (*P)(T, U)> struct B<P> { using param2 = U; };
334   };
335 
336   using Int = int;
337 
338   int *n;
339   using Int = A<int>::B<&n>::pointee;
340 
341   void f(int);
342   using Int = A<int>::B<&f>::param;
343 
344   void g(int, int);
345   using Int = A<int>::B<&g>::param2;
346 }
347 
348 namespace rdar41852459 {
349 template <auto V> struct G {};
350 
351 template <class T> struct S {
352   template <auto V> void f() {
353     G<V> x;
354   }
355   template <auto *PV> void f2() {
356     G<PV> x;
357   }
358   template <decltype(auto) V> void f3() {
359     G<V> x;
360   }
361 };
362 
363 template <auto *PV> struct I {};
364 
365 template <class T> struct K {
366   template <auto *PV> void f() {
367     I<PV> x;
368   }
369   template <auto V> void f2() {
370     I<V> x;
371   }
372   template <decltype(auto) V> void f3() {
373     I<V> x;
374   }
375 };
376 
377 template <decltype(auto)> struct L {};
378 template <class T> struct M {
379   template <auto *PV> void f() {
380     L<PV> x;
381   }
382   template <auto V> void f() {
383     L<V> x;
384   }
385   template <decltype(auto) V> void f() {
386     L<V> x;
387   }
388 };
389 }
390 
391 namespace PR42362 {
392   template<auto ...A> struct X { struct Y; void f(int...[A]); };
393   template<auto ...A> struct X<A...>::Y {};
394   template<auto ...A> void X<A...>::f(int...[A]) {}
395   void f() { X<1, 2>::Y y; X<1, 2>().f(0, 0); }
396 
397   template<typename, auto...> struct Y;
398   template<auto ...A> struct Y<int, A...> {};
399   Y<int, 1, 2, 3> y;
400 
401   template<auto (&...F)()> struct Z { struct Q; };
402   template<auto (&...F)()> struct Z<F...>::Q {};
403   Z<f, f, f>::Q q;
404 }
405 
406 namespace QualConv {
407   int *X;
408   template<const int *const *P> void f() {
409     using T = decltype(P);
410     using T = const int* const*;
411   }
412   template void f<&X>();
413 
414   template<const int *const &R> void g() {
415     using T = decltype(R);
416     using T = const int *const &;
417   }
418   template void g<(const int *const&)X>();
419 }
420 
421 namespace FunctionConversion {
422   struct a { void c(char *) noexcept; };
423   template<void (a::*f)(char*)> void g() {
424     using T = decltype(f);
425     using T = void (a::*)(char*); // (not 'noexcept')
426   }
427   template void g<&a::c>();
428 
429   void c() noexcept;
430   template<void (*p)()> void h() {
431     using T = decltype(p);
432     using T = void (*)(); // (not 'noexcept')
433   }
434   template void h<&c>();
435 }
436 
437 namespace VoidPtr {
438   // Note, this is an extension in C++17 but valid in C++20.
439   template<void *P> void f() {
440     using T = decltype(P);
441     using T = void*;
442   }
443   int n;
444   template void f<(void*)&n>();
445 }
446 
447 namespace PR42108 {
448   struct R {};
449   struct S { constexpr S() {} constexpr S(R) {} };
450   struct T { constexpr operator S() { return {}; } };
451   template <const S &> struct A {}; // expected-note {{template parameter is declared here}}
452   void f() {
453     A<R{}>(); // expected-error {{would bind reference to a temporary}}
454     A<S{}>(); // expected-error {{reference to temporary object}}
455     A<T{}>(); // expected-error {{reference to temporary object}}
456   }
457 }
458 
459 namespace PR46637 {
460   template<auto (*f)() -> auto> struct X { // expected-note {{here}}
461     auto call() { return f(); }
462   };
463   X<nullptr> x; // expected-error {{incompatible initializer}}
464 
465   void *f();
466   X<f> y;
467   int n = y.call(); // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type 'void *'}}
468 }
469 
470 namespace PR48517 {
471   template<const int *P> struct A { static constexpr const int *p = P; };
472   template<typename T> auto make_nonconst() {
473     static int n;
474     return A<&n>();
475   };
476   using T = decltype(make_nonconst<int>()); // expected-note {{previous}}
477   using U = decltype(make_nonconst<float>());
478   static_assert(T::p != U::p);
479   using T = U; // expected-error {{different types}}
480 
481   template<typename T> auto make_const() {
482     static constexpr int n = 42;
483     return A<&n>();
484   };
485   using V = decltype(make_const<int>()); // expected-note {{previous}}
486   using W = decltype(make_const<float>());
487   static_assert(*V::p == *W::p);
488   static_assert(V::p != W::p);
489   using V = W; // expected-error {{different types}}
490 
491   template<auto V> struct Q {
492     using X = int;
493     static_assert(V == "primary template should not be instantiated");
494   };
495   template<typename T> struct R {
496     int n;
497     constexpr int f() {
498       return Q<&R::n>::X;
499     }
500   };
501   template<> struct Q<&R<int>::n> { static constexpr int X = 1; };
502   static_assert(R<int>().f() == 1);
503 }
504 
505 namespace dependent_reference {
506   template<int &r> struct S { int *q = &r; };
507   template<int> auto f() { static int n; return S<n>(); }
508   auto v = f<0>();
509   auto w = f<1>();
510   static_assert(!is_same<decltype(v), decltype(w)>);
511   // Ensure that we can instantiate the definition of S<...>.
512   int n = *v.q + *w.q;
513 }
514 
515 namespace decay {
516   template<typename T, typename C, const char *const A[(int)T::count]> struct X {
517     template<typename CC> void f(const X<T, CC, A> &v) {}
518   };
519   struct A {
520     static constexpr const char *arr[] = {"hello", "world"};
521     static constexpr int count = 2;
522   };
523   void f() {
524     X<A, int, A::arr> x1;
525     X<A, float, A::arr> x2;
526     x1.f(x2);
527   }
528 }
529 
530 namespace TypeSuffix {
531   template <auto N> struct A {};
532   template <> struct A<1> { using type = int; }; // expected-note {{'A<1>::type' declared here}}
533   A<1L>::type a;                                 // expected-error {{no type named 'type' in 'TypeSuffix::A<1L>'; did you mean 'A<1>::type'?}}
534 
535   template <auto N> struct B {};
536   template <> struct B<1> { using type = int; }; // expected-note {{'B<1>::type' declared here}}
537   B<2>::type b;                                  // expected-error {{no type named 'type' in 'TypeSuffix::B<2>'; did you mean 'B<1>::type'?}}
538 
539   template <auto N> struct C {};
540   template <> struct C<'a'> { using type = signed char; }; // expected-note {{'C<'a'>::type' declared here}}
541   C<(signed char)'a'>::type c;                             // expected-error {{no type named 'type' in 'TypeSuffix::C<(signed char)'a'>'; did you mean 'C<'a'>::type'?}}
542 
543   template <auto N> struct D {};
544   template <> struct D<'a'> { using type = signed char; }; // expected-note {{'D<'a'>::type' declared here}}
545   D<'b'>::type d;                                          // expected-error {{no type named 'type' in 'TypeSuffix::D<'b'>'; did you mean 'D<'a'>::type'?}}
546 
547   template <auto N> struct E {};
548   template <> struct E<'a'> { using type = unsigned char; }; // expected-note {{'E<'a'>::type' declared here}}
549   E<(unsigned char)'a'>::type e;                             // expected-error {{no type named 'type' in 'TypeSuffix::E<(unsigned char)'a'>'; did you mean 'E<'a'>::type'?}}
550 
551   template <auto N> struct F {};
552   template <> struct F<'a'> { using type = unsigned char; }; // expected-note {{'F<'a'>::type' declared here}}
553   F<'b'>::type f;                                            // expected-error {{no type named 'type' in 'TypeSuffix::F<'b'>'; did you mean 'F<'a'>::type'?}}
554 
555   template <auto... N> struct X {};
556   X<1, 1u>::type y; // expected-error {{no type named 'type' in 'TypeSuffix::X<1, 1U>'}}
557   X<1, 1>::type z; // expected-error {{no type named 'type' in 'TypeSuffix::X<1, 1>'}}
558 }
559 
560 namespace no_crash {
561 template <class T>
562 class Base {
563 public:
564   template <class> class EntryPointSpec {};
565   template <auto Method>
566   using EntryPoint = EntryPointSpec<T>;
567 };
568 
569 class Derived : Base<Derived>{
570   template <class...> class Spec {};
571 
572   void Invalid(Undefined) const; // expected-error {{unknown type name 'Undefined'}}
573   void crash() {
574     return Spec{
575         EntryPoint<&Invalid>()
576     };
577   }
578 };
579 } // no_crash
580 
581 namespace PR47792 {
582   using I = int;
583 
584   template<decltype(auto)> int a;
585   const int n = 0;
586   const I n2 = 0;
587   static_assert(&a<n> == &a<0>, "both should have type 'int'");
588   static_assert(&a<n2> == &a<0>, "both should have type 'int'");
589 
590   int m;
591   const int &r1 = m;
592   int &r2 = m;
593   static_assert(&a<r1> != &a<r2>, "should have different types");
594 
595   const I &r3 = m;
596   static_assert(&a<r1> == &a<r3>, "should have different types");
597   static_assert(&a<r2> != &a<r3>, "should have different types");
598 
599   void foo();
600   template <void () = foo> void bar() {}
601   template void bar<>();    // expected-note {{previous explicit instantiation is here}}
602   template void bar<foo>(); // expected-error {{duplicate explicit instantiation of 'bar<&PR47792::foo>'}}
603 }
604 
605 namespace GH68024 {
606 template<auto>
607 struct s {};
608 
609 struct {
610   void operator()(int);
611 } f;
612 
613 template<typename T>
614 using a = s<f(T::x)>;
615 }
616 
617 namespace GH73460 {
618   template <class T, T, T> struct A;
619   template <class T, T n> struct A<T, n, n> {};
620 
621   int j;
622   template struct A<int&, j, j>;
623 } // namespace GH73460
624