instantiate-method.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. template<typename T>
  3. class X {
  4. public:
  5. void f(T x); // expected-error{{argument may not have 'void' type}}
  6. void g(T*);
  7. static int h(T, T); // expected-error {{argument may not have 'void' type}}
  8. };
  9. int identity(int x) { return x; }
  10. void test(X<int> *xi, int *ip, X<int(int)> *xf) {
  11. xi->f(17);
  12. xi->g(ip);
  13. xf->f(&identity);
  14. xf->g(identity);
  15. X<int>::h(17, 25);
  16. X<int(int)>::h(identity, &identity);
  17. }
  18. void test_bad() {
  19. X<void> xv; // expected-note{{in instantiation of template class 'X<void>' requested here}}
  20. }
  21. template<typename T, typename U>
  22. class Overloading {
  23. public:
  24. int& f(T, T); // expected-note{{previous declaration is here}}
  25. float& f(T, U); // expected-error{{functions that differ only in their return type cannot be overloaded}}
  26. };
  27. void test_ovl(Overloading<int, long> *oil, int i, long l) {
  28. int &ir = oil->f(i, i);
  29. float &fr = oil->f(i, l);
  30. }
  31. void test_ovl_bad() {
  32. Overloading<float, float> off; // expected-note{{in instantiation of template class 'Overloading<float, float>' requested here}}
  33. }
  34. template<typename T>
  35. class HasDestructor {
  36. public:
  37. virtual ~HasDestructor() = 0;
  38. };
  39. int i = sizeof(HasDestructor<int>); // FIXME: forces instantiation, but
  40. // the code below should probably instantiate by itself.
  41. int abstract_destructor[__is_abstract(HasDestructor<int>)? 1 : -1];
  42. template<typename T>
  43. class Constructors {
  44. public:
  45. Constructors(const T&);
  46. Constructors(const Constructors &other);
  47. };
  48. void test_constructors() {
  49. Constructors<int> ci1(17);
  50. Constructors<int> ci2 = ci1;
  51. }
  52. template<typename T>
  53. struct ConvertsTo {
  54. operator T();
  55. };
  56. void test_converts_to(ConvertsTo<int> ci, ConvertsTo<int *> cip) {
  57. int i = ci;
  58. int *ip = cip;
  59. }
  60. // PR4660
  61. template<class T> struct A0 { operator T*(); };
  62. template<class T> struct A1;
  63. int *a(A0<int> &x0, A1<int> &x1) {
  64. int *y0 = x0;
  65. int *y1 = x1; // expected-error{{no viable conversion}}
  66. }
  67. struct X0Base {
  68. int &f();
  69. int& g(int);
  70. static double &g(double);
  71. };
  72. template<typename T>
  73. struct X0 : X0Base {
  74. };
  75. template<typename U>
  76. struct X1 : X0<U> {
  77. int &f2() {
  78. return X0Base::f();
  79. }
  80. };
  81. void test_X1(X1<int> x1i) {
  82. int &ir = x1i.f2();
  83. }
  84. template<typename U>
  85. struct X2 : X0Base, U {
  86. int &f2() { return X0Base::f(); }
  87. };
  88. template<typename T>
  89. struct X3 {
  90. void test(T x) {
  91. double& d1 = X0Base::g(x);
  92. }
  93. };
  94. template struct X3<double>;
  95. // Don't try to instantiate this, it's invalid.
  96. namespace test1 {
  97. template <class T> class A {};
  98. template <class T> class B {
  99. void foo(A<test1::Undeclared> &a) // expected-error {{no member named 'Undeclared' in namespace 'test1'}}
  100. {}
  101. };
  102. template class B<int>;
  103. }
  104. namespace PR6947 {
  105. template< class T >
  106. struct X {
  107. int f0( )
  108. {
  109. typedef void ( X::*impl_fun_ptr )( );
  110. impl_fun_ptr pImpl = &X::template
  111. f0_impl1<int>;
  112. }
  113. private:
  114. int f1() {
  115. }
  116. template< class Processor>
  117. void f0_impl1( )
  118. {
  119. }
  120. };
  121. char g0() {
  122. X<int> pc;
  123. pc.f0();
  124. }
  125. }
  126. namespace PR7022 {
  127. template <typename >
  128. struct X1
  129. {
  130. typedef int state_t( );
  131. state_t g ;
  132. };
  133. template < typename U = X1<int> > struct X2
  134. {
  135. X2( U = U())
  136. {
  137. }
  138. };
  139. void m(void)
  140. {
  141. typedef X2<> X2_type;
  142. X2_type c;
  143. }
  144. }
  145. namespace SameSignatureAfterInstantiation {
  146. template<typename T> struct S {
  147. void f(T *); // expected-note {{previous}}
  148. void f(const T*); // expected-error-re {{multiple overloads of 'f' instantiate to the same signature 'void (const int *){{( __attribute__\(\(thiscall\)\))?}}'}}
  149. };
  150. S<const int> s; // expected-note {{instantiation}}
  151. }
  152. namespace PR22040 {
  153. template <typename T> struct Foobar {
  154. template <> void bazqux(typename T::type) {} // expected-error {{cannot specialize a function 'bazqux' within class scope}} expected-error 2{{cannot be used prior to '::' because it has no members}}
  155. };
  156. void test() {
  157. // FIXME: we should suppress the "no member" errors
  158. Foobar<void>::bazqux(); // expected-error{{no member named 'bazqux' in }} expected-note{{in instantiation of template class }}
  159. Foobar<int>::bazqux(); // expected-error{{no member named 'bazqux' in }} expected-note{{in instantiation of template class }}
  160. Foobar<int>::bazqux(3); // expected-error{{no member named 'bazqux' in }}
  161. }
  162. }
  163. template <typename>
  164. struct SpecializationOfGlobalFnInClassScope {
  165. template <>
  166. void ::Fn(); // expected-error{{cannot have a qualified name}}
  167. };
  168. class AbstractClassWithGlobalFn {
  169. template <typename>
  170. void ::f(); // expected-error{{cannot have a qualified name}}
  171. virtual void f1() = 0;
  172. };