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
4 // This test is split into two because we only produce "undefined internal"
5 // warnings if we didn't produce any errors.
9 using size_t = decltype(sizeof(0));
10 template<typename T
> struct initializer_list
{
15 // FIXME: This should probably not be necessary.
16 template<typename T
> initializer_list(initializer_list
<T
>) -> initializer_list
<T
>;
19 template<typename T
> constexpr bool has_type(...) { return false; }
20 template<typename T
> constexpr bool has_type(T
&) { return true; }
22 std::initializer_list il
= {1, 2, 3, 4, 5};
24 template<typename T
> struct vector
{
25 template<typename Iter
> vector(Iter
, Iter
);
26 vector(std::initializer_list
<T
>);
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
>;
33 vector v1
= {1, 2, 3, 4};
34 static_assert(has_type
<vector
<int>>(v1
));
36 struct iter
{ typedef char value_type
; } it
, end
;
38 static_assert(has_type
<vector
<char>>(v2
));
41 static_assert(has_type
<vector
<int>>(v3
));
43 vector v4
= {it
, end
};
44 static_assert(has_type
<vector
<iter
>>(v4
));
47 static_assert(has_type
<vector
<iter
>>(v5
));
49 template<typename
...T
> struct tuple
{ tuple(T
...); };
50 template<typename
...T
> explicit tuple(T
...t
) -> tuple
<T
...>; // expected-note {{declared}}
52 template<typename
...T
> tuple(tuple
<T
...>) -> tuple
<T
...>;
55 tuple ta
= tuple
{1, 'a', "foo", n
};
56 static_assert(has_type
<tuple
<int, char, const char*, int>>(ta
));
59 static_assert(has_type
<tuple
<int, char, const char*, int>>(tb
));
61 // FIXME: This should be tuple<tuple<...>>; when the above guide is removed.
63 static_assert(has_type
<tuple
<int, char, const char*, int>>(tc
));
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
));
68 // FIXME: This is a GCC extension for now; if CWG don't allow this, at least
69 // add a warning for it.
71 tuple
<int> *p
= new tuple
{0};
72 tuple
<float, float> *q
= new tuple(1.0f
, 2.0f
);
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'}}
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]}}
85 B b
= f
; // expected-error {{ambiguous deduction for template arguments of 'B'}}
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}}
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
>));
102 template<typename T
> struct X
{ X(T
); };
104 template<typename T
> using Y
= X
<T
>; // expected-note {{template}}
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}}
110 template void f
<B
>(); // expected-note {{in instantiation of}}
112 template<typename T
> struct C
{ C(T
); };
113 template<typename T
> C(T
) -> C
<T
>;
114 template<typename T
> void g(T a
) {
117 using U
= decltype(b
); // expected-note {{previous}}
118 using U
= decltype(c
); // expected-error {{different types ('C<const char *>' vs 'C<int>')}}
122 g("foo"); // expected-note {{instantiation of}}
126 namespace look_into_current_instantiation
{
127 template<typename U
> struct Q
{};
128 template<typename T
> struct A
{
130 template<typename
> using V
= Q
<A
<T
>::U
>;
131 template<typename W
= int> A(V
<W
>);
133 A a
= Q
<float>(); // ok, can look through class-scope typedefs and alias
134 // templates, and members of the current instantiation
137 template<typename T
> struct B
{ // expected-note {{could not match 'B<T>' against 'int'}}
141 B(typename
X::type
); // expected-note {{couldn't infer template argument 'T'}}
143 B b
= 0; // expected-error {{no viable}}
145 // We should have a substitution failure in the immediate context of
146 // deduction when using the C(T, U) constructor (probably; core wording
148 template<typename T
> struct C
{
149 using U
= typename
T::type
;
153 struct R
{ R(int); typedef R type
; };
159 namespace nondeducible
{
160 template<typename A
, typename B
> struct X
{};
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}}
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}}
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}}
172 template<typename A
= int,
174 X(float) -> X
<A
, B
...>; // ok
176 template <typename
> struct UnnamedTemplateParam
{};
177 template <typename
> // expected-note {{non-deducible template parameter (anonymous)}}
178 UnnamedTemplateParam() -> UnnamedTemplateParam
<int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}}
181 namespace default_args_from_ctor
{
182 template <class A
> struct S
{ S(A
= 0) {} };
185 template <class A
> struct T
{ template<typename B
> T(A
= 0, B
= 0) {} };
189 namespace transform_params
{
190 template<typename T
, T N
, template<T (*v
)[N
]> typename U
, T (*X
)[N
]>
192 template<typename V
, V M
, V (*Y
)[M
], template<V (*v
)[M
]> typename W
>
195 static constexpr T v
= N
;
199 template<int (*)[12]> struct Q
{};
202 static_assert(a
.v
== 12);
204 template<typename
...T
> struct B
{
205 template<T
...V
> B(const T (&...p
)[V
]) {
206 constexpr int Vs
[] = {V
...};
207 static_assert(Vs
[0] == 3 && Vs
[1] == 4 && Vs
[2] == 4);
209 static constexpr int (*p
)(T
...) = (int(*)(int, char, char))nullptr;
211 B
b({1, 2, 3}, "foo", {'x', 'y', 'z', 'w'}); // ok
213 template<typename
...T
> struct C
{
214 template<T
...V
, template<T
...> typename X
>
217 template<int...> struct Y
{};
220 template<typename
...T
> struct D
{
221 template<T
...V
> D(Y
<V
...>);
227 int arr3
[3], arr4
[4];
230 template<typename T
> struct A
{
231 template<typename
...U
> A(T
, U
...);
235 template<typename T
> struct B
{
236 template<int ...N
> B(T
, int (&...r
)[N
]);
240 template<typename T
> struct C
{
241 template<template<typename
> typename
...U
> C(T
, U
<int>...);
245 template<typename
...U
> struct X
{
246 template<typename T
> X(T
, U
...);
250 template<int ...N
> struct Y
{
251 template<typename T
> Y(T
, int (&...r
)[N
]);
255 template<template<typename
> typename
...U
> struct Z
{
256 template<typename T
> Z(T
, U
<int>...);
261 namespace tuple_tests
{
262 // The converting n-ary constructor appears viable, deducing T as an empty
263 // pack (until we check its SFINAE constraints).
265 template<class ...T
> struct tuple
{
266 template<class ...Args
> struct X
{ static const bool value
= false; };
267 template<class ...U
, bool Y
= X
<U
...>::value
> tuple(U
&&...u
);
272 // Don't get caught by surprise when X<...> doesn't even exist in the
273 // selected specialization!
275 template<class ...T
> struct tuple
{
276 template<class ...Args
> struct X
{ static const bool value
= false; };
277 // Substitution into X<U...>::value succeeds but produces the
278 // value-dependent expression
279 // tuple<T...>::X<>::value
280 // FIXME: Is that the right behavior?
281 template<class ...U
, bool Y
= X
<U
...>::value
> tuple(U
&&...u
);
283 template <> class tuple
<> {};
284 tuple a
= {1, 2, 3}; // expected-error {{excess elements in struct initializer}}
288 template<typename
...T
> struct scoped_lock
{
291 template<> struct scoped_lock
<> {};
296 namespace dependent
{
297 template<typename T
> struct X
{ // expected-note 3{{here}}
300 template<typename T
> int Var(T t
) {
302 return X(x
) + 1; // expected-error {{invalid operands}}
304 template<typename T
> int Cast(T t
) {
305 return X(X(t
)) + 1; // expected-error {{invalid operands}}
307 template<typename T
> int Cast2(T t
) {
308 return (X
)(X
)t
+ 1; // expected-error {{deduction not allowed}}
310 template<typename T
> int Cast3(T t
) {
311 return X
{X
{t
}} + 1; // expected-error {{invalid operands}}
313 template<typename T
> int Cast4(T t
) {
314 return (X
){(X
){t
}} + 1; // expected-error 2{{deduction not allowed}}
316 template<typename T
> int New(T t
) {
317 return X(new X(t
)) + 1; // expected-error {{invalid operands}}
319 template<typename T
> int *New2(T t
) {
320 return new X(X(t
)) * 2; // expected-error {{invalid operands}}
322 template int Var(float); // expected-note {{instantiation of}}
323 template int Cast(float); // expected-note {{instantiation of}}
324 template int Cast3(float); // expected-note {{instantiation of}}
325 template int New(float); // expected-note {{instantiation of}}
326 template int *New2(float); // expected-note {{instantiation of}}
327 template<typename T
> int operator+(X
<T
>, int);
328 template int Var(int);
329 template int Cast(int);
330 template int New(int);
332 template<template<typename
> typename Y
> void test() {
337 template void test
<X
>();
340 namespace injected_class_name
{
341 template<typename T
= void> struct A
{
343 template<typename U
> A(A
<U
>);
347 using T
= decltype(a
);
348 using T
= decltype(b
);
351 namespace member_guides
{
355 template <class T
> struct Bar
{
358 Bar(int) -> Bar
<int>;
363 template<typename T
> struct Public
; // expected-note {{declared public}}
364 Public(float) -> Public
<float>;
365 protected: // expected-note {{declared protected by intervening access specifier}}
366 template<typename T
> struct Protected
; // expected-note 2{{declared protected}}
367 Protected(float) -> Protected
<float>;
368 Public(int) -> Public
<int>; // expected-error {{different access}}
369 private: // expected-note {{declared private by intervening access specifier}}
370 template<typename T
> struct Private
; // expected-note {{declared private}}
371 Protected(int) -> Protected
<int>; // expected-error {{different access}}
372 public: // expected-note 2{{declared public by intervening access specifier}}
373 template<typename T
> Public(T
) -> Public
<T
>;
374 template<typename T
> Protected(T
) -> Protected
<T
>; // expected-error {{different access}}
375 template<typename T
> Private(T
) -> Private
<T
>; // expected-error {{different access}}
379 namespace rdar41903969
{
380 template <class T
> struct A
{};
381 template <class T
> struct B
;
382 template <class T
> struct C
{
387 void foo(A
<int> &a
, B
<int> &b
) {
392 template<typename T
> struct X
{
393 X(std::initializer_list
<T
>) = delete;
397 template <class T
> struct D
: X
<T
> {};
399 void bar(D
<int>& d
) {
404 namespace rdar41330135
{
405 template <int> struct A
{};
409 S(T a
, U t
, A
<sizeof(t
)>);
411 template <class T
> struct D
{
412 D(T t
, A
<sizeof(t
)>);
415 S
s(0, 0, A
<sizeof(int)>());
416 D
d(0, A
<sizeof(int)>());
419 namespace test_dupls
{
420 template<unsigned long> struct X
{};
421 template<typename T
> struct A
{
422 A(T t
, X
<sizeof(t
)>);
425 template<typename U
> struct B
{
426 B(U u
, X
<sizeof(u
)>);
433 namespace no_crash_on_default_arg
{
435 template <typename T
> class B
{
438 // This used to crash due to unparsed default arg above. The diagnostic could
439 // be improved, but the point of this test is to simply check we do not crash.
440 B(); // expected-error {{deduction guide declaration without trailing return type}}
442 } // namespace no_crash_on_default_arg
444 #pragma clang diagnostic push
445 #pragma clang diagnostic warning "-Wctad-maybe-unsupported"
446 namespace test_implicit_ctad_warning
{
452 struct NoExplicit
{ // expected-note {{add a deduction guide to suppress this warning}}
454 NoExplicit(T
, int) {}
457 // expected-warning@+1 {{'NoExplicit' may not intend to support class template argument deduction}}
463 HasExplicit(U
, int) {}
465 template <class U
> HasExplicit(U
, int) -> HasExplicit
<Tag
<U
>>;
469 // Motivating examples from (taken from Stephan Lavavej's 2018 Cppcon talk)
470 template <class T
, class U
>
471 struct AmateurPair
{ // expected-note {{add a deduction guide to suppress this warning}}
474 explicit AmateurPair(const T
&t
, const U
&u
) {}
476 // expected-warning@+1 {{'AmateurPair' may not intend to support class template argument deduction}}
477 AmateurPair
p1(42, "hello world"); // deduces to Pair<int, char[12]>
479 template <class T
, class U
>
480 struct AmateurPair2
{ // expected-note {{add a deduction guide to suppress this warning}}
483 explicit AmateurPair2(T t
, U u
) {}
485 // expected-warning@+1 {{'AmateurPair2' may not intend to support class template argument deduction}}
486 AmateurPair2
p2(42, "hello world"); // deduces to Pair2<int, const char*>
488 template <class T
, class U
>
491 explicit ProPair(T
const& t
, U
const& u
) {}
493 template<class T1
, class T2
>
494 ProPair(T1
, T2
) -> ProPair
<T1
, T2
>;
495 ProPair
p3(42, "hello world"); // deduces to ProPair<int, const char*>
496 static_assert(__is_same(decltype(p3
), ProPair
<int, const char*>));
498 // Test that user-defined explicit guides suppress the warning even if they
499 // aren't used as candidates.
501 struct TestExplicitCtor
{
502 TestExplicitCtor(T
) {}
505 explicit TestExplicitCtor(TestExplicitCtor
<T
> const&) -> TestExplicitCtor
<void>;
506 TestExplicitCtor
<int> ce1
{42};
507 TestExplicitCtor ce2
= ce1
;
508 static_assert(__is_same(decltype(ce2
), TestExplicitCtor
<int>), "");
510 struct allow_ctad_t
{
511 allow_ctad_t() = delete;
515 struct TestSuppression
{
516 TestSuppression(T
) {}
518 TestSuppression(allow_ctad_t
)->TestSuppression
<void>;
519 TestSuppression
ta("abc");
520 static_assert(__is_same(decltype(ta
), TestSuppression
<const char *>), "");
522 #pragma clang diagnostic pop
526 template <class H
, class P
> struct umm
;
528 template <class H
= int, class P
= int>
530 umm(H h
= 0, P p
= 0);
533 template <class H
, class P
> struct umm
;
544 template<typename
> class y
{ y(x
= x()); };
545 template<typename z
> y(z
)->y
<z
>;
547 // Not a constant initializer, but trivial default initialization. We won't
548 // detect this as trivial default initialization if synthesizing the implicit
549 // deduction guide 'template<typename T> y(x = x()) -> Y<T>;' leaves behind a
555 template<typename T
> struct A
{ A(T
); T x
; };
556 template<typename T
> int &&n
= A(T()).x
;
560 // Ensure we don't crash when CTAD fails.
561 template <typename T1
, typename T2
>
562 struct Foo
{ // expected-note{{candidate function template not viable}}
563 Foo(T1
, T2
); // expected-note{{candidate function template not viable}}
566 template <typename
... Args
>
567 void insert(Args
&&...args
);
570 insert(Foo(2, 2, 2)); // expected-error{{no viable constructor or deduction guide}}
575 template <class... Ts
>
576 struct overloaded
: Ts
... {
577 using Ts::operator()...;
579 template <class... Ts
>
580 overloaded(Ts
...) -> overloaded
<Ts
...>;
583 virtual void f() = 0;
587 namespace function_prototypes
{
588 template<class T
> using fptr1
= void (*) (T
);
589 template<class T
> using fptr2
= fptr1
<fptr1
<T
>>;
591 template<class T
> void foo0(fptr1
<T
>) {
592 static_assert(__is_same(T
, const char*));
594 void bar0(const char *const volatile __restrict
);
595 void t0() { foo0(&bar0
); }
597 template<class T
> void foo1(fptr1
<const T
*>) {
598 static_assert(__is_same(T
, char));
600 void bar1(const char * __restrict
);
601 void t1() { foo1(&bar1
); }
603 template<class T
> void foo2(fptr2
<const T
*>) {
604 static_assert(__is_same(T
, char));
606 void bar2(fptr1
<const char * __restrict
>);
607 void t2() { foo2(&bar2
); }
609 template<class T
> void foo3(fptr1
<const T
*>) {}
610 void bar3(char * __restrict
);
611 void t3() { foo3(&bar3
); }
612 // expected-error@-1 {{no matching function for call to 'foo3'}}
613 // expected-note@-4 {{candidate template ignored: cannot deduce a type for 'T' that would make 'const T' equal 'char'}}
615 template<class T
> void foo4(fptr2
<const T
*>) {}
616 void bar4(fptr1
<char * __restrict
>);
617 void t4() { foo4(&bar4
); }
618 // expected-error@-1 {{no matching function for call to 'foo4'}}
619 // expected-note@-4 {{candidate template ignored: cannot deduce a type for 'T' that would make 'const T' equal 'char'}}
621 template<typename T
> void foo5(T(T
)) {}
623 void t5() { foo5(bar5
); }
624 // expected-error@-1 {{no matching function for call to 'foo5'}}
625 // expected-note@-4 {{candidate template ignored: deduced conflicting types for parameter 'T' ('const int' vs. 'int')}}
628 template<typename T
> void foo6(void(*)(struct Foo6
, T
)) {}
629 void bar6(Foo6
, int);
630 void t6() { foo6(bar6
); }
634 // expected-no-diagnostics
635 namespace undefined_warnings
{
636 // Make sure we don't get an "undefined but used internal symbol" warning for the deduction guide here.
638 template <typename T
>
640 explicit TemplDObj(T func
) noexcept
{}
642 auto test1
= TemplDObj(0);
644 TemplDObj(float) -> TemplDObj
<double>;
645 auto test2
= TemplDObj(.0f
);