1 // RUN: %clang_cc1 -verify -std=c++11 %s
2 // RUN: %clang_cc1 -verify -std=c++11 -fdelayed-template-parsing %s
3 // RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
9 T (X::* f1())(int) { return 0; }
15 template void f0
<int>();
24 Y() { V
= ValueType(); }
38 // Instantiation of local classes with virtual functions.
39 namespace local_class_with_virtual_functions
{
40 template <typename T
> struct X
{ };
41 template <typename T
> struct Y
{ };
45 struct Z
: public X
<Y
<T
>*> {
46 virtual void g(Y
<T
>* y
) { }
47 void g2(int x
) {(void)x
;}
54 void test() { f
<S
>(); }
61 typedef int (X::*pmf_type
)();
62 class X
: public T
{ };
64 pmf_type pmf
= &T::foo
;
67 struct Y
{ int foo(); };
69 template void foo
<Y
>();
72 namespace TemplatePacksAndLambdas
{
73 template <typename
...T
> int g(T
...);
75 template <typename
...T
> static void f(int f
= g([]{ static T t
; return ++t
; }()...)) {}
77 void h() { S::f
<int, int, int>(); }
81 template <class Thing
> void forEach(Thing t
) { t
.func(); }
83 template <typename T
> void doIt() {
85 void func() { (void)i
; }
99 template <typename F
> bool apply(F f
) { return f(); }
102 template <typename
> struct T
{
107 bool operator()() { return x
== 0; }
114 void call() { T
<int>().foo(); }
118 template <class T
> void foo(const T
&t
) { t
.foo(); }
120 template <class F
> void bar(F
*f
) {
123 void foo() const { fn(); }
130 void test() { bar(go
); }
136 template <typename T
> static void foo(T function
);
137 template <typename T
> static void bar(T function
);
138 template <typename T
> static void func(T function
);
141 template <typename T
> void C::foo(T function
) { function(); }
143 template <typename T
> void C::bar(T function
) {
144 foo([&function
]() { function(); });
147 template <typename T
> void C::func(T function
) {
151 Struct(T function
) : mFunction(function
) {};
158 bar(Struct(function
));
168 template <typename _Functor
> function(_Functor __f
) { __f(); }
170 template <typename Func
> function
exec_func(Func f
) {
172 functor(Func f
) : func(f
) {}
173 void operator()() const { func(); }
179 void operator()() const {}
189 class C
: public C
<int> {}; // expected-error{{within its own definition}}
191 template <typename X
>
197 void f() { F
<int>(); }
203 int operator()() const { return 0; }
208 template <bool = true> int make_seed_pair() noexcept
{
216 return make_seed_pair();
221 // Forward declarations
223 template<typename T
> void f1() {
227 template void f1
<int>();
229 template<typename T
> void f1a() {
233 template void f1a
<int>();
235 template<typename T
> void f2() {
236 void g2(enum x2
); // expected-error{{ISO C++ forbids forward references to 'enum' types}}
239 template void f2
<int>();
241 template<typename T
> void f3() {
244 enum class x3
{ nothing
};
246 template void f3
<int>();
249 template<typename T
> void f4() {
250 void g4(struct x4
{} x
); // expected-error{{'x4' cannot be defined in a parameter type}}
252 template void f4
<int>();
254 template<typename T
> void f4a() {
255 void g4(union x4
{} x
); // expected-error{{'x4' cannot be defined in a parameter type}}
257 template void f4a
<int>();
260 template <class T
> void f();
261 template <class T
> struct S1
{
267 template struct S1
<int>;
269 template <class T
> struct S2
{
271 f
<enum new_enum
>(); // expected-error{{ISO C++ forbids forward references to 'enum' types}}
274 template struct S2
<int>;
276 template <class T
> struct S3
{
282 template struct S3
<int>;
284 template <class T
> struct S4
{
290 template struct S4
<int>;
292 template <class T
> struct S4a
{
298 template struct S4a
<int>;
300 template <class T
> struct S5
{
301 enum local
{ nothing
};
306 template struct S5
<int>;
308 template <class T
> struct S7
{
309 enum class local
{ nothing
};
314 template struct S7
<int>;
317 template <class T
> void fff(T
*x
);
318 template <class T
> struct S01
{
325 template struct S01
<int>;
327 template <class T
> struct S01a
{
334 template struct S01a
<int>;
336 template <class T
> struct S02
{
337 enum local
{ nothing
};
343 template struct S02
<int>;
345 template <class T
> struct S03
{
346 enum class local
{ nothing
};
352 template struct S03
<int>;
355 template <class T
> struct S04
{
361 template struct S04
<int>;
363 template <class T
> struct S04a
{
369 template struct S04a
<int>;
371 template <class T
> struct S05
{
377 template struct S05
<int>;
379 template <class T
> struct S06
{
381 class { virtual void mmm() {} } x
;
385 template struct S06
<int>;
389 template <typename T
>
392 static constexpr int get() { return 42; }
394 constexpr int n
= N::get();
395 static_assert(n
== 42, "n == 42");
398 void g() { f
<void>(); }
403 template<typename T
> void f1() {
404 struct S
{ // expected-note{{in instantiation of member class 'S' requested here}}
405 void g1(int n
= T::error
); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} \
406 // expected-note {{in instantiation of default function argument expression for 'g1<int>' required here}}
409 template void f1
<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f1<int>' requested here}}
411 template<typename T
> void f2() {
412 struct S
{ // expected-note{{in instantiation of member class 'S' requested here}}
413 void g2() noexcept(T::error
); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
416 template void f2
<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f2<int>' requested here}}
418 template<typename T
> void f3() {
420 val
= T::error
; // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
423 template void f3
<int>(); //expected-note{{in instantiation of function template specialization 'PR21332::f3<int>' requested here}}
425 template<typename T
> void f4() {
427 val
= T::error
; // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
430 template void f4
<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f4<int>' requested here}}
432 template<typename T
> void f5() {
433 class S
{ // expected-note {{in instantiation of default member initializer 'PR21332::f5()::S::val' requested here}}
434 int val
= T::error
; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
437 template void f5
<int>(); // expected-note {{in instantiation of function template specialization 'PR21332::f5<int>' requested here}}
439 template<typename T
> void f6() {
440 class S
{ // expected-note {{in instantiation of member function 'PR21332::f6()::S::get' requested here}}
442 class S2
{ // expected-note {{in instantiation of member class 'S2' requested here}}
443 void g1(int n
= T::error
); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} \
444 // expected-note {{in instantiation of default function argument expression for 'g1<int>' required here}}
449 template void f6
<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f6<int>' requested here}}
451 template<typename T
> void f7() {
452 struct S
{ void g() noexcept(undefined_val
); }; // expected-error{{use of undeclared identifier 'undefined_val'}}
454 template void f7
<int>();
457 // Ensure that we correctly perform implicit conversions when instantiating the
458 // default arguments of local functions.
459 namespace rdar23721638
{
461 A(const char *) = delete; // expected-note 2 {{explicitly marked deleted here}}
464 template <typename T
> void foo() {
465 struct Inner
{ // expected-note {{in instantiation}}
466 void operator()(T a
= "") {} // expected-error {{conversion function from 'const char[1]' to 'rdar23721638::A' invokes a deleted function}} \
467 // expected-note {{in instantiation of default function argument expression for 'operator()<rdar23721638::A>' required here}} \
468 // expected-note {{passing argument to parameter 'a' here}}
472 template void foo
<A
>(); // expected-note {{in instantiation}}
474 template <typename T
> void bar() {
475 auto lambda
= [](T a
= "") {}; // expected-error {{conversion function from 'const char[1]' to 'rdar23721638::A' invokes a deleted function}} \
476 // expected-note {{in instantiation of default function argument expression for 'operator()<rdar23721638::A>' required here}} \
477 // expected-note {{passing argument to parameter 'a' here}}
480 template void bar
<A
>(); // expected-note {{in instantiation}}
483 namespace anon_union_default_member_init
{
484 template<typename T
> void f() {
491 void g() { f
<int>(); }
495 template <typename T
>
496 void f(int x
= [](T x
= nullptr) -> int { return x
; }());
497 // expected-error@-1 {{cannot initialize a parameter of type 'int' with an rvalue of type 'std::nullptr_t'}}
498 // expected-note@-2 {{in instantiation of default function argument expression for 'operator()<int>' required here}}
499 // expected-note@-3 {{passing argument to parameter 'x' here}}
501 void g() { f
<int>(); }
502 // expected-note@-1 {{in instantiation of default function argument expression for 'f<int>' required here}}
505 namespace LambdaInDefaultMemberInitializer
{
506 template<typename T
> void f() {
508 void *p
= [this] { return &p
; }();
511 template void f
<int>();
514 #if __cplusplus >= 201703L
516 // Reduced from https://github.com/llvm/llvm-project/issues/98526
517 // This relies on the deferral instantiation of the local lambda, otherwise we would fail in DeduceReturnType().
518 namespace local_recursive_lambda
{
520 template <typename F
> struct recursive_lambda
{
521 template <typename
... Args
> auto operator()(Args
&&...args
) const {
522 return fn(*this, args
...);
527 template <typename F
> recursive_lambda(F
) -> recursive_lambda
<F
>;
530 recursive_lambda
{[&](auto &self_fn
, int) -> int {
535 } // namespace local_recursive_lambda