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