instantiate-local-class.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // RUN: %clang_cc1 -verify -std=c++11 %s
  2. // RUN: %clang_cc1 -verify -std=c++11 -fdelayed-template-parsing %s
  3. template<typename T>
  4. void f0() {
  5. struct X;
  6. typedef struct Y {
  7. T (X::* f1())(int) { return 0; }
  8. } Y2;
  9. Y2 y = Y();
  10. }
  11. template void f0<int>();
  12. // PR5764
  13. namespace PR5764 {
  14. struct X {
  15. template <typename T>
  16. void Bar() {
  17. typedef T ValueType;
  18. struct Y {
  19. Y() { V = ValueType(); }
  20. ValueType V;
  21. };
  22. Y y;
  23. }
  24. };
  25. void test(X x) {
  26. x.Bar<int>();
  27. }
  28. }
  29. // Instantiation of local classes with virtual functions.
  30. namespace local_class_with_virtual_functions {
  31. template <typename T> struct X { };
  32. template <typename T> struct Y { };
  33. template <typename T>
  34. void f() {
  35. struct Z : public X<Y<T>*> {
  36. virtual void g(Y<T>* y) { }
  37. void g2(int x) {(void)x;}
  38. };
  39. Z z;
  40. (void)z;
  41. }
  42. struct S { };
  43. void test() { f<S>(); }
  44. }
  45. namespace PR8801 {
  46. template<typename T>
  47. void foo() {
  48. class X;
  49. typedef int (X::*pmf_type)();
  50. class X : public T { };
  51. pmf_type pmf = &T::foo;
  52. }
  53. struct Y { int foo(); };
  54. template void foo<Y>();
  55. }
  56. namespace TemplatePacksAndLambdas {
  57. template <typename ...T> int g(T...);
  58. struct S {
  59. template <typename ...T> static void f(int f = g([]{ static T t; return ++t; }()...)) {}
  60. };
  61. void h() { S::f<int, int, int>(); }
  62. }
  63. namespace PR9685 {
  64. template <class Thing> void forEach(Thing t) { t.func(); }
  65. template <typename T> void doIt() {
  66. struct Functor {
  67. void func() { (void)i; }
  68. int i;
  69. };
  70. forEach(Functor());
  71. }
  72. void call() {
  73. doIt<int>();
  74. }
  75. }
  76. namespace PR12702 {
  77. struct S {
  78. template <typename F> bool apply(F f) { return f(); }
  79. };
  80. template <typename> struct T {
  81. void foo() {
  82. struct F {
  83. int x;
  84. bool operator()() { return x == 0; }
  85. };
  86. S().apply(F());
  87. }
  88. };
  89. void call() { T<int>().foo(); }
  90. }
  91. namespace PR17139 {
  92. template <class T> void foo(const T &t) { t.foo(); }
  93. template <class F> void bar(F *f) {
  94. struct B {
  95. F *fn;
  96. void foo() const { fn(); }
  97. } b = { f };
  98. foo(b);
  99. }
  100. void go() {}
  101. void test() { bar(go); }
  102. }
  103. namespace PR17740 {
  104. class C {
  105. public:
  106. template <typename T> static void foo(T function);
  107. template <typename T> static void bar(T function);
  108. template <typename T> static void func(T function);
  109. };
  110. template <typename T> void C::foo(T function) { function(); }
  111. template <typename T> void C::bar(T function) {
  112. foo([&function]() { function(); });
  113. }
  114. template <typename T> void C::func(T function) {
  115. struct Struct {
  116. T mFunction;
  117. Struct(T function) : mFunction(function) {};
  118. void operator()() {
  119. mFunction();
  120. };
  121. };
  122. bar(Struct(function));
  123. }
  124. void call() {
  125. C::func([]() {});
  126. }
  127. }
  128. namespace PR14373 {
  129. struct function {
  130. template <typename _Functor> function(_Functor __f) { __f(); }
  131. };
  132. template <typename Func> function exec_func(Func f) {
  133. struct functor {
  134. functor(Func f) : func(f) {}
  135. void operator()() const { func(); }
  136. Func func;
  137. };
  138. return functor(f);
  139. }
  140. struct Type {
  141. void operator()() const {}
  142. };
  143. int call() {
  144. exec_func(Type());
  145. return 0;
  146. }
  147. }
  148. namespace PR18907 {
  149. template <typename>
  150. class C : public C<int> {}; // expected-error{{within its own definition}}
  151. template <typename X>
  152. void F() {
  153. struct A : C<X> {};
  154. }
  155. struct B {
  156. void f() { F<int>(); }
  157. };
  158. }
  159. namespace PR23194 {
  160. struct X {
  161. int operator()() const { return 0; }
  162. };
  163. struct Y {
  164. Y(int) {}
  165. };
  166. template <bool = true> int make_seed_pair() noexcept {
  167. struct state_t {
  168. X x;
  169. Y y{x()};
  170. };
  171. return 0;
  172. }
  173. int func() {
  174. return make_seed_pair();
  175. }
  176. }
  177. namespace PR18653 {
  178. // Forward declarations
  179. template<typename T> void f1() {
  180. void g1(struct x1);
  181. struct x1 {};
  182. }
  183. template void f1<int>();
  184. template<typename T> void f1a() {
  185. void g1(union x1);
  186. union x1 {};
  187. }
  188. template void f1a<int>();
  189. template<typename T> void f2() {
  190. void g2(enum x2); // expected-error{{ISO C++ forbids forward references to 'enum' types}}
  191. enum x2 { nothing };
  192. }
  193. template void f2<int>();
  194. template<typename T> void f3() {
  195. void g3(enum class x3);
  196. enum class x3 { nothing };
  197. }
  198. template void f3<int>();
  199. template<typename T> void f4() {
  200. void g4(struct x4 {} x); // expected-error{{'x4' cannot be defined in a parameter type}}
  201. }
  202. template void f4<int>();
  203. template<typename T> void f4a() {
  204. void g4(union x4 {} x); // expected-error{{'x4' cannot be defined in a parameter type}}
  205. }
  206. template void f4a<int>();
  207. template <class T> void f();
  208. template <class T> struct S1 {
  209. void m() {
  210. f<class newclass>();
  211. f<union newunion>();
  212. }
  213. };
  214. template struct S1<int>;
  215. template <class T> struct S2 {
  216. void m() {
  217. f<enum new_enum>(); // expected-error{{ISO C++ forbids forward references to 'enum' types}}
  218. }
  219. };
  220. template struct S2<int>;
  221. template <class T> struct S3 {
  222. void m() {
  223. f<enum class new_enum>();
  224. }
  225. };
  226. template struct S3<int>;
  227. template <class T> struct S4 {
  228. struct local {};
  229. void m() {
  230. f<local>();
  231. }
  232. };
  233. template struct S4<int>;
  234. template <class T> struct S4a {
  235. union local {};
  236. void m() {
  237. f<local>();
  238. }
  239. };
  240. template struct S4a<int>;
  241. template <class T> struct S5 {
  242. enum local { nothing };
  243. void m() {
  244. f<local>();
  245. }
  246. };
  247. template struct S5<int>;
  248. template <class T> struct S7 {
  249. enum class local { nothing };
  250. void m() {
  251. f<local>();
  252. }
  253. };
  254. template struct S7<int>;
  255. template <class T> void fff(T *x);
  256. template <class T> struct S01 {
  257. struct local { };
  258. void m() {
  259. local x;
  260. fff(&x);
  261. }
  262. };
  263. template struct S01<int>;
  264. template <class T> struct S01a {
  265. union local { };
  266. void m() {
  267. local x;
  268. fff(&x);
  269. }
  270. };
  271. template struct S01a<int>;
  272. template <class T> struct S02 {
  273. enum local { nothing };
  274. void m() {
  275. local x;
  276. fff(&x);
  277. }
  278. };
  279. template struct S02<int>;
  280. template <class T> struct S03 {
  281. enum class local { nothing };
  282. void m() {
  283. local x;
  284. fff(&x);
  285. }
  286. };
  287. template struct S03<int>;
  288. template <class T> struct S04 {
  289. void m() {
  290. struct { } x;
  291. fff(&x);
  292. }
  293. };
  294. template struct S04<int>;
  295. template <class T> struct S04a {
  296. void m() {
  297. union { } x;
  298. fff(&x);
  299. }
  300. };
  301. template struct S04a<int>;
  302. template <class T> struct S05 {
  303. void m() {
  304. enum { nothing } x;
  305. fff(&x);
  306. }
  307. };
  308. template struct S05<int>;
  309. template <class T> struct S06 {
  310. void m() {
  311. class { virtual void mmm() {} } x;
  312. fff(&x);
  313. }
  314. };
  315. template struct S06<int>;
  316. }
  317. namespace PR20625 {
  318. template <typename T>
  319. void f() {
  320. struct N {
  321. static constexpr int get() { return 42; }
  322. };
  323. constexpr int n = N::get();
  324. static_assert(n == 42, "n == 42");
  325. }
  326. void g() { f<void>(); }
  327. }
  328. namespace PR21332 {
  329. template<typename T> void f1() {
  330. struct S { // expected-note{{in instantiation of member class 'S' requested here}}
  331. void g1(int n = T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
  332. };
  333. }
  334. template void f1<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f1<int>' requested here}}
  335. template<typename T> void f2() {
  336. struct S { // expected-note{{in instantiation of member class 'S' requested here}}
  337. void g2() noexcept(T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
  338. };
  339. }
  340. template void f2<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f2<int>' requested here}}
  341. template<typename T> void f3() {
  342. enum S {
  343. val = T::error; // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
  344. };
  345. }
  346. template void f3<int>(); //expected-note{{in instantiation of function template specialization 'PR21332::f3<int>' requested here}}
  347. template<typename T> void f4() {
  348. enum class S {
  349. val = T::error; // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
  350. };
  351. }
  352. template void f4<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f4<int>' requested here}}
  353. template<typename T> void f5() {
  354. class S { // expected-note {{in instantiation of default member initializer 'PR21332::f5()::S::val' requested here}}
  355. int val = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
  356. };
  357. }
  358. template void f5<int>(); // expected-note {{in instantiation of function template specialization 'PR21332::f5<int>' requested here}}
  359. template<typename T> void f6() {
  360. class S { // expected-note {{in instantiation of member function 'PR21332::f6()::S::get' requested here}}
  361. void get() {
  362. class S2 { // expected-note {{in instantiation of member class 'S2' requested here}}
  363. void g1(int n = T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
  364. };
  365. }
  366. };
  367. }
  368. template void f6<int>(); // expected-note{{in instantiation of function template specialization 'PR21332::f6<int>' requested here}}
  369. template<typename T> void f7() {
  370. struct S { void g() noexcept(undefined_val); }; // expected-error{{use of undeclared identifier 'undefined_val'}}
  371. }
  372. template void f7<int>();
  373. }