Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 2 | template<typename T> |
| 3 | class C { C(int a0 = 0); }; |
| 4 | |
| 5 | template<> |
| 6 | C<char>::C(int a0); |
| 7 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8 | struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}} |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 9 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 10 | template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \ |
| 11 | // expected-note{{passing argument to parameter 'b' here}} |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 12 | |
| 13 | template<typename T> void f2(T a, T b = T()) { } |
| 14 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 15 | template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}} |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 16 | |
| 17 | void g() { |
| 18 | f1(10); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 19 | f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}} |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 20 | |
| 21 | f2(10); |
| 22 | f2(S()); |
| 23 | |
| 24 | f3(10); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 25 | f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}} |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 26 | } |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 27 | |
| 28 | template<typename T> struct F { |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 29 | F(T t = 10); // expected-error{{no viable conversion}} \ |
| 30 | // expected-note{{passing argument to parameter 't' here}} |
| 31 | void f(T t = 10); // expected-error{{no viable conversion}} \ |
| 32 | // expected-note{{passing argument to parameter 't' here}} |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
Douglas Gregor | 0b84a53 | 2009-08-25 15:24:38 +0000 | [diff] [blame] | 35 | struct FD : F<int> { }; |
| 36 | |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 37 | void g2() { |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 38 | F<int> f; |
Douglas Gregor | 0b84a53 | 2009-08-25 15:24:38 +0000 | [diff] [blame] | 39 | FD fd; |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 40 | } |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 41 | |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 42 | void g3(F<int> f, F<struct S> s) { |
| 43 | f.f(); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 44 | s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}} |
Anders Carlsson | 21e1c4e | 2009-09-06 16:54:02 +0000 | [diff] [blame] | 45 | |
| 46 | F<int> f2; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 47 | F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}} |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 50 | template<typename T> struct G { |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 51 | G(T) {} |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | void s(G<int> flags = 10) { } |
| 55 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 56 | // Test default arguments |
| 57 | template<typename T> |
| 58 | struct X0 { |
| 59 | void f(T = T()); // expected-error{{no matching}} |
| 60 | }; |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 62 | template<typename U> |
| 63 | void X0<U>::f(U) { } |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 65 | void test_x0(X0<int> xi) { |
| 66 | xi.f(); |
| 67 | xi.f(17); |
| 68 | } |
| 69 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 70 | struct NotDefaultConstructible { // expected-note 2{{candidate}} |
| 71 | NotDefaultConstructible(int); // expected-note 2{{candidate}} |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) { |
| 75 | xn.f(NotDefaultConstructible(17)); |
| 76 | xn.f(42); |
| 77 | xn.f(); // expected-note{{in instantiation of default function argument}} |
| 78 | } |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 79 | |
| 80 | template<typename T> |
| 81 | struct X1 { |
| 82 | typedef T value_type; |
| 83 | X1(const value_type& value = value_type()); |
| 84 | }; |
| 85 | |
| 86 | void test_X1() { |
| 87 | X1<int> x1; |
| 88 | } |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 89 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 90 | template<typename T> |
| 91 | struct X2 { |
| 92 | void operator()(T = T()); // expected-error{{no matching}} |
| 93 | }; |
| 94 | |
| 95 | void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) { |
| 96 | x2i(); |
| 97 | x2i(17); |
| 98 | x2n(NotDefaultConstructible(17)); |
| 99 | x2n(); // expected-note{{in instantiation of default function argument}} |
| 100 | } |
| 101 | |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 102 | // PR5283 |
| 103 | namespace PR5283 { |
| 104 | template<typename T> struct A { |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 105 | A(T = 1); // expected-error 3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'int'}} \ |
| 106 | // expected-note 3{{passing argument to parameter here}} |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | struct B : A<int*> { |
| 110 | B(); |
| 111 | }; |
| 112 | B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 113 | |
| 114 | struct C : virtual A<int*> { |
| 115 | C(); |
| 116 | }; |
| 117 | C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 118 | |
| 119 | struct D { |
| 120 | D(); |
| 121 | |
| 122 | A<int*> a; |
| 123 | }; |
| 124 | D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 125 | } |
Sebastian Redl | a29e51b | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 126 | |
| 127 | // PR5301 |
| 128 | namespace pr5301 { |
| 129 | void f(int, int = 0); |
| 130 | |
| 131 | template <typename T> |
| 132 | void g(T, T = 0); |
| 133 | |
| 134 | template <int I> |
| 135 | void i(int a = I); |
| 136 | |
| 137 | template <typename T> |
| 138 | void h(T t) { |
| 139 | f(0); |
| 140 | g(1); |
| 141 | g(t); |
| 142 | i<2>(); |
| 143 | } |
| 144 | |
| 145 | void test() { |
| 146 | h(0); |
| 147 | } |
| 148 | } |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 150 | // PR5810 |
| 151 | namespace PR5810 { |
| 152 | template<typename T> |
| 153 | struct allocator { |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 154 | allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array size is negative}} |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | template<typename T> |
| 158 | struct vector { |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 159 | vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}} |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | struct A { }; |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 163 | struct B { }; |
| 164 | |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 165 | template<typename> |
| 166 | void FilterVTs() { |
| 167 | vector<A> Result; |
| 168 | } |
| 169 | |
| 170 | void f() { |
| 171 | vector<A> Result; |
| 172 | } |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 173 | |
| 174 | template<typename T> |
| 175 | struct X { |
| 176 | vector<B> bs; |
| 177 | X() { } |
| 178 | }; |
| 179 | |
| 180 | void f2() { |
| 181 | X<float> x; // expected-note{{member function}} |
| 182 | } |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 183 | } |
Douglas Gregor | 525f96c | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 184 | |
| 185 | template<typename T> void f4(T, int = 17); |
| 186 | template<> void f4<int>(int, int); |
| 187 | |
| 188 | void f4_test(int i) { |
| 189 | f4(i); |
| 190 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 191 | |
| 192 | // Instantiate for initialization |
| 193 | namespace InstForInit { |
| 194 | template<typename T> |
| 195 | struct Ptr { |
| 196 | typedef T* type; |
| 197 | Ptr(type); |
| 198 | }; |
| 199 | |
| 200 | template<typename T> |
| 201 | struct Holder { |
| 202 | Holder(int i, Ptr<T> ptr = 0); |
| 203 | }; |
| 204 | |
| 205 | void test_holder(int i) { |
| 206 | Holder<int> h(i); |
| 207 | } |
| 208 | }; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 209 | |
| 210 | namespace PR5810b { |
| 211 | template<typename T> |
| 212 | T broken() { |
| 213 | T t; |
| 214 | double**** not_it = t; |
| 215 | } |
| 216 | |
| 217 | void f(int = broken<int>()); |
| 218 | void g() { f(17); } |
| 219 | } |
| 220 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 221 | namespace PR5810c { |
| 222 | template<typename T> |
| 223 | struct X { |
| 224 | X() { |
| 225 | T t; |
| 226 | double *****p = t; // expected-error{{cannot initialize a variable of type 'double *****' with an lvalue of type 'int'}} |
| 227 | } |
| 228 | X(const X&) { } |
| 229 | }; |
| 230 | |
| 231 | struct Y : X<int> { // expected-note{{instantiation of}} |
| 232 | }; |
| 233 | |
| 234 | void f(Y y = Y()); |
| 235 | |
| 236 | void g() { f(); } |
| 237 | } |
| 238 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 239 | namespace PR8127 { |
| 240 | template< typename T > class PointerClass { |
| 241 | public: |
| 242 | PointerClass( T * object_p ) : p_( object_p ) { |
| 243 | p_->acquire(); |
| 244 | } |
| 245 | private: |
| 246 | T * p_; |
| 247 | }; |
| 248 | |
| 249 | class ExternallyImplementedClass; |
| 250 | |
| 251 | class MyClass { |
| 252 | void foo( PointerClass<ExternallyImplementedClass> = 0 ); |
| 253 | }; |
| 254 | } |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 255 | |
| 256 | namespace rdar8427926 { |
| 257 | template<typename T> |
| 258 | struct Boom { |
| 259 | ~Boom() { |
| 260 | T t; |
| 261 | double *******ptr = t; // expected-error 2{{cannot initialize}} |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | Boom<float> *bfp; |
| 266 | |
| 267 | struct X { |
| 268 | void f(Boom<int> = Boom<int>()) { } // expected-note{{requested here}} |
| 269 | void g(int x = (delete bfp, 0)); // expected-note{{requested here}} |
| 270 | }; |
| 271 | |
| 272 | void test(X *x) { |
| 273 | x->f(); |
| 274 | x->g(); |
| 275 | } |
| 276 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame^] | 277 | |
| 278 | namespace PR8401 { |
| 279 | template<typename T> |
| 280 | struct A { |
| 281 | A() { T* x = 1; } // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} |
| 282 | }; |
| 283 | |
| 284 | template<typename T> |
| 285 | struct B { |
| 286 | B(const A<T>& a = A<T>()); // expected-note{{in instantiation of}} |
| 287 | }; |
| 288 | |
| 289 | void f(B<int> b = B<int>()); |
| 290 | |
| 291 | void g() { |
| 292 | f(); |
| 293 | } |
| 294 | } |