blob: 438f5b1aa95f743fe5033fe5fc97ae8ddb4c6a76 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Charles Lie7cbb3e2015-11-17 20:25:05 +00002// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4
Douglas Gregor4f15f4d2009-09-17 19:51:30 +00005template<typename T>
6class C { C(int a0 = 0); };
7
8template<>
9C<char>::C(int a0);
10
John McCalle1ac8d12010-01-13 00:25:19 +000011struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
Charles Lie7cbb3e2015-11-17 20:25:05 +000012#if __cplusplus >= 201103L // C++11 or later
13// expected-note@-2 3 {{candidate constructor (the implicit move constructor) not viable}}
14#endif
Anders Carlsson4562f1f2009-08-25 03:18:48 +000015
Douglas Gregor4f4946a2010-04-22 00:20:18 +000016template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \
17// expected-note{{passing argument to parameter 'b' here}}
Anders Carlsson4562f1f2009-08-25 03:18:48 +000018
19template<typename T> void f2(T a, T b = T()) { }
20
John McCall85f90552010-03-10 11:27:22 +000021template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}}
Anders Carlsson4562f1f2009-08-25 03:18:48 +000022
23void g() {
24 f1(10);
John McCall85f90552010-03-10 11:27:22 +000025 f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}}
Anders Carlsson4562f1f2009-08-25 03:18:48 +000026
27 f2(10);
28 f2(S());
29
30 f3(10);
John McCall85f90552010-03-10 11:27:22 +000031 f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}}
Anders Carlsson4562f1f2009-08-25 03:18:48 +000032}
Anders Carlsson10ebe782009-08-25 13:07:08 +000033
34template<typename T> struct F {
Douglas Gregor4f4946a2010-04-22 00:20:18 +000035 F(T t = 10); // expected-error{{no viable conversion}} \
36 // expected-note{{passing argument to parameter 't' here}}
37 void f(T t = 10); // expected-error{{no viable conversion}} \
38 // expected-note{{passing argument to parameter 't' here}}
Anders Carlsson10ebe782009-08-25 13:07:08 +000039};
40
Douglas Gregor73156722009-08-25 15:24:38 +000041struct FD : F<int> { };
42
Anders Carlsson10ebe782009-08-25 13:07:08 +000043void g2() {
Anders Carlsson657bad42009-09-05 05:14:19 +000044 F<int> f;
Douglas Gregor73156722009-08-25 15:24:38 +000045 FD fd;
Anders Carlsson10ebe782009-08-25 13:07:08 +000046}
Anders Carlsson114056f2009-08-25 13:46:13 +000047
Anders Carlssondc6d2c32009-09-05 05:38:54 +000048void g3(F<int> f, F<struct S> s) {
49 f.f();
John McCall85f90552010-03-10 11:27:22 +000050 s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}}
Anders Carlssonfaf1ced2009-09-06 16:54:02 +000051
52 F<int> f2;
John McCall85f90552010-03-10 11:27:22 +000053 F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}}
Anders Carlssondc6d2c32009-09-05 05:38:54 +000054}
55
Anders Carlsson114056f2009-08-25 13:46:13 +000056template<typename T> struct G {
Anders Carlsson657bad42009-09-05 05:14:19 +000057 G(T) {}
Anders Carlsson114056f2009-08-25 13:46:13 +000058};
59
60void s(G<int> flags = 10) { }
61
Douglas Gregorc732aba2009-09-11 18:44:32 +000062// Test default arguments
63template<typename T>
64struct X0 {
65 void f(T = T()); // expected-error{{no matching}}
66};
Anders Carlsson114056f2009-08-25 13:46:13 +000067
Douglas Gregorc732aba2009-09-11 18:44:32 +000068template<typename U>
69void X0<U>::f(U) { }
Anders Carlssondc6d2c32009-09-05 05:38:54 +000070
Douglas Gregorc732aba2009-09-11 18:44:32 +000071void test_x0(X0<int> xi) {
72 xi.f();
73 xi.f(17);
74}
75
Charles Lie7cbb3e2015-11-17 20:25:05 +000076struct NotDefaultConstructible { // expected-note 2 {{candidate constructor (the implicit copy constructor) not viable}}
77#if __cplusplus >= 201103L // C++11 or later
78// expected-note@-2 2 {{candidate constructor (the implicit move constructor) not viable}}
79#endif
Douglas Gregor1bc688d2009-11-09 19:27:57 +000080 NotDefaultConstructible(int); // expected-note 2{{candidate}}
Douglas Gregorc732aba2009-09-11 18:44:32 +000081};
82
83void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) {
84 xn.f(NotDefaultConstructible(17));
85 xn.f(42);
86 xn.f(); // expected-note{{in instantiation of default function argument}}
87}
Douglas Gregor64621e62009-09-16 18:34:49 +000088
89template<typename T>
90struct X1 {
91 typedef T value_type;
92 X1(const value_type& value = value_type());
93};
94
95void test_X1() {
96 X1<int> x1;
97}
Anders Carlsson561f7932009-10-29 15:46:07 +000098
Douglas Gregor1bc688d2009-11-09 19:27:57 +000099template<typename T>
100struct X2 {
101 void operator()(T = T()); // expected-error{{no matching}}
102};
103
104void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) {
105 x2i();
106 x2i(17);
107 x2n(NotDefaultConstructible(17));
108 x2n(); // expected-note{{in instantiation of default function argument}}
109}
110
Anders Carlsson561f7932009-10-29 15:46:07 +0000111// PR5283
112namespace PR5283 {
113template<typename T> struct A {
Douglas Gregor4f4946a2010-04-22 00:20:18 +0000114 A(T = 1); // expected-error 3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'int'}} \
115 // expected-note 3{{passing argument to parameter here}}
Anders Carlsson561f7932009-10-29 15:46:07 +0000116};
117
118struct B : A<int*> {
119 B();
120};
121B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
122
123struct C : virtual A<int*> {
124 C();
125};
126C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
127
128struct D {
129 D();
130
131 A<int*> a;
132};
133D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
134}
Sebastian Redl14236c82009-11-08 13:56:19 +0000135
136// PR5301
137namespace pr5301 {
138 void f(int, int = 0);
139
140 template <typename T>
141 void g(T, T = 0);
142
143 template <int I>
144 void i(int a = I);
145
146 template <typename T>
147 void h(T t) {
148 f(0);
149 g(1);
150 g(t);
151 i<2>();
152 }
153
154 void test() {
155 h(0);
156 }
157}
Douglas Gregor1bc688d2009-11-09 19:27:57 +0000158
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000159// PR5810
160namespace PR5810 {
161 template<typename T>
162 struct allocator {
Chandler Carrutha92409c2011-01-04 04:44:35 +0000163 allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}}
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000164 };
165
166 template<typename T>
167 struct vector {
Douglas Gregor033f6752009-12-23 23:03:06 +0000168 vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}}
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000169 };
170
171 struct A { };
Douglas Gregor033f6752009-12-23 23:03:06 +0000172 struct B { };
173
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000174 template<typename>
175 void FilterVTs() {
176 vector<A> Result;
177 }
178
179 void f() {
180 vector<A> Result;
181 }
Douglas Gregor033f6752009-12-23 23:03:06 +0000182
183 template<typename T>
184 struct X {
185 vector<B> bs;
186 X() { }
187 };
188
189 void f2() {
190 X<float> x; // expected-note{{member function}}
191 }
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000192}
Douglas Gregor8c702532010-02-05 07:33:43 +0000193
194template<typename T> void f4(T, int = 17);
195template<> void f4<int>(int, int);
196
197void f4_test(int i) {
198 f4(i);
199}
Douglas Gregord9848152010-04-26 14:36:57 +0000200
201// Instantiate for initialization
202namespace InstForInit {
203 template<typename T>
204 struct Ptr {
205 typedef T* type;
206 Ptr(type);
207 };
208
209 template<typename T>
210 struct Holder {
211 Holder(int i, Ptr<T> ptr = 0);
212 };
213
214 void test_holder(int i) {
215 Holder<int> h(i);
216 }
217};
Douglas Gregor8a01b2a2010-09-11 20:24:53 +0000218
219namespace PR5810b {
220 template<typename T>
221 T broken() {
222 T t;
223 double**** not_it = t;
224 }
225
226 void f(int = broken<int>());
227 void g() { f(17); }
228}
229
Douglas Gregor32b3de52010-09-11 23:32:50 +0000230namespace PR5810c {
231 template<typename T>
232 struct X {
233 X() {
234 T t;
235 double *****p = t; // expected-error{{cannot initialize a variable of type 'double *****' with an lvalue of type 'int'}}
236 }
237 X(const X&) { }
238 };
239
240 struct Y : X<int> { // expected-note{{instantiation of}}
241 };
242
243 void f(Y y = Y());
244
245 void g() { f(); }
246}
247
Douglas Gregor8a01b2a2010-09-11 20:24:53 +0000248namespace PR8127 {
249 template< typename T > class PointerClass {
250 public:
251 PointerClass( T * object_p ) : p_( object_p ) {
252 p_->acquire();
253 }
254 private:
255 T * p_;
256 };
257
258 class ExternallyImplementedClass;
259
260 class MyClass {
261 void foo( PointerClass<ExternallyImplementedClass> = 0 );
262 };
263}
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000264
265namespace rdar8427926 {
266 template<typename T>
267 struct Boom {
268 ~Boom() {
269 T t;
270 double *******ptr = t; // expected-error 2{{cannot initialize}}
271 }
272 };
273
274 Boom<float> *bfp;
275
276 struct X {
277 void f(Boom<int> = Boom<int>()) { } // expected-note{{requested here}}
278 void g(int x = (delete bfp, 0)); // expected-note{{requested here}}
279 };
280
281 void test(X *x) {
282 x->f();
283 x->g();
284 }
285}
Douglas Gregorf0873f42010-10-19 17:17:35 +0000286
287namespace PR8401 {
288 template<typename T>
289 struct A {
290 A() { T* x = 1; } // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
291 };
292
293 template<typename T>
294 struct B {
295 B(const A<T>& a = A<T>()); // expected-note{{in instantiation of}}
296 };
297
298 void f(B<int> b = B<int>());
299
300 void g() {
301 f();
302 }
303}
Eli Friedmanc25372b2012-04-26 22:43:24 +0000304
305namespace PR12581 {
306 const int a = 0;
307 template < typename > struct A;
308 template < typename MatrixType, int =
309 A < MatrixType >::Flags ? : A < MatrixType >::Flags & a > class B;
310 void
311 fn1 ()
312 {
313 }
314}
Benjamin Kramer90633e32012-11-23 17:04:52 +0000315
316namespace PR13758 {
317 template <typename T> struct move_from {
Richard Smithdd2ca572012-11-26 08:32:48 +0000318 T invalid;
Benjamin Kramer90633e32012-11-23 17:04:52 +0000319 };
320 template <class K>
321 struct unordered_map {
322 explicit unordered_map(int n = 42);
323 unordered_map(move_from<K> other);
324 };
325 template<typename T>
326 void StripedHashTable() {
Richard Smithdd2ca572012-11-26 08:32:48 +0000327 new unordered_map<void>();
328 new unordered_map<void>;
Benjamin Kramer90633e32012-11-23 17:04:52 +0000329 }
330 void tt() {
Richard Smithdd2ca572012-11-26 08:32:48 +0000331 StripedHashTable<int>();
Benjamin Kramer90633e32012-11-23 17:04:52 +0000332 }
333}