1 // RUN: %clang_cc1 -verify -std=c++11 %s 2 // RUN: %clang_cc1 -verify -std=c++11 -fdelayed-template-parsing %s 3 4 template<typename T> 5 void f0() { 6 struct X; 7 typedef struct Y { 8 T (X::* f1())(int) { return 0; } 9 } Y2; 10 11 Y2 y = Y(); 12 } 13 14 template void f0<int>(); 15 16 // PR5764 17 namespace PR5764 { 18 struct X { 19 template <typename T> 20 void Bar() { 21 typedef T ValueType; 22 struct Y { 23 Y() { V = ValueType(); } 24 25 ValueType V; 26 }; 27 28 Y y; 29 } 30 }; 31 32 void test(X x) { 33 x.Bar<int>(); 34 } 35 } 36 37 // Instantiation of local classes with virtual functions. 38 namespace local_class_with_virtual_functions { 39 template <typename T> struct X { }; 40 template <typename T> struct Y { }; 41 42 template <typename T> 43 void f() { 44 struct Z : public X<Y<T>*> { 45 virtual void g(Y<T>* y) { } 46 void g2(int x) {(void)x;} 47 }; 48 Z z; 49 (void)z; 50 } 51 52 struct S { }; 53 void test() { f<S>(); } 54 } 55 56 namespace PR8801 { 57 template<typename T> 58 void foo() { 59 class X; 60 typedef int (X::*pmf_type)(); 61 class X : public T { }; 62 63 pmf_type pmf = &T::foo; 64 } 65 66 struct Y { int foo(); }; 67 68 template void foo<Y>(); 69 } 70 71 namespace TemplatePacksAndLambdas { 72 template <typename ...T> int g(T...); 73 struct S { 74 template <typename ...T> static void f(int f = g([]{ static T t; return ++t; }()...)) {} 75 }; 76 void h() { S::f<int, int, int>(); } 77 } 78 79 namespace PR9685 { 80 template <class Thing> void forEach(Thing t) { t.func(); } 81 82 template <typename T> void doIt() { 83 struct Functor { 84 void func() { (void)i; } 85 int i; 86 }; 87 88 forEach(Functor()); 89 } 90 91 void call() { 92 doIt<int>(); 93 } 94 } 95 96 namespace PR12702 { 97 struct S { 98 template <typename F> bool apply(F f) { return f(); } 99 }; 100 101 template <typename> struct T { 102 void foo() { 103 struct F { 104 int x; 105 106 bool operator()() { return x == 0; } 107 }; 108 109 S().apply(F()); 110 } 111 }; 112 113 void call() { T<int>().foo(); } 114 } 115 116 namespace PR17139 { 117 template <class T> void foo(const T &t) { t.foo(); } 118 119 template <class F> void bar(F *f) { 120 struct B { 121 F *fn; 122 void foo() const { fn(); } 123 } b = { f }; 124 foo(b); 125 } 126 127 void go() {} 128 129 void test() { bar(go); } 130 } 131 132 namespace PR17740 { 133 class C { 134 public: 135 template <typename T> static void foo(T function); 136 template <typename T> static void bar(T function); 137 template <typename T> static void func(T function); 138 }; 139 140 template <typename T> void C::foo(T function) { function(); } 141 142 template <typename T> void C::bar(T function) { 143 foo([&function]() { function(); }); 144 } 145 146 template <typename T> void C::func(T function) { 147 struct Struct { 148 T mFunction; 149 150 Struct(T function) : mFunction(function) {}; 151 152 void operator()() { 153 mFunction(); 154 }; 155 }; 156 157 bar(Struct(function)); 158 } 159 160 void call() { 161 C::func([]() {}); 162 } 163 } 164 165 namespace PR14373 { 166 struct function { 167 template <typename _Functor> function(_Functor __f) { __f(); } 168 }; 169 template <typename Func> function exec_func(Func f) { 170 struct functor { 171 functor(Func f) : func(f) {} 172 void operator()() const { func(); } 173 Func func; 174 }; 175 return functor(f); 176 } 177 struct Type { 178 void operator()() const {} 179 }; 180 int call() { 181 exec_func(Type()); 182 return 0; 183 } 184 } 185 186 namespace PR18907 { 187 template <typename> 188 class C : public C<int> {}; // expected-error{{within its own definition}} 189 190 template <typename X> 191 void F() { 192 struct A : C<X> {}; 193 } 194 195 struct B { 196 void f() { F<int>(); } 197 }; 198 } 199 200 namespace PR23194 { 201 struct X { 202 int operator()() const { return 0; } 203 }; 204 struct Y { 205 Y(int) {} 206 }; 207 template <bool = true> int make_seed_pair() noexcept { 208 struct state_t { 209 X x; 210 Y y{x()}; 211 }; 212 return 0; 213 } 214 int func() { 215 return make_seed_pair(); 216 } 217 } 218 219 namespace PR18653 { 220 // Forward declarations 221 222 template<typename T> void f1() { 223 void g1(struct x1); 224 struct x1 {}; 225 } 226 template void f1<int>(); 227 228 template<typename T> void f1a() { 229 void g1(union x1); 230 union x1 {}; 231 } 232 template void f1a<int>(); 233 234 template<typename T> void f2() { 235 void g2(enum x2); // expected-error{{ISO C++ forbids forward references to 'enum' types}} 236 enum x2 { nothing }; 237 } 238 template void f2<int>(); 239 240 template<typename T> void f3() { 241 enum class x3; 242 void g3(enum x3); 243 enum class x3 { nothing }; 244 } 245 template void f3<int>(); 246 247 248 template<typename T> void f4() { 249 void g4(struct x4 {} x); // expected-error{{'x4' cannot be defined in a parameter type}} 250 } 251 template void f4<int>(); 252 253 template<typename T> void f4a() { 254 void g4(union x4 {} x); // expected-error{{'x4' cannot be defined in a parameter type}} 255 } 256 template void f4a<int>(); 257 258 259 template <class T> void f(); 260 template <class T> struct S1 { 261 void m() { 262 f<class newclass>(); 263 f<union newunion>(); 264 } 265 }; 266 template struct S1<int>; 267 268 template <class T> struct S2 { 269 void m() { 270 f<enum new_enum>(); // expected-error{{ISO C++ forbids forward references to 'enum' types}} 271 } 272 }; 273 template struct S2<int>; 274 275 template <class T> struct S3 { 276 void m() { 277 enum class new_enum; 278 f<enum new_enum>(); 279 } 280 }; 281 template struct S3<int>; 282 283 template <class T> struct S4 { 284 struct local {}; 285 void m() { 286 f<local>(); 287 } 288 }; 289 template struct S4<int>; 290 291 template <class T> struct S4a { 292 union local {}; 293 void m() { 294 f<local>(); 295 } 296 }; 297 template struct S4a<int>; 298 299 template <class T> struct S5 { 300 enum local { nothing }; 301 void m() { 302 f<local>(); 303 } 304 }; 305 template struct S5<int>; 306 307 template <class T> struct S7 { 308 enum class local { nothing }; 309 void m() { 310 f<local>(); 311 } 312 }; 313 template struct S7<int>; 314 315 316 template <class T> void fff(T *x); 317 template <class T> struct S01 { 318 struct local { }; 319 void m() { 320 local x; 321 fff(&x); 322 } 323 }; 324 template struct S01<int>; 325 326 template <class T> struct S01a { 327 union local { }; 328 void m() { 329 local x; 330 fff(&x); 331 } 332 }; 333 template struct S01a<int>; 334 335 template <class T> struct S02 { 336 enum local { nothing }; 337 void m() { 338 local x; 339 fff(&x); 340 } 341 }; 342 template struct S02<int>; 343 344 template <class T> struct S03 { 345 enum class local { nothing }; 346 void m() { 347 local x; 348 fff(&x); 349 } 350 }; 351 template struct S03<int>; 352 353 354 template <class T> struct S04 { 355 void m() { 356 struct { } x; 357 fff(&x); 358 } 359 }; 360 template struct S04<int>; 361 362 template <class T> struct S04a { 363 void m() { 364 union { } x; 365 fff(&x); 366 } 367 }; 368 template struct S04a<int>; 369 370 template <class T> struct S05 { 371 void m() { 372 enum { nothing } x; 373 fff(&x); 374 } 375 }; 376 template struct S05<int>; 377 378 template <class T> struct S06 { 379 void m() { 380 class { virtual void mmm() {} } x; 381 fff(&x); 382 } 383 }; 384 template struct S06<int>; 385 } 386 387 namespace PR20625 { 388 template <typename T> 389 void f() { 390 struct N { 391 static constexpr int get() { return 42; } 392 }; 393 constexpr int n = N::get(); 394 static_assert(n == 42, "n == 42"); 395 } 396 397 void g() { f<void>(); } 398 } 399 400 401 namespace PR21332 { 402 template<typename T> void f1() { 403 struct S { // expected-note{{in instantiation of member class 'S' requested here}} 404 void g1(int n = T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} 405 }; 406 } 407 template void f1<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f1<int>' requested here}} 408 409 template<typename T> void f2() { 410 struct S { // expected-note{{in instantiation of member class 'S' requested here}} 411 void g2() noexcept(T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} 412 }; 413 } 414 template void f2<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f2<int>' requested here}} 415 416 template<typename T> void f3() { 417 enum S { 418 val = T::error; // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}} 419 }; 420 } 421 template void f3<int>(); //expected-note{{in instantiation of function template specialization 'PR21332::f3<int>' requested here}} 422 423 template<typename T> void f4() { 424 enum class S { 425 val = T::error; // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}} 426 }; 427 } 428 template void f4<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f4<int>' requested here}} 429 430 template<typename T> void f5() { 431 class S { // expected-note {{in instantiation of default member initializer 'PR21332::f5()::S::val' requested here}} 432 int val = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} 433 }; 434 } 435 template void f5<int>(); // expected-note {{in instantiation of function template specialization 'PR21332::f5<int>' requested here}} 436 437 template<typename T> void f6() { 438 class S { // expected-note {{in instantiation of member function 'PR21332::f6()::S::get' requested here}} 439 void get() { 440 class S2 { // expected-note {{in instantiation of member class 'S2' requested here}} 441 void g1(int n = T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} 442 }; 443 } 444 }; 445 } 446 template void f6<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f6<int>' requested here}} 447 448 template<typename T> void f7() { 449 struct S { void g() noexcept(undefined_val); }; // expected-error{{use of undeclared identifier 'undefined_val'}} 450 } 451 template void f7<int>(); 452 } 453 454 // rdar://23721638: Ensure that we correctly perform implicit 455 // conversions when instantiating the default arguments of local functions. 456 namespace rdar23721638 { 457 struct A { 458 A(const char *) = delete; // expected-note 2 {{explicitly marked deleted here}} 459 }; 460 461 template <typename T> void foo() { 462 struct Inner { // expected-note {{in instantiation}} 463 void operator()(T a = "") {} // expected-error {{conversion function from 'const char [1]' to 'rdar23721638::A' invokes a deleted function}} 464 // expected-note@-1 {{passing argument to parameter 'a' here}} 465 }; 466 Inner()(); // expected-error {{type 'Inner' does not provide a call operator}} 467 } 468 template void foo<A>(); // expected-note 2 {{in instantiation}} 469 470 template <typename T> void bar() { 471 auto lambda = [](T a = "") {}; // expected-error {{conversion function from 'const char [1]' to 'rdar23721638::A' invokes a deleted function}} 472 // expected-note@-1 {{passing argument to parameter 'a' here}} 473 lambda(); 474 } 475 template void bar<A>(); // expected-note {{in instantiation}} 476 } 477 478 namespace anon_union_default_member_init { 479 template<typename T> void f() { 480 struct S { 481 union { 482 int i = 0; 483 }; 484 }; 485 } 486 void g() { f<int>(); } 487 } 488 489 namespace PR45000 { 490 template <typename T> 491 void f(int x = [](T x = nullptr) -> int { return x; }()); 492 // expected-error@-1 {{cannot initialize a parameter of type 'int' with an rvalue of type 'nullptr_t'}} 493 // expected-note@-2 {{passing argument to parameter 'x' here}} 494 495 void g() { f<int>(); } 496 // expected-note@-1 {{in instantiation of default function argument expression for 'f<int>' required here}} 497 } 498 499 namespace LambdaInDefaultMemberInitializer { 500 template<typename T> void f() { 501 struct S { 502 void *p = [this] { return &p; }(); 503 }; 504 } 505 template void f<int>(); 506 } 507