Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Charles Li | e7cbb3e | 2015-11-17 20:25:05 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
| 4 | |
Douglas Gregor | 4f15f4d | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 5 | template<typename T> |
| 6 | class C { C(int a0 = 0); }; |
| 7 | |
| 8 | template<> |
| 9 | C<char>::C(int a0); |
| 10 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 11 | struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}} |
Charles Li | e7cbb3e | 2015-11-17 20:25:05 +0000 | [diff] [blame] | 12 | #if __cplusplus >= 201103L // C++11 or later |
| 13 | // expected-note@-2 3 {{candidate constructor (the implicit move constructor) not viable}} |
| 14 | #endif |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 15 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 16 | template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \ |
| 17 | // expected-note{{passing argument to parameter 'b' here}} |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 18 | |
| 19 | template<typename T> void f2(T a, T b = T()) { } |
| 20 | |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 21 | template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}} |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 22 | |
| 23 | void g() { |
| 24 | f1(10); |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 25 | f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}} |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 26 | |
| 27 | f2(10); |
| 28 | f2(S()); |
| 29 | |
| 30 | f3(10); |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 31 | f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}} |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 32 | } |
Anders Carlsson | 10ebe78 | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 33 | |
| 34 | template<typename T> struct F { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 35 | 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 Carlsson | 10ebe78 | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Douglas Gregor | 7315672 | 2009-08-25 15:24:38 +0000 | [diff] [blame] | 41 | struct FD : F<int> { }; |
| 42 | |
Anders Carlsson | 10ebe78 | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 43 | void g2() { |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 44 | F<int> f; |
Douglas Gregor | 7315672 | 2009-08-25 15:24:38 +0000 | [diff] [blame] | 45 | FD fd; |
Anders Carlsson | 10ebe78 | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 46 | } |
Anders Carlsson | 114056f | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | dc6d2c3 | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 48 | void g3(F<int> f, F<struct S> s) { |
| 49 | f.f(); |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 50 | s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}} |
Anders Carlsson | faf1ced | 2009-09-06 16:54:02 +0000 | [diff] [blame] | 51 | |
| 52 | F<int> f2; |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 53 | F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}} |
Anders Carlsson | dc6d2c3 | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Anders Carlsson | 114056f | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 56 | template<typename T> struct G { |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 57 | G(T) {} |
Anders Carlsson | 114056f | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | void s(G<int> flags = 10) { } |
| 61 | |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 62 | // Test default arguments |
| 63 | template<typename T> |
| 64 | struct X0 { |
| 65 | void f(T = T()); // expected-error{{no matching}} |
| 66 | }; |
Anders Carlsson | 114056f | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 67 | |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 68 | template<typename U> |
| 69 | void X0<U>::f(U) { } |
Anders Carlsson | dc6d2c3 | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 71 | void test_x0(X0<int> xi) { |
| 72 | xi.f(); |
| 73 | xi.f(17); |
| 74 | } |
| 75 | |
Charles Li | e7cbb3e | 2015-11-17 20:25:05 +0000 | [diff] [blame] | 76 | struct 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 Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 80 | NotDefaultConstructible(int); // expected-note 2{{candidate}} |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | void 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 Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 88 | |
| 89 | template<typename T> |
| 90 | struct X1 { |
| 91 | typedef T value_type; |
| 92 | X1(const value_type& value = value_type()); |
| 93 | }; |
| 94 | |
| 95 | void test_X1() { |
| 96 | X1<int> x1; |
| 97 | } |
Anders Carlsson | 561f793 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 99 | template<typename T> |
| 100 | struct X2 { |
| 101 | void operator()(T = T()); // expected-error{{no matching}} |
| 102 | }; |
| 103 | |
| 104 | void 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 Carlsson | 561f793 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 111 | // PR5283 |
| 112 | namespace PR5283 { |
| 113 | template<typename T> struct A { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 114 | 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 Carlsson | 561f793 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct B : A<int*> { |
| 119 | B(); |
| 120 | }; |
| 121 | B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 122 | |
| 123 | struct C : virtual A<int*> { |
| 124 | C(); |
| 125 | }; |
| 126 | C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 127 | |
| 128 | struct D { |
| 129 | D(); |
| 130 | |
| 131 | A<int*> a; |
| 132 | }; |
| 133 | D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 134 | } |
Sebastian Redl | 14236c8 | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 135 | |
| 136 | // PR5301 |
| 137 | namespace 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 Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 158 | |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 159 | // PR5810 |
| 160 | namespace PR5810 { |
| 161 | template<typename T> |
| 162 | struct allocator { |
Chandler Carruth | a92409c | 2011-01-04 04:44:35 +0000 | [diff] [blame] | 163 | allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}} |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | template<typename T> |
| 167 | struct vector { |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 168 | vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}} |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | struct A { }; |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 172 | struct B { }; |
| 173 | |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 174 | template<typename> |
| 175 | void FilterVTs() { |
| 176 | vector<A> Result; |
| 177 | } |
| 178 | |
| 179 | void f() { |
| 180 | vector<A> Result; |
| 181 | } |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 182 | |
| 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 Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 192 | } |
Douglas Gregor | 8c70253 | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 193 | |
| 194 | template<typename T> void f4(T, int = 17); |
| 195 | template<> void f4<int>(int, int); |
| 196 | |
| 197 | void f4_test(int i) { |
| 198 | f4(i); |
| 199 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 200 | |
| 201 | // Instantiate for initialization |
| 202 | namespace 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 Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 218 | |
| 219 | namespace 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 Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 230 | namespace 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 Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 248 | namespace 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 Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 264 | |
| 265 | namespace 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 Gregor | f0873f4 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 286 | |
| 287 | namespace 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 Friedman | c25372b | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 304 | |
| 305 | namespace 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 Kramer | 90633e3 | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 315 | |
| 316 | namespace PR13758 { |
| 317 | template <typename T> struct move_from { |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 318 | T invalid; |
Benjamin Kramer | 90633e3 | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 319 | }; |
| 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 Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 327 | new unordered_map<void>(); |
| 328 | new unordered_map<void>; |
Benjamin Kramer | 90633e3 | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 329 | } |
| 330 | void tt() { |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 331 | StripedHashTable<int>(); |
Benjamin Kramer | 90633e3 | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 332 | } |
| 333 | } |