David Majnemer | dc9be21 | 2015-10-08 10:04:46 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -verify -std=c++11 %s |
| 2 | // RUN: %clang_cc1 -verify -std=c++11 -fdelayed-template-parsing %s |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 3 | |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 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>(); |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 15 | |
| 16 | // PR5764 |
| 17 | namespace PR5764 { |
John McCall | 3155f57 | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 18 | struct X { |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 19 | template <typename T> |
| 20 | void Bar() { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 21 | typedef T ValueType; |
John McCall | 3155f57 | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 22 | struct Y { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 23 | Y() { V = ValueType(); } |
| 24 | |
| 25 | ValueType V; |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | Y y; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | void test(X x) { |
| 33 | x.Bar<int>(); |
| 34 | } |
| 35 | } |
| 36 | |
Chandler Carruth | 3e0c140 | 2010-02-15 22:12:26 +0000 | [diff] [blame] | 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 | } |
Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 55 | |
| 56 | namespace PR8801 { |
| 57 | template<typename T> |
| 58 | void foo() { |
| 59 | class X; |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 60 | typedef int (X::*pmf_type)(); |
Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 61 | class X : public T { }; |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 62 | |
| 63 | pmf_type pmf = &T::foo; |
Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 66 | struct Y { int foo(); }; |
Douglas Gregor | e9fc8dc | 2010-12-21 21:22:51 +0000 | [diff] [blame] | 67 | |
| 68 | template void foo<Y>(); |
| 69 | } |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 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 | } |
David Majnemer | fd6c685f | 2013-11-27 22:57:44 +0000 | [diff] [blame] | 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()); |
Kaelyn Uhrain | 297a138 | 2013-11-28 00:13:38 +0000 | [diff] [blame] | 182 | return 0; |
David Majnemer | fd6c685f | 2013-11-27 22:57:44 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
David Majnemer | a64cb5a | 2014-02-22 00:17:46 +0000 | [diff] [blame] | 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 | } |
Serge Pavlov | 907233f | 2015-04-28 17:58:47 +0000 | [diff] [blame] | 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 | } |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 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 | |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 228 | template<typename T> void f1a() { |
| 229 | void g1(union x1); |
| 230 | union x1 {}; |
| 231 | } |
| 232 | template void f1a<int>(); |
| 233 | |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 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 | void g3(enum class x3); |
| 242 | enum class x3 { nothing }; |
| 243 | } |
| 244 | template void f3<int>(); |
| 245 | |
| 246 | |
| 247 | template<typename T> void f4() { |
| 248 | void g4(struct x4 {} x); // expected-error{{'x4' cannot be defined in a parameter type}} |
| 249 | } |
| 250 | template void f4<int>(); |
| 251 | |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 252 | template<typename T> void f4a() { |
| 253 | void g4(union x4 {} x); // expected-error{{'x4' cannot be defined in a parameter type}} |
| 254 | } |
| 255 | template void f4a<int>(); |
| 256 | |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 257 | |
| 258 | template <class T> void f(); |
| 259 | template <class T> struct S1 { |
| 260 | void m() { |
| 261 | f<class newclass>(); |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 262 | f<union newunion>(); |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 263 | } |
| 264 | }; |
| 265 | template struct S1<int>; |
| 266 | |
| 267 | template <class T> struct S2 { |
| 268 | void m() { |
| 269 | f<enum new_enum>(); // expected-error{{ISO C++ forbids forward references to 'enum' types}} |
| 270 | } |
| 271 | }; |
| 272 | template struct S2<int>; |
| 273 | |
| 274 | template <class T> struct S3 { |
| 275 | void m() { |
| 276 | f<enum class new_enum>(); |
| 277 | } |
| 278 | }; |
| 279 | template struct S3<int>; |
| 280 | |
| 281 | template <class T> struct S4 { |
| 282 | struct local {}; |
| 283 | void m() { |
| 284 | f<local>(); |
| 285 | } |
| 286 | }; |
| 287 | template struct S4<int>; |
| 288 | |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 289 | template <class T> struct S4a { |
| 290 | union local {}; |
| 291 | void m() { |
| 292 | f<local>(); |
| 293 | } |
| 294 | }; |
| 295 | template struct S4a<int>; |
| 296 | |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 297 | template <class T> struct S5 { |
| 298 | enum local { nothing }; |
| 299 | void m() { |
| 300 | f<local>(); |
| 301 | } |
| 302 | }; |
| 303 | template struct S5<int>; |
| 304 | |
| 305 | template <class T> struct S7 { |
| 306 | enum class local { nothing }; |
| 307 | void m() { |
| 308 | f<local>(); |
| 309 | } |
| 310 | }; |
| 311 | template struct S7<int>; |
| 312 | |
| 313 | |
| 314 | template <class T> void fff(T *x); |
| 315 | template <class T> struct S01 { |
| 316 | struct local { }; |
| 317 | void m() { |
| 318 | local x; |
| 319 | fff(&x); |
| 320 | } |
| 321 | }; |
| 322 | template struct S01<int>; |
| 323 | |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 324 | template <class T> struct S01a { |
| 325 | union local { }; |
| 326 | void m() { |
| 327 | local x; |
| 328 | fff(&x); |
| 329 | } |
| 330 | }; |
| 331 | template struct S01a<int>; |
| 332 | |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 333 | template <class T> struct S02 { |
| 334 | enum local { nothing }; |
| 335 | void m() { |
| 336 | local x; |
| 337 | fff(&x); |
| 338 | } |
| 339 | }; |
| 340 | template struct S02<int>; |
| 341 | |
| 342 | template <class T> struct S03 { |
| 343 | enum class local { nothing }; |
| 344 | void m() { |
| 345 | local x; |
| 346 | fff(&x); |
| 347 | } |
| 348 | }; |
| 349 | template struct S03<int>; |
| 350 | |
| 351 | |
| 352 | template <class T> struct S04 { |
| 353 | void m() { |
| 354 | struct { } x; |
| 355 | fff(&x); |
| 356 | } |
| 357 | }; |
| 358 | template struct S04<int>; |
| 359 | |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 360 | template <class T> struct S04a { |
| 361 | void m() { |
| 362 | union { } x; |
| 363 | fff(&x); |
| 364 | } |
| 365 | }; |
| 366 | template struct S04a<int>; |
| 367 | |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 368 | template <class T> struct S05 { |
| 369 | void m() { |
| 370 | enum { nothing } x; |
| 371 | fff(&x); |
| 372 | } |
| 373 | }; |
| 374 | template struct S05<int>; |
| 375 | |
| 376 | template <class T> struct S06 { |
| 377 | void m() { |
| 378 | class { virtual void mmm() {} } x; |
| 379 | fff(&x); |
| 380 | } |
| 381 | }; |
| 382 | template struct S06<int>; |
| 383 | } |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 384 | |
| 385 | namespace PR20625 { |
| 386 | template <typename T> |
| 387 | void f() { |
| 388 | struct N { |
| 389 | static constexpr int get() { return 42; } |
| 390 | }; |
| 391 | constexpr int n = N::get(); |
| 392 | static_assert(n == 42, "n == 42"); |
| 393 | } |
| 394 | |
| 395 | void g() { f<void>(); } |
| 396 | } |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 397 | |
| 398 | |
| 399 | namespace PR21332 { |
| 400 | template<typename T> void f1() { |
| 401 | struct S { // expected-note{{in instantiation of member class 'S' requested here}} |
| 402 | void g1(int n = T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} |
| 403 | }; |
| 404 | } |
| 405 | template void f1<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f1<int>' requested here}} |
| 406 | |
| 407 | template<typename T> void f2() { |
| 408 | struct S { // expected-note{{in instantiation of member class 'S' requested here}} |
| 409 | void g2() noexcept(T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} |
| 410 | }; |
| 411 | } |
| 412 | template void f2<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f2<int>' requested here}} |
| 413 | |
| 414 | template<typename T> void f3() { |
| 415 | enum S { |
| 416 | val = T::error; // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}} |
| 417 | }; |
| 418 | } |
| 419 | template void f3<int>(); //expected-note{{in instantiation of function template specialization 'PR21332::f3<int>' requested here}} |
| 420 | |
| 421 | template<typename T> void f4() { |
| 422 | enum class S { |
| 423 | val = T::error; // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}} |
| 424 | }; |
| 425 | } |
| 426 | template void f4<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f4<int>' requested here}} |
| 427 | |
| 428 | template<typename T> void f5() { |
| 429 | class S { // expected-note {{in instantiation of default member initializer 'PR21332::f5()::S::val' requested here}} |
| 430 | int val = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} |
| 431 | }; |
| 432 | } |
| 433 | template void f5<int>(); // expected-note {{in instantiation of function template specialization 'PR21332::f5<int>' requested here}} |
| 434 | |
| 435 | template<typename T> void f6() { |
| 436 | class S { // expected-note {{in instantiation of member function 'PR21332::f6()::S::get' requested here}} |
| 437 | void get() { |
| 438 | class S2 { // expected-note {{in instantiation of member class 'S2' requested here}} |
| 439 | void g1(int n = T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} |
| 440 | }; |
| 441 | } |
| 442 | }; |
| 443 | } |
| 444 | template void f6<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f6<int>' requested here}} |
| 445 | |
| 446 | template<typename T> void f7() { |
| 447 | struct S { void g() noexcept(undefined_val); }; // expected-error{{use of undeclared identifier 'undefined_val'}} |
| 448 | } |
| 449 | template void f7<int>(); |
| 450 | } |
John McCall | dc40b61 | 2015-12-11 01:56:36 +0000 | [diff] [blame] | 451 | |
| 452 | // rdar://23721638: Ensure that we correctly perform implicit |
| 453 | // conversions when instantiating the default arguments of local functions. |
| 454 | namespace rdar23721638 { |
| 455 | struct A { |
| 456 | A(const char *) = delete; // expected-note 2 {{explicitly marked deleted here}} |
| 457 | }; |
| 458 | |
| 459 | template <typename T> void foo() { |
| 460 | struct Inner { // expected-note {{in instantiation}} |
| 461 | void operator()(T a = "") {} // expected-error {{conversion function from 'const char [1]' to 'rdar23721638::A' invokes a deleted function}} |
| 462 | // expected-note@-1 {{passing argument to parameter 'a' here}} |
| 463 | // expected-note@-2 {{candidate function not viable}} |
| 464 | }; |
| 465 | Inner()(); // expected-error {{no matching function}} |
| 466 | } |
| 467 | template void foo<A>(); // expected-note 2 {{in instantiation}} |
| 468 | |
| 469 | template <typename T> void bar() { |
| 470 | auto lambda = [](T a = "") {}; // expected-error {{conversion function from 'const char [1]' to 'rdar23721638::A' invokes a deleted function}} |
| 471 | // expected-note@-1 {{passing argument to parameter 'a' here}} |
| 472 | // expected-note@-2 {{candidate function not viable}} |
| 473 | // expected-note@-3 {{conversion candidate of type}} |
| 474 | lambda(); // expected-error {{no matching function}} |
| 475 | } |
| 476 | template void bar<A>(); // expected-note {{in instantiation}} |
| 477 | } |