blob: 5d301be2fb13733b2f25dfda804fc56ec792c8ba [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregord85cef52009-09-17 19:51:30 +00002template<typename T>
3class C { C(int a0 = 0); };
4
5template<>
6C<char>::C(int a0);
7
John McCall220ccbf2010-01-13 00:25:19 +00008struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
Anders Carlsson9351c172009-08-25 03:18:48 +00009
Douglas Gregora41a8c52010-04-22 00:20:18 +000010template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \
11// expected-note{{passing argument to parameter 'b' here}}
Anders Carlsson9351c172009-08-25 03:18:48 +000012
13template<typename T> void f2(T a, T b = T()) { }
14
John McCall7c2342d2010-03-10 11:27:22 +000015template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}}
Anders Carlsson9351c172009-08-25 03:18:48 +000016
17void g() {
18 f1(10);
John McCall7c2342d2010-03-10 11:27:22 +000019 f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}}
Anders Carlsson9351c172009-08-25 03:18:48 +000020
21 f2(10);
22 f2(S());
23
24 f3(10);
John McCall7c2342d2010-03-10 11:27:22 +000025 f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}}
Anders Carlsson9351c172009-08-25 03:18:48 +000026}
Anders Carlsson8644aec2009-08-25 13:07:08 +000027
28template<typename T> struct F {
Douglas Gregora41a8c52010-04-22 00:20:18 +000029 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 Carlsson8644aec2009-08-25 13:07:08 +000033};
34
Douglas Gregor0b84a532009-08-25 15:24:38 +000035struct FD : F<int> { };
36
Anders Carlsson8644aec2009-08-25 13:07:08 +000037void g2() {
Anders Carlsson25cae7f2009-09-05 05:14:19 +000038 F<int> f;
Douglas Gregor0b84a532009-08-25 15:24:38 +000039 FD fd;
Anders Carlsson8644aec2009-08-25 13:07:08 +000040}
Anders Carlsson5653ca52009-08-25 13:46:13 +000041
Anders Carlsson6bc107b2009-09-05 05:38:54 +000042void g3(F<int> f, F<struct S> s) {
43 f.f();
John McCall7c2342d2010-03-10 11:27:22 +000044 s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}}
Anders Carlsson21e1c4e2009-09-06 16:54:02 +000045
46 F<int> f2;
John McCall7c2342d2010-03-10 11:27:22 +000047 F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}}
Anders Carlsson6bc107b2009-09-05 05:38:54 +000048}
49
Anders Carlsson5653ca52009-08-25 13:46:13 +000050template<typename T> struct G {
Anders Carlsson25cae7f2009-09-05 05:14:19 +000051 G(T) {}
Anders Carlsson5653ca52009-08-25 13:46:13 +000052};
53
54void s(G<int> flags = 10) { }
55
Douglas Gregor6cc15182009-09-11 18:44:32 +000056// Test default arguments
57template<typename T>
58struct X0 {
59 void f(T = T()); // expected-error{{no matching}}
60};
Anders Carlsson5653ca52009-08-25 13:46:13 +000061
Douglas Gregor6cc15182009-09-11 18:44:32 +000062template<typename U>
63void X0<U>::f(U) { }
Anders Carlsson6bc107b2009-09-05 05:38:54 +000064
Douglas Gregor6cc15182009-09-11 18:44:32 +000065void test_x0(X0<int> xi) {
66 xi.f();
67 xi.f(17);
68}
69
Douglas Gregord47c47d2009-11-09 19:27:57 +000070struct NotDefaultConstructible { // expected-note 2{{candidate}}
71 NotDefaultConstructible(int); // expected-note 2{{candidate}}
Douglas Gregor6cc15182009-09-11 18:44:32 +000072};
73
74void 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 Gregore95b4092009-09-16 18:34:49 +000079
80template<typename T>
81struct X1 {
82 typedef T value_type;
83 X1(const value_type& value = value_type());
84};
85
86void test_X1() {
87 X1<int> x1;
88}
Anders Carlsson0ebb6d32009-10-29 15:46:07 +000089
Douglas Gregord47c47d2009-11-09 19:27:57 +000090template<typename T>
91struct X2 {
92 void operator()(T = T()); // expected-error{{no matching}}
93};
94
95void 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 Carlsson0ebb6d32009-10-29 15:46:07 +0000102// PR5283
103namespace PR5283 {
104template<typename T> struct A {
Douglas Gregora41a8c52010-04-22 00:20:18 +0000105 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 Carlsson0ebb6d32009-10-29 15:46:07 +0000107};
108
109struct B : A<int*> {
110 B();
111};
112B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
113
114struct C : virtual A<int*> {
115 C();
116};
117C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
118
119struct D {
120 D();
121
122 A<int*> a;
123};
124D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
125}
Sebastian Redla29e51b2009-11-08 13:56:19 +0000126
127// PR5301
128namespace 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 Gregord47c47d2009-11-09 19:27:57 +0000149
Douglas Gregor65222e82009-12-23 18:19:08 +0000150// PR5810
151namespace PR5810 {
152 template<typename T>
153 struct allocator {
Chandler Carruthb2b5cc02011-01-04 04:44:35 +0000154 allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}}
Douglas Gregor65222e82009-12-23 18:19:08 +0000155 };
156
157 template<typename T>
158 struct vector {
Douglas Gregor036aed12009-12-23 23:03:06 +0000159 vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}}
Douglas Gregor65222e82009-12-23 18:19:08 +0000160 };
161
162 struct A { };
Douglas Gregor036aed12009-12-23 23:03:06 +0000163 struct B { };
164
Douglas Gregor65222e82009-12-23 18:19:08 +0000165 template<typename>
166 void FilterVTs() {
167 vector<A> Result;
168 }
169
170 void f() {
171 vector<A> Result;
172 }
Douglas Gregor036aed12009-12-23 23:03:06 +0000173
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 Gregor65222e82009-12-23 18:19:08 +0000183}
Douglas Gregor525f96c2010-02-05 07:33:43 +0000184
185template<typename T> void f4(T, int = 17);
186template<> void f4<int>(int, int);
187
188void f4_test(int i) {
189 f4(i);
190}
Douglas Gregor087fb7d2010-04-26 14:36:57 +0000191
192// Instantiate for initialization
193namespace 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 Gregorbe0f7bd2010-09-11 20:24:53 +0000209
210namespace 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 Gregor4fcf5b22010-09-11 23:32:50 +0000221namespace 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 Gregorbe0f7bd2010-09-11 20:24:53 +0000239namespace 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 Gregor5833b0b2010-09-14 22:55:20 +0000255
256namespace 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 Gregor102ff972010-10-19 17:17:35 +0000277
278namespace 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}