default-expr-arguments.cpp revision 9b94cd1b2527c8dc9ec19a8608f2313010e721b5
1// RUN: %clang_cc1 -fsyntax-only -verify %s 2template<typename T> 3class C { C(int a0 = 0); }; 4 5template<> 6C<char>::C(int a0); 7 8struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}} 9 10template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \ 11// expected-note{{passing argument to parameter 'b' here}} 12 13template<typename T> void f2(T a, T b = T()) { } 14 15template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}} 16 17void g() { 18 f1(10); 19 f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}} 20 21 f2(10); 22 f2(S()); 23 24 f3(10); 25 f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}} 26} 27 28template<typename T> struct F { 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}} 33}; 34 35struct FD : F<int> { }; 36 37void g2() { 38 F<int> f; 39 FD fd; 40} 41 42void g3(F<int> f, F<struct S> s) { 43 f.f(); 44 s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}} 45 46 F<int> f2; 47 F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}} 48} 49 50template<typename T> struct G { 51 G(T) {} 52}; 53 54void s(G<int> flags = 10) { } 55 56// Test default arguments 57template<typename T> 58struct X0 { 59 void f(T = T()); // expected-error{{no matching}} 60}; 61 62template<typename U> 63void X0<U>::f(U) { } 64 65void test_x0(X0<int> xi) { 66 xi.f(); 67 xi.f(17); 68} 69 70struct NotDefaultConstructible { // expected-note 2{{candidate}} 71 NotDefaultConstructible(int); // expected-note 2{{candidate}} 72}; 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} 79 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} 89 90template<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 102// PR5283 103namespace PR5283 { 104template<typename T> struct A { 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}} 107}; 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} 126 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} 149 150// PR5810 151namespace PR5810 { 152 template<typename T> 153 struct allocator { 154 allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}} 155 }; 156 157 template<typename T> 158 struct vector { 159 vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}} 160 }; 161 162 struct A { }; 163 struct B { }; 164 165 template<typename> 166 void FilterVTs() { 167 vector<A> Result; 168 } 169 170 void f() { 171 vector<A> Result; 172 } 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 } 183} 184 185template<typename T> void f4(T, int = 17); 186template<> void f4<int>(int, int); 187 188void f4_test(int i) { 189 f4(i); 190} 191 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}; 209 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 221namespace 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 239namespace 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} 255 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} 277 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} 295 296namespace PR12581 { 297 const int a = 0; 298 template < typename > struct A; 299 template < typename MatrixType, int = 300 A < MatrixType >::Flags ? : A < MatrixType >::Flags & a > class B; 301 void 302 fn1 () 303 { 304 } 305} 306