Debian Codegraph @

clang/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp

raw
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 }
16
17 template<typename T> constexpr bool has_type(...) { return false; }
18 template<typename T> constexpr bool has_type(T&) { return true; }
19
20 std::initializer_list il1 = {1, 2, 3, 4, 5};
21 auto il2 = std::initializer_list{1, 2, 3, 4};
22 auto il3 = std::initializer_list{il1};
23 auto il4 = std::initializer_list{il1, il1, il1};
24 static_assert(has_type<std::initializer_list<int>>(il1));
25 static_assert(has_type<std::initializer_list<int>>(il2));
26 static_assert(has_type<std::initializer_list<int>>(il3));
27 static_assert(has_type<std::initializer_list<std::initializer_list<int>>>(il4));
28
29 template<typename T> struct vector {
30 template<typename Iter> vector(Iter, Iter);
31 vector(std::initializer_list<T>);
32 };
33
34 template<typename T> vector(std::initializer_list<T>) -> vector<T>;
35 template<typename Iter> explicit vector(Iter, Iter) -> vector<typename Iter::value_type>;
36 template<typename T> explicit vector(std::size_t, T) -> vector<T>;
37
38 vector v1 = {1, 2, 3, 4};
39 static_assert(has_type<vector<int>>(v1));
40
41 struct iter { typedef char value_type; } it, end;
42 vector v2(it, end);
43 static_assert(has_type<vector<char>>(v2));
44
45 vector v3(5, 5);
46 static_assert(has_type<vector<int>>(v3));
47
48 vector v4 = {it, end};
49 static_assert(has_type<vector<iter>>(v4));
50
51 vector v5{it, end};
52 static_assert(has_type<vector<iter>>(v5));
53
54 template<typename ...T> struct tuple { tuple(T...); };
55 template<typename ...T> explicit tuple(T ...t) -> tuple<T...>; // expected-note {{declared}}
56 // FIXME: Remove
57 template<typename ...T> tuple(tuple<T...>) -> tuple<T...>;
58
59 const int n = 4;
60 tuple ta = tuple{1, 'a', "foo", n};
61 static_assert(has_type<tuple<int, char, const char*, int>>(ta));
62
63 tuple tb{ta};
64 static_assert(has_type<tuple<int, char, const char*, int>>(tb));
65
66 // FIXME: This should be tuple<tuple<...>>; when the above guide is removed.
67 tuple tc = {ta};
68 static_assert(has_type<tuple<int, char, const char*, int>>(tc));
69
70 tuple td = {1, 2, 3}; // expected-error {{selected an explicit deduction guide}}
71 static_assert(has_type<tuple<int, char, const char*, int>>(td));
72
73 // FIXME: This is a GCC extension for now; if CWG don't allow this, at least
74 // add a warning for it.
75 namespace new_expr {
76 tuple<int> *p = new tuple{0};
77 tuple<float, float> *q = new tuple(1.0f, 2.0f);
78 }
79
80 namespace ambiguity {
81 template<typename T> struct A {};
82 A(unsigned short) -> A<int>; // expected-note {{candidate}}
83 A(short) -> A<int>; // expected-note {{candidate}}
84 A a = 0; // expected-error {{ambiguous deduction for template arguments of 'A'}}
85
86 template<typename T> struct B {};
87 template<typename T> B(T(&)(int)) -> B<int>; // expected-note {{candidate function [with T = int]}}
88 template<typename T> B(int(&)(T)) -> B<int>; // expected-note {{candidate function [with T = int]}}
89 int f(int);
90 B b = f; // expected-error {{ambiguous deduction for template arguments of 'B'}}
91 }
92
93 // FIXME: Revisit this once CWG decides if attributes, and [[deprecated]] in
94 // particular, should be permitted here.
95 namespace deprecated {
96 template<typename T> struct A { A(int); };
97 [[deprecated]] A(int) -> A<void>; // expected-note {{marked deprecated here}}
98 A a = 0; // expected-warning {{'<deduction guide for A>' is deprecated}}
99 }
100
101 namespace dependent {
102 template<template<typename...> typename A> decltype(auto) a = A{1, 2, 3};
103 static_assert(has_type<vector<int>>(a<vector>));
104 static_assert(has_type<tuple<int, int, int>>(a<tuple>));
105
106 struct B {
107 template<typename T> struct X { X(T); };
108 X(int) -> X<int>;
109 template<typename T> using Y = X<T>;
110 };
111 template<typename T> void f() {
112 typename T::X tx = 0;
113 typename T::Y ty = 0;
114 }
115 template void f<B>();
116
117 template<typename T> struct C { C(T); };
118 template<typename T> C(T) -> C<T>;
119 template<typename T> void g(T a) {
120 C b = 0;
121 C c = a;
122 using U = decltype(b); // expected-note {{previous}}
123 using U = decltype(c); // expected-error {{different types ('C<const char *>' vs 'C<int>')}}
124 }
125 void h() {
126 g(0);
127 g("foo"); // expected-note {{instantiation of}}
128 }
129 }
130
131 namespace look_into_current_instantiation {
132 template<typename U> struct Q {};
133 template<typename T> struct A {
134 using U = T;
135 template<typename> using V = Q<A<T>::U>;
136 template<typename W = int> A(V<W>);
137 };
138 A a = Q<float>(); // ok, can look through class-scope typedefs and alias
139 // templates, and members of the current instantiation
140 A<float> &r = a;
141
142 template<typename T> struct B { // expected-note {{could not match 'B<T>' against 'int'}} \
143 // expected-note {{implicit deduction guide declared as 'template <typename T> B(B<T>) -> B<T>'}}
144 struct X {
145 typedef T type;
146 };
147 B(typename X::type); // expected-note {{couldn't infer template argument 'T'}} \
148 // expected-note {{implicit deduction guide declared as 'template <typename T> B(typename X::type) -> B<T>'}}
149 };
150 B b = 0; // expected-error {{no viable}}
151
152 // We should have a substitution failure in the immediate context of
153 // deduction when using the C(T, U) constructor (probably; core wording
154 // unclear).
155 template<typename T> struct C {
156 using U = typename T::type;
157 C(T, U);
158 };
159
160 struct R { R(int); typedef R type; };
161 C(...) -> C<R>;
162
163 C c = {1, 2};
164 }
165
166 namespace nondeducible {
167 template<typename A, typename B> struct X {};
168
169 template<typename A> // expected-note {{non-deducible template parameter 'A'}}
170 X() -> X<A, int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}}
171
172 template<typename A> // expected-note {{non-deducible template parameter 'A'}}
173 X(typename X<A, int>::type) -> X<A, int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}}
174
175 template<typename A = int,
176 typename B> // expected-note {{non-deducible template parameter 'B'}}
177 X(int) -> X<A, B>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}}
178
179 template<typename A = int,
180 typename ...B>
181 X(float) -> X<A, B...>; // ok
182
183 template <typename> struct UnnamedTemplateParam {};
184 template <typename> // expected-note {{non-deducible template parameter (anonymous)}}
185 UnnamedTemplateParam() -> UnnamedTemplateParam<int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}}
186 }
187
188 namespace default_args_from_ctor {
189 template <class A> struct S { S(A = 0) {} };
190 S s(0);
191
192 template <class A> struct T { template<typename B> T(A = 0, B = 0) {} };
193 T t(0, 0);
194 }
195
196 namespace transform_params {
197 template<typename T, T N, template<T (*v)[N]> typename U, T (*X)[N]>
198 struct A {
199 template<typename V, V M, V (*Y)[M], template<V (*v)[M]> typename W>
200 A(U<X>, W<Y>);
201
202 static constexpr T v = N;
203 };
204
205 int n[12];
206 template<int (*)[12]> struct Q {};
207 Q<&n> qn;
208 A a(qn, qn);
209 static_assert(a.v == 12);
210
211 template<typename ...T> struct B {
212 template<T ...V> B(const T (&...p)[V]) {
213 constexpr int Vs[] = {V...};
214 static_assert(Vs[0] == 3 && Vs[1] == 4 && Vs[2] == 4);
215 }
216 static constexpr int (*p)(T...) = (int(*)(int, char, char))nullptr;
217 };
218 B b({1, 2, 3}, "foo", {'x', 'y', 'z', 'w'}); // ok
219
220 template<typename ...T> struct C {
221 template<T ...V, template<T...> typename X>
222 C(X<V...>);
223 };
224 template<int...> struct Y {};
225 C c(Y<0, 1, 2>{});
226
227 template<typename ...T> struct D {
228 template<T ...V> D(Y<V...>);
229 };
230 D d(Y<0, 1, 2>{});
231 }
232
233 namespace variadic {
234 int arr3[3], arr4[4];
235
236 // PR32673
237 template<typename T> struct A {
238 template<typename ...U> A(T, U...);
239 };
240 A a(1, 2, 3);
241
242 template<typename T> struct B {
243 template<int ...N> B(T, int (&...r)[N]);
244 };
245 B b(1, arr3, arr4);
246
247 template<typename T> struct C {
248 template<template<typename> typename ...U> C(T, U<int>...);
249 };
250 C c(1, a, b);
251
252 template<typename ...U> struct X {
253 template<typename T> X(T, U...);
254 };
255 X x(1, 2, 3);
256
257 template<int ...N> struct Y {
258 template<typename T> Y(T, int (&...r)[N]);
259 };
260 Y y(1, arr3, arr4);
261
262 template<template<typename> typename ...U> struct Z {
263 template<typename T> Z(T, U<int>...);
264 };
265 Z z(1, a, b);
266 }
267
268 namespace tuple_tests {
269 // The converting n-ary constructor appears viable, deducing T as an empty
270 // pack (until we check its SFINAE constraints).
271 namespace libcxx_1 {
272 template<class ...T> struct tuple {
273 template<class ...Args> struct X { static const bool value = false; };
274 template<class ...U, bool Y = X<U...>::value> tuple(U &&...u);
275 };
276 tuple a = {1, 2, 3};
277 }
278
279 // Don't get caught by surprise when X<...> doesn't even exist in the
280 // selected specialization!
281 namespace libcxx_2 {
282 template<class ...T> struct tuple {
283 template<class ...Args> struct X { static const bool value = false; };
284 // Substitution into X<U...>::value succeeds but produces the
285 // value-dependent expression
286 // tuple<T...>::X<>::value
287 // FIXME: Is that the right behavior?
288 template<class ...U, bool Y = X<U...>::value> tuple(U &&...u);
289 };
290 template <> class tuple<> {};
291 tuple a = {1, 2, 3}; // expected-error {{excess elements in struct initializer}}
292 }
293
294 namespace libcxx_3 {
295 template<typename ...T> struct scoped_lock {
296 scoped_lock(T...);
297 };
298 template<> struct scoped_lock<> {};
299 scoped_lock l = {};
300 }
301 }
302
303 namespace dependent {
304 template<typename T> struct X { // expected-note 3{{here}}
305 X(T);
306 };
307 template<typename T> int Var(T t) {
308 X x(t);
309 return X(x) + 1; // expected-error {{invalid operands}}
310 }
311 template<typename T> int Cast(T t) {
312 return X(X(t)) + 1; // expected-error {{invalid operands}}
313 }
314 template<typename T> int Cast2(T t) {
315 return (X)(X)t + 1; // expected-error {{deduction not allowed}}
316 }
317 template<typename T> int Cast3(T t) {
318 return X{X{t}} + 1; // expected-error {{invalid operands}}
319 }
320 template<typename T> int Cast4(T t) {
321 return (X){(X){t}} + 1; // expected-error 2{{deduction not allowed}}
322 }
323 template<typename T> int New(T t) {
324 return X(new X(t)) + 1; // expected-error {{invalid operands}}
325 };
326 template<typename T> int *New2(T t) {
327 return new X(X(t)) * 2; // expected-error {{invalid operands}}
328 };
329 template int Var(float); // expected-note {{instantiation of}}
330 template int Cast(float); // expected-note {{instantiation of}}
331 template int Cast3(float); // expected-note {{instantiation of}}
332 template int New(float); // expected-note {{instantiation of}}
333 template int *New2(float); // expected-note {{instantiation of}}
334 template<typename T> int operator+(X<T>, int);
335 template int Var(int);
336 template int Cast(int);
337 template int New(int);
338
339 template<template<typename> typename Y> void test() {
340 Y(0);
341 new Y(0);
342 Y y(0);
343 }
344 template void test<X>();
345 }
346
347 namespace injected_class_name {
348 template<typename T = void> struct A {
349 A();
350 template<typename U> A(A<U>);
351 };
352 A<int> a;
353 A b = a;
354 using T = decltype(a);
355 using T = decltype(b);
356 }
357
358 namespace member_guides {
359 // PR34520
360 template<class>
361 struct Foo {
362 template <class T> struct Bar {
363 Bar(...) {}
364 };
365 Bar(int) -> Bar<int>;
366 };
367 Foo<int>::Bar b = 0;
368
369 struct A {
370 template<typename T> struct Public; // expected-note {{declared public}}
371 Public(float) -> Public<float>;
372 protected: // expected-note {{declared protected by intervening access specifier}}
373 template<typename T> struct Protected; // expected-note 2{{declared protected}}
374 Protected(float) -> Protected<float>;
375 Public(int) -> Public<int>; // expected-error {{different access}}
376 private: // expected-note {{declared private by intervening access specifier}}
377 template<typename T> struct Private; // expected-note {{declared private}}
378 Protected(int) -> Protected<int>; // expected-error {{different access}}
379 public: // expected-note 2{{declared public by intervening access specifier}}
380 template<typename T> Public(T) -> Public<T>;
381 template<typename T> Protected(T) -> Protected<T>; // expected-error {{different access}}
382 template<typename T> Private(T) -> Private<T>; // expected-error {{different access}}
383 };
384 }
385
386 namespace rdar41903969 {
387 template <class T> struct A {};
388 template <class T> struct B;
389 template <class T> struct C {
390 C(A<T>&);
391 C(B<T>&);
392 };
393
394 void foo(A<int> &a, B<int> &b) {
395 (void)C{b};
396 (void)C{a};
397 }
398
399 template<typename T> struct X {
400 X(std::initializer_list<T>) = delete;
401 X(const X&);
402 };
403
404 template <class T> struct D : X<T> {};
405
406 void bar(D<int>& d) {
407 (void)X{d};
408 }
409 }
410
411 namespace rdar41330135 {
412 template <int> struct A {};
413 template <class T>
414 struct S {
415 template <class U>
416 S(T a, U t, A<sizeof(t)>);
417 };
418 template <class T> struct D {
419 D(T t, A<sizeof(t)>);
420 };
421 int f() {
422 S s(0, 0, A<sizeof(int)>());
423 D d(0, A<sizeof(int)>());
424 }
425
426 namespace test_dupls {
427 template<unsigned long> struct X {};
428 template<typename T> struct A {
429 A(T t, X<sizeof(t)>);
430 };
431 A a(0, {});
432 template<typename U> struct B {
433 B(U u, X<sizeof(u)>);
434 };
435 B b(0, {});
436 }
437
438 }
439
440 namespace no_crash_on_default_arg {
441 class A {
442 template <typename T> class B {
443 B(int c = 1);
444 };
445 // This used to crash due to unparsed default arg above. The diagnostic could
446 // be improved, but the point of this test is to simply check we do not crash.
447 B(); // expected-error {{deduction guide declaration without trailing return type}}
448 };
449 } // namespace no_crash_on_default_arg
450
451 #pragma clang diagnostic push
452 #pragma clang diagnostic warning "-Wctad-maybe-unsupported"
453 namespace test_implicit_ctad_warning {
454
455 template <class T>
456 struct Tag {};
457
458 template <class T>
459 struct NoExplicit { // expected-note {{add a deduction guide to suppress this warning}}
460 NoExplicit(T) {}
461 NoExplicit(T, int) {}
462 };
463
464 // expected-warning@+1 {{'NoExplicit' may not intend to support class template argument deduction}}
465 NoExplicit ne(42);
466
467 template <class U>
468 struct HasExplicit {
469 HasExplicit(U) {}
470 HasExplicit(U, int) {}
471 };
472 template <class U> HasExplicit(U, int) -> HasExplicit<Tag<U>>;
473
474 HasExplicit he(42);
475
476 // Motivating examples from (taken from Stephan Lavavej's 2018 Cppcon talk)
477 template <class T, class U>
478 struct AmateurPair { // expected-note {{add a deduction guide to suppress this warning}}
479 T first;
480 U second;
481 explicit AmateurPair(const T &t, const U &u) {}
482 };
483 // expected-warning@+1 {{'AmateurPair' may not intend to support class template argument deduction}}
484 AmateurPair p1(42, "hello world"); // deduces to Pair<int, char[12]>
485
486 template <class T, class U>
487 struct AmateurPair2 { // expected-note {{add a deduction guide to suppress this warning}}
488 T first;
489 U second;
490 explicit AmateurPair2(T t, U u) {}
491 };
492 // expected-warning@+1 {{'AmateurPair2' may not intend to support class template argument deduction}}
493 AmateurPair2 p2(42, "hello world"); // deduces to Pair2<int, const char*>
494
495 template <class T, class U>
496 struct ProPair {
497 T first; U second;
498 explicit ProPair(T const& t, U const& u) {}
499 };
500 template<class T1, class T2>
501 ProPair(T1, T2) -> ProPair<T1, T2>;
502 ProPair p3(42, "hello world"); // deduces to ProPair<int, const char*>
503 static_assert(__is_same(decltype(p3), ProPair<int, const char*>));
504
505 // Test that user-defined explicit guides suppress the warning even if they
506 // aren't used as candidates.
507 template <class T>
508 struct TestExplicitCtor {
509 TestExplicitCtor(T) {}
510 };
511 template <class T>
512 explicit TestExplicitCtor(TestExplicitCtor<T> const&) -> TestExplicitCtor<void>;
513 TestExplicitCtor<int> ce1{42};
514 TestExplicitCtor ce2 = ce1;
515 static_assert(__is_same(decltype(ce2), TestExplicitCtor<int>), "");
516
517 struct allow_ctad_t {
518 allow_ctad_t() = delete;
519 };
520
521 template <class T>
522 struct TestSuppression {
523 TestSuppression(T) {}
524 };
525 TestSuppression(allow_ctad_t)->TestSuppression<void>;
526 TestSuppression ta("abc");
527 static_assert(__is_same(decltype(ta), TestSuppression<const char *>), "");
528 }
529 #pragma clang diagnostic pop
530
531 namespace PR41549 {
532
533 template <class H, class P> struct umm;
534
535 template <class H = int, class P = int>
536 struct umm {
537 umm(H h = 0, P p = 0);
538 };
539
540 template <class H, class P> struct umm;
541
542 umm m(1);
543
544 }
545
546 namespace PR45124 {
547 class a { int d; };
548 class b : a {};
549
550 struct x { ~x(); };
551 template<typename> class y { y(x = x()); };
552 template<typename z> y(z)->y<z>;
553
554 // Not a constant initializer, but trivial default initialization. We won't
555 // detect this as trivial default initialization if synthesizing the implicit
556 // deduction guide 'template<typename T> y(x = x()) -> Y<T>;' leaves behind a
557 // pending cleanup.
558 __thread b g;
559 }
560
561 namespace PR47175 {
562 template<typename T> struct A { A(T); T x; };
563 template<typename T> int &&n = A(T()).x;
564 int m = n<int>;
565 }
566
567 // Ensure we don't crash when CTAD fails.
568 template <typename T1, typename T2>
569 struct Foo { // expected-note {{candidate function template not viable}} \
570 // expected-note {{implicit deduction guide declared as 'template <typename T1, typename T2> Foo(Foo<T1, T2>) -> Foo<T1, T2>'}}
571 Foo(T1, T2); // expected-note {{candidate function template not viable}} \
572 // expected-note {{implicit deduction guide declared as 'template <typename T1, typename T2> Foo(T1, T2) -> Foo<T1, T2>'}}
573 };
574
575 template <typename... Args>
576 void insert(Args &&...args);
577
578 void foo() {
579 insert(Foo(2, 2, 2)); // expected-error{{no viable constructor or deduction guide}}
580 }
581
582 namespace PR52139 {
583 struct Abstract {
584 template <class... Ts>
585 struct overloaded : Ts... {
586 using Ts::operator()...;
587 };
588 template <class... Ts>
589 overloaded(Ts...) -> overloaded<Ts...>;
590
591 private:
592 virtual void f() = 0;
593 };
594 }
595
596 namespace function_prototypes {
597 template<class T> using fptr1 = void (*) (T);
598 template<class T> using fptr2 = fptr1<fptr1<T>>;
599
600 template<class T> void foo0(fptr1<T>) {
601 static_assert(__is_same(T, const char*));
602 }
603 void bar0(const char *const volatile __restrict);
604 void t0() { foo0(&bar0); }
605
606 template<class T> void foo1(fptr1<const T *>) {
607 static_assert(__is_same(T, char));
608 }
609 void bar1(const char * __restrict);
610 void t1() { foo1(&bar1); }
611
612 template<class T> void foo2(fptr2<const T *>) {
613 static_assert(__is_same(T, char));
614 }
615 void bar2(fptr1<const char * __restrict>);
616 void t2() { foo2(&bar2); }
617
618 template<class T> void foo3(fptr1<const T *>) {}
619 void bar3(char * __restrict);
620 void t3() { foo3(&bar3); }
621 // expected-error@-1 {{no matching function for call to 'foo3'}}
622 // expected-note@-4 {{candidate template ignored: cannot deduce a type for 'T' that would make 'const T' equal 'char'}}
623
624 template<class T> void foo4(fptr2<const T *>) {}
625 void bar4(fptr1<char * __restrict>);
626 void t4() { foo4(&bar4); }
627 // expected-error@-1 {{no matching function for call to 'foo4'}}
628 // expected-note@-4 {{candidate template ignored: cannot deduce a type for 'T' that would make 'const T' equal 'char'}}
629
630 template<typename T> void foo5(T(T)) {}
631 const int bar5(int);
632 void t5() { foo5(bar5); }
633 // expected-error@-1 {{no matching function for call to 'foo5'}}
634 // expected-note@-4 {{candidate template ignored: deduced conflicting types for parameter 'T' ('const int' vs. 'int')}}
635
636 struct Foo6 {};
637 template<typename T> void foo6(void(*)(struct Foo6, T)) {}
638 void bar6(Foo6, int);
639 void t6() { foo6(bar6); }
640 }
641 #else
642
643 // expected-no-diagnostics
644 namespace undefined_warnings {
645 // Make sure we don't get an "undefined but used internal symbol" warning for the deduction guide here.
646 namespace {
647 template <typename T>
648 struct TemplDObj {
649 explicit TemplDObj(T func) noexcept {}
650 };
651 auto test1 = TemplDObj(0);
652
653 TemplDObj(float) -> TemplDObj<double>;
654 auto test2 = TemplDObj(.0f);
655 }
656 }
657
658 namespace GH51710 {
659 template<typename T>
660 struct A {
661 A(T f()) {}
662 A(int f(), T) {}
663
664 A(T array[10]) {}
665 A(int array[10], T) {}
666 };
667
668 template<typename T>
669 struct B {
670 B(T array[]) {}
671 B(int array[], T) {}
672 };
673
674
675 int foo();
676
677 void bar() {
678 A test1(foo);
679 A test2(foo, 1);
680
681 int array[10];
682 A test3(array);
683 A test4(array, 1);
684
685 B test5(array);
686 B test6(array, 1);
687 }
688 } // namespace GH51710
689
690 #endif