1 // RUN: %clang_cc1 -std=c++1z -verify %s -DERRORS -Wundefined-func-template
2 // RUN: %clang_cc1 -std=c++1z -verify %s -UERRORS -Wundefined-func-template
3
4 // This test is split into two because we only produce "undefined internal"
5 // warnings if we didn't produce any errors.
6 #if ERRORS
7
8 namespace std {
9 using size_t = decltype(sizeof(0));
10 template<typename T> struct initializer_list {
11 const T *p;
12 size_t n;
13 initializer_list();
14 };
15 // FIXME: This should probably not be necessary.
16 template<typename T> initializer_list(initializer_list<T>) -> initializer_list<T>;
17 }
18
has_type(...)19 template<typename T> constexpr bool has_type(...) { return false; }
has_type(T)20 template<typename T> constexpr bool has_type(T) { return true; }
21
22 std::initializer_list il = {1, 2, 3, 4, 5};
23
24 template<typename T> struct vector {
25 template<typename Iter> vector(Iter, Iter);
26 vector(std::initializer_list<T>);
27 };
28
29 template<typename T> vector(std::initializer_list<T>) -> vector<T>;
30 template<typename Iter> explicit vector(Iter, Iter) -> vector<typename Iter::value_type>;
31 template<typename T> explicit vector(std::size_t, T) -> vector<T>;
32
33 vector v1 = {1, 2, 3, 4};
34 static_assert(has_type<vector<int>>(v1));
35
36 struct iter { typedef char value_type; } it, end;
37 vector v2(it, end);
38 static_assert(has_type<vector<char>>(v2));
39
40 vector v3(5, 5);
41 static_assert(has_type<vector<int>>(v3));
42
43 vector v4 = {it, end};
44 static_assert(has_type<vector<iter>>(v4));
45
46 vector v5{it, end};
47 static_assert(has_type<vector<iter>>(v5));
48
49 template<typename ...T> struct tuple { tuple(T...); };
50 template<typename ...T> explicit tuple(T ...t) -> tuple<T...>; // expected-note {{declared}}
51 // FIXME: Remove
52 template<typename ...T> tuple(tuple<T...>) -> tuple<T...>;
53
54 const int n = 4;
55 tuple ta = tuple{1, 'a', "foo", n};
56 static_assert(has_type<tuple<int, char, const char*, int>>(ta));
57
58 tuple tb{ta};
59 static_assert(has_type<tuple<int, char, const char*, int>>(tb));
60
61 // FIXME: This should be tuple<tuple<...>>; when the above guide is removed.
62 tuple tc = {ta};
63 static_assert(has_type<tuple<int, char, const char*, int>>(tc));
64
65 tuple td = {1, 2, 3}; // expected-error {{selected an explicit deduction guide}}
66 static_assert(has_type<tuple<int, char, const char*, int>>(td));
67
68 // FIXME: This is a GCC extension for now; if CWG don't allow this, at least
69 // add a warning for it.
70 namespace new_expr {
71 tuple<int> *p = new tuple{0};
72 tuple<float, float> *q = new tuple(1.0f, 2.0f);
73 }
74
75 namespace ambiguity {
76 template<typename T> struct A {};
77 A(unsigned short) -> A<int>; // expected-note {{candidate}}
78 A(short) -> A<int>; // expected-note {{candidate}}
79 A a = 0; // expected-error {{ambiguous deduction for template arguments of 'A'}}
80
81 template<typename T> struct B {};
82 template<typename T> B(T(&)(int)) -> B<int>; // expected-note {{candidate function [with T = int]}}
83 template<typename T> B(int(&)(T)) -> B<int>; // expected-note {{candidate function [with T = int]}}
84 int f(int);
85 B b = f; // expected-error {{ambiguous deduction for template arguments of 'B'}}
86 }
87
88 // FIXME: Revisit this once CWG decides if attributes, and [[deprecated]] in
89 // particular, should be permitted here.
90 namespace deprecated {
91 template<typename T> struct A { A(int); };
92 [[deprecated]] A(int) -> A<void>; // expected-note {{marked deprecated here}}
93 A a = 0; // expected-warning {{'<deduction guide for A>' is deprecated}}
94 }
95
96 namespace dependent {
97 template<template<typename...> typename A> decltype(auto) a = A{1, 2, 3};
98 static_assert(has_type<vector<int>>(a<vector>));
99 static_assert(has_type<tuple<int, int, int>>(a<tuple>));
100
101 struct B {
102 template<typename T> struct X { X(T); };
103 X(int) -> X<int>;
104 template<typename T> using Y = X<T>; // expected-note {{template}}
105 };
f()106 template<typename T> void f() {
107 typename T::X tx = 0;
108 typename T::Y ty = 0; // expected-error {{alias template 'Y' requires template arguments; argument deduction only allowed for class templates}}
109 }
110 template void f<B>(); // expected-note {{in instantiation of}}
111
112 template<typename T> struct C { C(T); };
113 template<typename T> C(T) -> C<T>;
g(T a)114 template<typename T> void g(T a) {
115 C b = 0;
116 C c = a;
117 using U = decltype(b); // expected-note {{previous}}
118 using U = decltype(c); // expected-error {{different types ('C<const char *>' vs 'C<int>')}}
119 }
h()120 void h() {
121 g(0);
122 g("foo"); // expected-note {{instantiation of}}
123 }
124 }
125
126 namespace look_into_current_instantiation {
127 template<typename U> struct Q {};
128 template<typename T> struct A {
129 using U = T;
130 template<typename> using V = Q<A<T>::U>;
131 template<typename W = int> A(V<W>);
132 };
133 A a = Q<float>(); // ok, can look through class-scope typedefs and alias
134 // templates, and members of the current instantiation
135 A<float> &r = a;
136
137 template<typename T> struct B { // expected-note {{could not match 'B<T>' against 'int'}}
138 struct X {
139 typedef T type;
140 };
141 B(typename X::type); // expected-note {{couldn't infer template argument 'T'}}
142 };
143 B b = 0; // expected-error {{no viable}}
144
145 // We should have a substitution failure in the immediate context of
146 // deduction when using the C(T, U) constructor (probably; core wording
147 // unclear).
148 template<typename T> struct C {
149 using U = typename T::type;
150 C(T, U);
151 };
152
153 struct R { R(int); typedef R type; };
154 C(...) -> C<R>;
155
156 C c = {1, 2};
157 }
158
159 namespace nondeducible {
160 template<typename A, typename B> struct X {};
161
162 template<typename A> // expected-note {{non-deducible template parameter 'A'}}
163 X() -> X<A, int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}}
164
165 template<typename A> // expected-note {{non-deducible template parameter 'A'}}
166 X(typename X<A, int>::type) -> X<A, int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}}
167
168 template<typename A = int,
169 typename B> // expected-note {{non-deducible template parameter 'B'}}
170 X(int) -> X<A, B>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}}
171
172 template<typename A = int,
173 typename ...B>
174 X(float) -> X<A, B...>; // ok
175 }
176
177 namespace default_args_from_ctor {
Sdefault_args_from_ctor::S178 template <class A> struct S { S(A = 0) {} };
179 S s(0);
180
Tdefault_args_from_ctor::T181 template <class A> struct T { template<typename B> T(A = 0, B = 0) {} };
182 T t(0, 0);
183 }
184
185 namespace transform_params {
186 template<typename T, T N, template<T (*v)[N]> typename U, T (*X)[N]>
187 struct A {
188 template<typename V, V M, V (*Y)[M], template<V (*v)[M]> typename W>
189 A(U<X>, W<Y>);
190
191 static constexpr T v = N;
192 };
193
194 int n[12];
195 template<int (*)[12]> struct Q {};
196 Q<&n> qn;
197 A a(qn, qn);
198 static_assert(a.v == 12);
199
200 template<typename ...T> struct B {
Btransform_params::B201 template<T ...V> B(const T (&...p)[V]) {
202 constexpr int Vs[] = {V...};
203 static_assert(Vs[0] == 3 && Vs[1] == 4 && Vs[2] == 4);
204 }
205 static constexpr int (*p)(T...) = (int(*)(int, char, char))nullptr;
206 };
207 B b({1, 2, 3}, "foo", {'x', 'y', 'z', 'w'}); // ok
208
209 template<typename ...T> struct C {
210 template<T ...V, template<T...> typename X>
211 C(X<V...>);
212 };
213 template<int...> struct Y {};
214 C c(Y<0, 1, 2>{});
215
216 template<typename ...T> struct D {
217 template<T ...V> D(Y<V...>);
218 };
219 D d(Y<0, 1, 2>{});
220 }
221
222 namespace variadic {
223 int arr3[3], arr4[4];
224
225 // PR32673
226 template<typename T> struct A {
227 template<typename ...U> A(T, U...);
228 };
229 A a(1, 2, 3);
230
231 template<typename T> struct B {
232 template<int ...N> B(T, int (&...r)[N]);
233 };
234 B b(1, arr3, arr4);
235
236 template<typename T> struct C {
237 template<template<typename> typename ...U> C(T, U<int>...);
238 };
239 C c(1, a, b);
240
241 template<typename ...U> struct X {
242 template<typename T> X(T, U...);
243 };
244 X x(1, 2, 3);
245
246 template<int ...N> struct Y {
247 template<typename T> Y(T, int (&...r)[N]);
248 };
249 Y y(1, arr3, arr4);
250
251 template<template<typename> typename ...U> struct Z {
252 template<typename T> Z(T, U<int>...);
253 };
254 Z z(1, a, b);
255 }
256
257 namespace tuple_tests {
258 // The converting n-ary constructor appears viable, deducing T as an empty
259 // pack (until we check its SFINAE constraints).
260 namespace libcxx_1 {
261 template<class ...T> struct tuple {
262 template<class ...Args> struct X { static const bool value = false; };
263 template<class ...U, bool Y = X<U...>::value> tuple(U &&...u);
264 };
265 tuple a = {1, 2, 3};
266 }
267
268 // Don't get caught by surprise when X<...> doesn't even exist in the
269 // selected specialization!
270 namespace libcxx_2 {
271 template<class ...T> struct tuple {
272 template<class ...Args> struct X { static const bool value = false; };
273 // Substitution into X<U...>::value succeeds but produces the
274 // value-dependent expression
275 // tuple<T...>::X<>::value
276 // FIXME: Is that the right behavior?
277 template<class ...U, bool Y = X<U...>::value> tuple(U &&...u);
278 };
279 template <> class tuple<> {};
280 tuple a = {1, 2, 3}; // expected-error {{excess elements in struct initializer}}
281 }
282
283 namespace libcxx_3 {
284 template<typename ...T> struct scoped_lock {
285 scoped_lock(T...);
286 };
287 template<> struct scoped_lock<> {};
288 scoped_lock l = {};
289 }
290 }
291
292 namespace dependent {
293 template<typename T> struct X {
294 X(T);
295 };
Var(T t)296 template<typename T> int Var(T t) {
297 X x(t);
298 return X(x) + 1; // expected-error {{invalid operands}}
299 }
Cast(T t)300 template<typename T> int Cast(T t) {
301 return X(X(t)) + 1; // expected-error {{invalid operands}}
302 }
New(T t)303 template<typename T> int New(T t) {
304 return X(new X(t)) + 1; // expected-error {{invalid operands}}
305 };
306 template int Var(float); // expected-note {{instantiation of}}
307 template int Cast(float); // expected-note {{instantiation of}}
308 template int New(float); // expected-note {{instantiation of}}
309 template<typename T> int operator+(X<T>, int);
310 template int Var(int);
311 template int Cast(int);
312 template int New(int);
313
test()314 template<template<typename> typename Y> void test() {
315 Y(0);
316 new Y(0);
317 Y y(0);
318 }
319 template void test<X>();
320 }
321
322 namespace injected_class_name {
323 template<typename T = void> struct A {
324 A();
325 template<typename U> A(A<U>);
326 };
327 A<int> a;
328 A b = a;
329 using T = decltype(a);
330 using T = decltype(b);
331 }
332
333 namespace member_guides {
334 // PR34520
335 template<class>
336 struct Foo {
337 template <class T> struct Bar {
Barmember_guides::Foo::Bar338 Bar(...) {}
339 };
340 Bar(int) -> Bar<int>;
341 };
342 Foo<int>::Bar b = 0;
343
344 struct A {
345 template<typename T> struct Public; // expected-note {{declared public}}
346 Public(float) -> Public<float>;
347 protected: // expected-note {{declared protected by intervening access specifier}}
348 template<typename T> struct Protected; // expected-note 2{{declared protected}}
349 Protected(float) -> Protected<float>;
350 Public(int) -> Public<int>; // expected-error {{different access}}
351 private: // expected-note {{declared private by intervening access specifier}}
352 template<typename T> struct Private; // expected-note {{declared private}}
353 Protected(int) -> Protected<int>; // expected-error {{different access}}
354 public: // expected-note 2{{declared public by intervening access specifier}}
355 template<typename T> Public(T) -> Public<T>;
356 template<typename T> Protected(T) -> Protected<T>; // expected-error {{different access}}
357 template<typename T> Private(T) -> Private<T>; // expected-error {{different access}}
358 };
359 }
360
361 namespace rdar41903969 {
362 template <class T> struct A {};
363 template <class T> struct B;
364 template <class T> struct C {
365 C(A<T>&);
366 C(B<T>&);
367 };
368
foo(A<int> & a,B<int> & b)369 void foo(A<int> &a, B<int> &b) {
370 (void)C{b};
371 (void)C{a};
372 }
373
374 template<typename T> struct X {
375 X(std::initializer_list<T>) = delete;
376 X(const X&);
377 };
378
379 template <class T> struct D : X<T> {};
380
bar(D<int> & d)381 void bar(D<int>& d) {
382 (void)X{d};
383 }
384 }
385
386 namespace rdar41330135 {
387 template <int> struct A {};
388 template <class T>
389 struct S {
390 template <class U>
391 S(T a, U t, A<sizeof(t)>);
392 };
393 template <class T> struct D {
394 D(T t, A<sizeof(t)>);
395 };
f()396 int f() {
397 S s(0, 0, A<sizeof(int)>());
398 D d(0, A<sizeof(int)>());
399 }
400
401 namespace test_dupls {
402 template<unsigned long> struct X {};
403 template<typename T> struct A {
404 A(T t, X<sizeof(t)>);
405 };
406 A a(0, {});
407 template<typename U> struct B {
408 B(U u, X<sizeof(u)>);
409 };
410 B b(0, {});
411 }
412
413 }
414
415 #pragma clang diagnostic push
416 #pragma clang diagnostic warning "-Wctad-maybe-unsupported"
417 namespace test_implicit_ctad_warning {
418
419 template <class T>
420 struct Tag {};
421
422 template <class T>
423 struct NoExplicit { // expected-note {{add a deduction guide to suppress this warning}}
NoExplicittest_implicit_ctad_warning::NoExplicit424 NoExplicit(T) {}
NoExplicittest_implicit_ctad_warning::NoExplicit425 NoExplicit(T, int) {}
426 };
427
428 // expected-warning@+1 {{'NoExplicit' may not intend to support class template argument deduction}}
429 NoExplicit ne(42);
430
431 template <class U>
432 struct HasExplicit {
HasExplicittest_implicit_ctad_warning::HasExplicit433 HasExplicit(U) {}
HasExplicittest_implicit_ctad_warning::HasExplicit434 HasExplicit(U, int) {}
435 };
436 template <class U> HasExplicit(U, int) -> HasExplicit<Tag<U>>;
437
438 HasExplicit he(42);
439
440 // Motivating examples from (taken from Stephan Lavavej's 2018 Cppcon talk)
441 template <class T, class U>
442 struct AmateurPair { // expected-note {{add a deduction guide to suppress this warning}}
443 T first;
444 U second;
AmateurPairtest_implicit_ctad_warning::AmateurPair445 explicit AmateurPair(const T &t, const U &u) {}
446 };
447 // expected-warning@+1 {{'AmateurPair' may not intend to support class template argument deduction}}
448 AmateurPair p1(42, "hello world"); // deduces to Pair<int, char[12]>
449
450 template <class T, class U>
451 struct AmateurPair2 { // expected-note {{add a deduction guide to suppress this warning}}
452 T first;
453 U second;
AmateurPair2test_implicit_ctad_warning::AmateurPair2454 explicit AmateurPair2(T t, U u) {}
455 };
456 // expected-warning@+1 {{'AmateurPair2' may not intend to support class template argument deduction}}
457 AmateurPair2 p2(42, "hello world"); // deduces to Pair2<int, const char*>
458
459 template <class T, class U>
460 struct ProPair {
461 T first; U second;
ProPairtest_implicit_ctad_warning::ProPair462 explicit ProPair(T const& t, U const& u) {}
463 };
464 template<class T1, class T2>
465 ProPair(T1, T2) -> ProPair<T1, T2>;
466 ProPair p3(42, "hello world"); // deduces to ProPair<int, const char*>
467 static_assert(__is_same(decltype(p3), ProPair<int, const char*>));
468
469 // Test that user-defined explicit guides suppress the warning even if they
470 // aren't used as candidates.
471 template <class T>
472 struct TestExplicitCtor {
TestExplicitCtortest_implicit_ctad_warning::TestExplicitCtor473 TestExplicitCtor(T) {}
474 };
475 template <class T>
476 explicit TestExplicitCtor(TestExplicitCtor<T> const&) -> TestExplicitCtor<void>;
477 TestExplicitCtor<int> ce1{42};
478 TestExplicitCtor ce2 = ce1;
479 static_assert(__is_same(decltype(ce2), TestExplicitCtor<int>), "");
480
481 struct allow_ctad_t {
482 allow_ctad_t() = delete;
483 };
484
485 template <class T>
486 struct TestSuppression {
TestSuppressiontest_implicit_ctad_warning::TestSuppression487 TestSuppression(T) {}
488 };
489 TestSuppression(allow_ctad_t)->TestSuppression<void>;
490 TestSuppression ta("abc");
491 static_assert(__is_same(decltype(ta), TestSuppression<const char *>), "");
492 }
493 #pragma clang diagnostic pop
494
495 namespace PR41549 {
496
497 template <class H, class P> struct umm;
498
499 template <class H = int, class P = int>
500 struct umm {
501 umm(H h = 0, P p = 0);
502 };
503
504 template <class H, class P> struct umm;
505
506 umm m(1);
507
508 }
509
510 namespace PR45124 {
511 class a { int d; };
512 class b : a {};
513
514 struct x { ~x(); };
515 template<typename> class y { y(x = x()); };
516 template<typename z> y(z)->y<z>;
517
518 // Not a constant initializer, but trivial default initialization. We won't
519 // detect this as trivial default initialization if synthesizing the implicit
520 // deduction guide 'template<typename T> y(x = x()) -> Y<T>;' leaves behind a
521 // pending cleanup.
522 __thread b g;
523 }
524
525 #else
526
527 // expected-no-diagnostics
528 namespace undefined_warnings {
529 // Make sure we don't get an "undefined but used internal symbol" warning for the deduction guide here.
530 namespace {
531 template <typename T>
532 struct TemplDObj {
TemplDObjundefined_warnings::__anonc49db5a10111::TemplDObj533 explicit TemplDObj(T func) noexcept {}
534 };
535 auto test1 = TemplDObj(0);
536
537 TemplDObj(float) -> TemplDObj<double>;
538 auto test2 = TemplDObj(.0f);
539 }
540 }
541 #endif
542