1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z %s
2
3 template<typename T, T val> struct A {};
4
5 template<typename T, typename U> constexpr bool is_same = false; // expected-note +{{here}}
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
Func()52 void Func() {
53 A<const char*, __func__> a; // expected-error {{pointer to subobject of predefined '__func__' variable}}
54 }
55
56 namespace LabelAddrDiff {
f()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; };
addr(S && s)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; // expected-note +{{here}}
88 constexpr int E::*edb = db; // expected-note +{{here}}
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; // expected-note +{{here}}
94 constexpr int B::*bce = (int B::*)ce; // expected-note +{{here}}
95
96 // FIXME: This should all be accepted, but we don't yet have a representation
97 // nor mangling for this form of template argument.
98 using Ab = A<int B::*, b>;
99 using Ab = A<int B::*, &B::b>;
100 using Abce = A<int B::*, bce>; // expected-error {{not supported}}
101 using Abde = A<int B::*, bde>; // expected-error {{not supported}}
102 static_assert(!is_same<Ab, Abce>, ""); // expected-error {{undeclared}} expected-error {{must be a type}}
103 static_assert(!is_same<Ab, Abde>, ""); // expected-error {{undeclared}} expected-error {{must be a type}}
104 static_assert(!is_same<Abce, Abde>, ""); // expected-error 2{{undeclared}} expected-error {{must be a type}}
105 static_assert(is_same<Abce, A<int B::*, (int B::*)(int C::*)&E::e>>, ""); // expected-error {{undeclared}} expected-error {{not supported}}
106
107 using Ae = A<int E::*, e>;
108 using Ae = A<int E::*, &E::e>;
109 using Aecb = A<int E::*, ecb>; // expected-error {{not supported}}
110 using Aedb = A<int E::*, edb>; // expected-error {{not supported}}
111 static_assert(!is_same<Ae, Aecb>, ""); // expected-error {{undeclared}} expected-error {{must be a type}}
112 static_assert(!is_same<Ae, Aedb>, ""); // expected-error {{undeclared}} expected-error {{must be a type}}
113 static_assert(!is_same<Aecb, Aedb>, ""); // expected-error 2{{undeclared}} expected-error {{must be a type}}
114 static_assert(is_same<Aecb, A<int E::*, (int E::*)(int C::*)&B::b>>, ""); // expected-error {{undeclared}} expected-error {{not supported}}
115
116 using An = A<int E::*, nullptr>;
117 using A0 = A<int E::*, (int E::*)0>;
118 static_assert(is_same<An, A0>);
119 }
120
121 namespace DeduceDifferentType {
122 template<int N> struct A {};
123 template<long N> int a(A<N>); // expected-note {{does not have the same type}}
124 int a_imp = a(A<3>()); // expected-error {{no matching function}}
125 int a_exp = a<3>(A<3>());
126
127 template<decltype(nullptr)> struct B {};
128 template<int *P> int b(B<P>); // expected-error {{value of type 'int *' is not implicitly convertible to 'decltype(nullptr)'}}
129 int b_imp = b(B<nullptr>()); // expected-error {{no matching function}}
130 int b_exp = b<nullptr>(B<nullptr>()); // expected-error {{no matching function}}
131
operator intDeduceDifferentType::X132 struct X { constexpr operator int() { return 0; } } x;
133 template<X &> struct C {};
134 template<int N> int c(C<N>); // expected-error {{value of type 'int' is not implicitly convertible to 'DeduceDifferentType::X &'}}
135 int c_imp = c(C<x>()); // expected-error {{no matching function}}
136 int c_exp = c<x>(C<x>()); // expected-error {{no matching function}}
137
138 struct Z;
139 struct Y { constexpr operator Z&(); } y;
operator Y&DeduceDifferentType::Z140 struct Z { constexpr operator Y&() { return y; } } z;
operator Z&()141 constexpr Y::operator Z&() { return z; }
142 template<Y &> struct D {};
143 template<Z &z> int d(D<z>); // expected-note {{couldn't infer template argument 'z'}}
144 int d_imp = d(D<y>()); // expected-error {{no matching function}}
145 int d_exp = d<y>(D<y>());
146 }
147
148 namespace DeclMatch {
149 template<typename T, T> int f();
150 template<typename T> class X { friend int f<T, 0>(); static int n; };
f()151 template<typename T, T> int f() { return X<T>::n; }
152 int k = f<int, 0>(); // ok, friend
153 }
154
155 namespace PR24921 {
156 enum E { e };
157 template<E> void f();
158 template<int> void f(int);
f()159 template<> void f<e>() {}
160 }
161
162 namespace Auto {
163 namespace Basic {
164 // simple auto
165 template<auto x> constexpr auto constant = x; // expected-note {{declared here}}
166
167 auto v1 = constant<5>;
168 auto v2 = constant<true>;
169 auto v3 = constant<'a'>;
170 auto v4 = constant<2.5>; // expected-error {{cannot have type 'double'}}
171
172 using T1 = decltype(v1);
173 using T1 = int;
174 using T2 = decltype(v2);
175 using T2 = bool;
176 using T3 = decltype(v3);
177 using T3 = char;
178
179 // pointers
180 template<auto v> class B { };
181 template<auto* p> class B<p> { }; // expected-note {{matches}}
182 template<auto** pp> class B<pp> { };
183 template<auto* p0> int &f(B<p0> b); // expected-note {{candidate}}
184 template<auto** pp0> float &f(B<pp0> b); // expected-note {{candidate}}
185
186 int a, *b = &a;
187 int &r = f(B<&a>());
188 float &s = f(B<&b>());
189
type_affects_identity(B<& a>)190 void type_affects_identity(B<&a>) {}
type_affects_identity(B<(const int *)& a>)191 void type_affects_identity(B<(const int*)&a>) {}
type_affects_identity(B<(void *)& a>)192 void type_affects_identity(B<(void*)&a>) {}
type_affects_identity(B<(const void *)& a>)193 void type_affects_identity(B<(const void*)&a>) {}
194
195 // pointers to members
196 template<typename T, auto *T::*p> struct B<p> {};
197 template<typename T, auto **T::*p> struct B<p> {};
198 template<typename T, auto *T::*p0> char &f(B<p0> b); // expected-note {{candidate}}
199 template<typename T, auto **T::*pp0> short &f(B<pp0> b); // expected-note {{candidate}}
200
201 struct X { int n; int *p; int **pp; typedef int a, b; };
202 auto t = f(B<&X::n>()); // expected-error {{no match}}
203 char &u = f(B<&X::p>());
204 short &v = f(B<&X::pp>());
205
206 struct Y : X {};
type_affects_identity(B<& X::n>)207 void type_affects_identity(B<&X::n>) {}
type_affects_identity(B<(int Y::*)& X::n>)208 void type_affects_identity(B<(int Y::*)&X::n>) {} // FIXME: expected-error {{sorry}}
type_affects_identity(B<(const int X::*)& X::n>)209 void type_affects_identity(B<(const int X::*)&X::n>) {}
type_affects_identity(B<(const int Y::*)& X::n>)210 void type_affects_identity(B<(const int Y::*)&X::n>) {} // FIXME: expected-error {{sorry}}
211
212 // A case where we need to do auto-deduction, and check whether the
213 // resulting dependent types match during partial ordering. These
214 // templates are not ordered due to the mismatching function parameter.
215 template<typename T, auto *(*f)(T, typename T::a)> struct B<f> {}; // expected-note {{matches}}
216 template<typename T, auto **(*f)(T, typename T::b)> struct B<f> {}; // expected-note {{matches}}
217 int **g(X, int);
218 B<&g> bg; // expected-error {{ambiguous}}
219 }
220
221 namespace Chained {
222 // chained template argument deduction
223 template<long n> struct C { };
224 template<class T> struct D;
225 template<class T, T n> struct D<C<n>>
226 {
227 using Q = T;
228 };
229 using DQ = long;
230 using DQ = D<C<short(2)>>::Q;
231
232 // chained template argument deduction from an array bound
233 template<typename T> struct E;
234 template<typename T, T n> struct E<int[n]> {
235 using Q = T;
236 };
237 using EQ = E<int[short(42)]>::Q;
238 using EQ = decltype(sizeof 0);
239
240 template<int N> struct F;
241 template<typename T, T N> int foo(F<N> *) = delete; // expected-note {{explicitly deleted}}
242 void foo(void *); // expected-note {{candidate function}}
bar(F<0> * p)243 void bar(F<0> *p) {
244 foo(p); // expected-error {{deleted function}}
245 }
246 }
247
248 namespace ArrayToPointer {
249 constexpr char s[] = "test";
250 template<const auto* p> struct S { };
251 S<s> p;
252
253 template<typename R, typename P, R F(P)> struct A {};
254 template<typename R, typename P, R F(P)> void x(A<R, P, F> a);
g(int)255 void g(int) { x(A<void, int, &g>()); }
256 }
257
258 namespace DecltypeAuto {
259 template<auto v> struct A { };
260 template<decltype(auto) v> struct DA { };
261 template<auto&> struct R { };
262
263 auto n = 0; // expected-note + {{declared here}}
264 A<n> a; // expected-error {{not a constant}} expected-note {{non-const variable 'n'}}
265 DA<n> da1; // expected-error {{not a constant}} expected-note {{non-const variable 'n'}}
266 DA<(n)> da2;
267 R<n> r;
268 }
269
270 namespace Decomposition {
271 // Types of deduced non-type template arguments must match exactly, so
272 // partial ordering fails in both directions here.
273 template<auto> struct Any;
274 template<int N> struct Any<N> { typedef int Int; }; // expected-note 3{{match}}
275 template<short N> struct Any<N> { typedef int Short; }; // expected-note 3{{match}}
276 Any<0>::Int is_int; // expected-error {{ambiguous}}
277 Any<(short)0>::Short is_short; // expected-error {{ambiguous}}
278 Any<(char)0>::Short is_char; // expected-error {{ambiguous}}
279
280 template<int, auto> struct NestedAny;
281 template<auto N> struct NestedAny<0, N>; // expected-note 3{{match}}
282 template<int N> struct NestedAny<0, N> { typedef int Int; }; // expected-note 3{{match}}
283 template<short N> struct NestedAny<0, N> { typedef int Short; }; // expected-note 3{{match}}
284 NestedAny<0, 0>::Int nested_int; // expected-error {{ambiguous}}
285 NestedAny<0, (short)0>::Short nested_short; // expected-error {{ambiguous}}
286 NestedAny<0, (char)0>::Short nested_char; // expected-error {{ambiguous}}
287
288 double foo(int, bool);
289 template<auto& f> struct fn_result_type;
290
291 template<class R, class... Args, R (& f)(Args...)>
292 struct fn_result_type<f>
293 {
294 using type = R;
295 };
296
297 using R1 = fn_result_type<foo>::type;
298 using R1 = double;
299
300 template<int, auto &f> struct fn_result_type_partial_order;
301 template<auto &f> struct fn_result_type_partial_order<0, f>;
302 template<class R, class... Args, R (& f)(Args...)>
303 struct fn_result_type_partial_order<0, f> {};
304 fn_result_type_partial_order<0, foo> frtpo;
305 }
306
307 namespace Variadic {
308 template<auto... vs> struct value_list { };
309
310 using size_t = decltype(sizeof 0);
311 template<size_t n, class List> struct nth_element;
312 template<size_t n, class List> constexpr auto nth_element_v = nth_element<n, List>::value;
313
314 template<size_t n, auto v0, auto... vs>
315 struct nth_element<n, value_list<v0, vs...>>
316 {
317 static constexpr auto value = nth_element<n - 1, value_list<vs...>>::value;
318 };
319 template<auto v0, auto... vs>
320 struct nth_element<0, value_list<v0, vs...>>
321 {
322 static constexpr auto value = v0;
323 };
324
325 static_assert(nth_element_v<2, value_list<'a', 27U, false>> == false, "value mismatch");
326 }
327 }
328
329 namespace Nested {
330 template<typename T> struct A {
331 template<auto X> struct B;
332 template<auto *P> struct B<P>;
333 template<auto **P> struct B<P> { using pointee = decltype(+**P); };
334 template<auto (*P)(T)> struct B<P> { using param = T; };
335 template<typename U, auto (*P)(T, U)> struct B<P> { using param2 = U; };
336 };
337
338 using Int = int;
339
340 int *n;
341 using Int = A<int>::B<&n>::pointee;
342
343 void f(int);
344 using Int = A<int>::B<&f>::param;
345
346 void g(int, int);
347 using Int = A<int>::B<&g>::param2;
348 }
349
350 namespace rdar41852459 {
351 template <auto V> struct G {};
352
353 template <class T> struct S {
frdar41852459::S354 template <auto V> void f() {
355 G<V> x;
356 }
f2rdar41852459::S357 template <auto *PV> void f2() {
358 G<PV> x;
359 }
f3rdar41852459::S360 template <decltype(auto) V> void f3() {
361 G<V> x;
362 }
363 };
364
365 template <auto *PV> struct I {};
366
367 template <class T> struct K {
frdar41852459::K368 template <auto *PV> void f() {
369 I<PV> x;
370 }
f2rdar41852459::K371 template <auto V> void f2() {
372 I<V> x;
373 }
f3rdar41852459::K374 template <decltype(auto) V> void f3() {
375 I<V> x;
376 }
377 };
378
379 template <decltype(auto)> struct L {};
380 template <class T> struct M {
frdar41852459::M381 template <auto *PV> void f() {
382 L<PV> x;
383 }
frdar41852459::M384 template <auto V> void f() {
385 L<V> x;
386 }
frdar41852459::M387 template <decltype(auto) V> void f() {
388 L<V> x;
389 }
390 };
391 }
392
393 namespace PR42362 {
394 template<auto ...A> struct X { struct Y; void f(int...[A]); };
395 template<auto ...A> struct X<A...>::Y {};
f(int...[A])396 template<auto ...A> void X<A...>::f(int...[A]) {}
f()397 void f() { X<1, 2>::Y y; X<1, 2>().f(0, 0); }
398
399 template<typename, auto...> struct Y;
400 template<auto ...A> struct Y<int, A...> {};
401 Y<int, 1, 2, 3> y;
402
403 template<auto (&...F)()> struct Z { struct Q; };
404 template<auto (&...F)()> struct Z<F...>::Q {};
405 Z<f, f, f>::Q q;
406 }
407
408 namespace QualConv {
409 int *X;
f()410 template<const int *const *P> void f() {
411 using T = decltype(P);
412 using T = const int* const*;
413 }
414 template void f<&X>();
415
g()416 template<const int *const &R> void g() {
417 using T = decltype(R);
418 using T = const int *const &;
419 }
420 template void g<(const int *const&)X>();
421 }
422
423 namespace FunctionConversion {
424 struct a { void c(char *) noexcept; };
g()425 template<void (a::*f)(char*)> void g() {
426 using T = decltype(f);
427 using T = void (a::*)(char*); // (not 'noexcept')
428 }
429 template void g<&a::c>();
430
431 void c() noexcept;
h()432 template<void (*p)()> void h() {
433 using T = decltype(p);
434 using T = void (*)(); // (not 'noexcept')
435 }
436 template void h<&c>();
437 }
438
439 namespace VoidPtr {
440 // Note, this is an extension in C++17 but valid in C++20.
f()441 template<void *P> void f() {
442 using T = decltype(P);
443 using T = void*;
444 }
445 int n;
446 template void f<(void*)&n>();
447 }
448
449 namespace PR42108 {
450 struct R {};
SPR42108::S451 struct S { constexpr S() {} constexpr S(R) {} };
operator SPR42108::T452 struct T { constexpr operator S() { return {}; } };
453 template <const S &> struct A {};
f()454 void f() {
455 A<R{}>(); // expected-error {{would bind reference to a temporary}}
456 A<S{}>(); // expected-error {{reference to temporary object}}
457 A<T{}>(); // expected-error {{reference to temporary object}}
458 }
459 }
460
461 namespace PR46637 {
462 template<auto (*f)() -> auto> struct X { // expected-note {{here}}
callPR46637::X463 auto call() { return f(); }
464 };
465 X<nullptr> x; // expected-error {{incompatible initializer}}
466
467 void *f();
468 X<f> y;
469 int n = y.call(); // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type 'void *'}}
470 }
471
472 namespace PR48517 {
473 template<const int *P> struct A { static constexpr const int *p = P; };
make_nonconst()474 template<typename T> auto make_nonconst() {
475 static int n;
476 return A<&n>();
477 };
478 using T = decltype(make_nonconst<int>()); // expected-note {{previous}}
479 using U = decltype(make_nonconst<float>());
480 static_assert(T::p != U::p);
481 using T = U; // expected-error {{different types}}
482
make_const()483 template<typename T> auto make_const() {
484 static constexpr int n = 42;
485 return A<&n>();
486 };
487 using V = decltype(make_const<int>()); // expected-note {{previous}}
488 using W = decltype(make_const<float>());
489 static_assert(*V::p == *W::p);
490 static_assert(V::p != W::p);
491 using V = W; // expected-error {{different types}}
492
493 template<auto V> struct Q {
494 using X = int;
495 static_assert(V == "primary template should not be instantiated");
496 };
497 template<typename T> struct R {
498 int n;
fPR48517::R499 constexpr int f() {
500 return Q<&R::n>::X;
501 }
502 };
503 template<> struct Q<&R<int>::n> { static constexpr int X = 1; };
504 static_assert(R<int>().f() == 1);
505 }
506
507 namespace dependent_reference {
508 template<int &r> struct S { int *q = &r; };
f()509 template<int> auto f() { static int n; return S<n>(); }
510 auto v = f<0>();
511 auto w = f<1>();
512 static_assert(!is_same<decltype(v), decltype(w)>);
513 // Ensure that we can instantiate the definition of S<...>.
514 int n = *v.q + *w.q;
515 }
516
517 namespace decay {
518 template<typename T, typename C, const char *const A[(int)T::count]> struct X {
fdecay::X519 template<typename CC> void f(const X<T, CC, A> &v) {}
520 };
521 struct A {
522 static constexpr const char *arr[] = {"hello", "world"};
523 static constexpr int count = 2;
524 };
f()525 void f() {
526 X<A, int, A::arr> x1;
527 X<A, float, A::arr> x2;
528 x1.f(x2);
529 }
530 }
531
532 namespace TypeSuffix {
533 template <auto N> struct A {};
534 template <> struct A<1> { using type = int; }; // expected-note {{'A<1>::type' declared here}}
535 A<1L>::type a; // expected-error {{no type named 'type' in 'TypeSuffix::A<1L>'; did you mean 'A<1>::type'?}}
536
537 template <auto N> struct B {};
538 template <> struct B<1> { using type = int; }; // expected-note {{'B<1>::type' declared here}}
539 B<2>::type b; // expected-error {{no type named 'type' in 'TypeSuffix::B<2>'; did you mean 'B<1>::type'?}}
540
541 template <auto N> struct C {};
542 template <> struct C<'a'> { using type = signed char; }; // expected-note {{'C<'a'>::type' declared here}}
543 C<(signed char)'a'>::type c; // expected-error {{no type named 'type' in 'TypeSuffix::C<(signed char)'a'>'; did you mean 'C<'a'>::type'?}}
544
545 template <auto N> struct D {};
546 template <> struct D<'a'> { using type = signed char; }; // expected-note {{'D<'a'>::type' declared here}}
547 D<'b'>::type d; // expected-error {{no type named 'type' in 'TypeSuffix::D<'b'>'; did you mean 'D<'a'>::type'?}}
548
549 template <auto N> struct E {};
550 template <> struct E<'a'> { using type = unsigned char; }; // expected-note {{'E<'a'>::type' declared here}}
551 E<(unsigned char)'a'>::type e; // expected-error {{no type named 'type' in 'TypeSuffix::E<(unsigned char)'a'>'; did you mean 'E<'a'>::type'?}}
552
553 template <auto N> struct F {};
554 template <> struct F<'a'> { using type = unsigned char; }; // expected-note {{'F<'a'>::type' declared here}}
555 F<'b'>::type f; // expected-error {{no type named 'type' in 'TypeSuffix::F<'b'>'; did you mean 'F<'a'>::type'?}}
556
557 template <auto... N> struct X {};
558 X<1, 1u>::type y; // expected-error {{no type named 'type' in 'TypeSuffix::X<1, 1U>'}}
559 X<1, 1>::type z; // expected-error {{no type named 'type' in 'TypeSuffix::X<1, 1>'}}
560 }
561