18fbe78f6SDaniel Dunbar // RUN: %clang_cc1 -fsyntax-only -verify %s
2*e7cbb3edSCharles Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3*e7cbb3edSCharles Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4*e7cbb3edSCharles Li
54f15f4deSDouglas Gregor template<typename T>
64f15f4deSDouglas Gregor class C { C(int a0 = 0); };
74f15f4deSDouglas Gregor
84f15f4deSDouglas Gregor template<>
94f15f4deSDouglas Gregor C<char>::C(int a0);
104f15f4deSDouglas Gregor
11e1ac8d17SJohn McCall struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
12*e7cbb3edSCharles Li #if __cplusplus >= 201103L // C++11 or later
13*e7cbb3edSCharles Li // expected-note@-2 3 {{candidate constructor (the implicit move constructor) not viable}}
14*e7cbb3edSCharles Li #endif
154562f1f0SAnders Carlsson
f1(T a,T b=10)164f4946aaSDouglas Gregor template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \
174f4946aaSDouglas Gregor // expected-note{{passing argument to parameter 'b' here}}
184562f1f0SAnders Carlsson
f2(T a,T b=T ())194562f1f0SAnders Carlsson template<typename T> void f2(T a, T b = T()) { }
204562f1f0SAnders Carlsson
2185f90559SJohn McCall template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}}
224562f1f0SAnders Carlsson
g()234562f1f0SAnders Carlsson void g() {
244562f1f0SAnders Carlsson f1(10);
2585f90559SJohn McCall f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}}
264562f1f0SAnders Carlsson
274562f1f0SAnders Carlsson f2(10);
284562f1f0SAnders Carlsson f2(S());
294562f1f0SAnders Carlsson
304562f1f0SAnders Carlsson f3(10);
3185f90559SJohn McCall f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}}
324562f1f0SAnders Carlsson }
3310ebe787SAnders Carlsson
3410ebe787SAnders Carlsson template<typename T> struct F {
354f4946aaSDouglas Gregor F(T t = 10); // expected-error{{no viable conversion}} \
364f4946aaSDouglas Gregor // expected-note{{passing argument to parameter 't' here}}
374f4946aaSDouglas Gregor void f(T t = 10); // expected-error{{no viable conversion}} \
384f4946aaSDouglas Gregor // expected-note{{passing argument to parameter 't' here}}
3910ebe787SAnders Carlsson };
4010ebe787SAnders Carlsson
417315672aSDouglas Gregor struct FD : F<int> { };
427315672aSDouglas Gregor
g2()4310ebe787SAnders Carlsson void g2() {
4410ebe787SAnders Carlsson F<int> f;
457315672aSDouglas Gregor FD fd;
4610ebe787SAnders Carlsson }
47114056f2SAnders Carlsson
g3(F<int> f,F<struct S> s)48dc6d2c3cSAnders Carlsson void g3(F<int> f, F<struct S> s) {
49dc6d2c3cSAnders Carlsson f.f();
5085f90559SJohn McCall s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}}
51faf1ced5SAnders Carlsson
52faf1ced5SAnders Carlsson F<int> f2;
5385f90559SJohn McCall F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}}
54dc6d2c3cSAnders Carlsson }
55dc6d2c3cSAnders Carlsson
56114056f2SAnders Carlsson template<typename T> struct G {
GG57114056f2SAnders Carlsson G(T) {}
58114056f2SAnders Carlsson };
59114056f2SAnders Carlsson
s(G<int> flags=10)60114056f2SAnders Carlsson void s(G<int> flags = 10) { }
61114056f2SAnders Carlsson
62c732aba9SDouglas Gregor // Test default arguments
63c732aba9SDouglas Gregor template<typename T>
64c732aba9SDouglas Gregor struct X0 {
65c732aba9SDouglas Gregor void f(T = T()); // expected-error{{no matching}}
66c732aba9SDouglas Gregor };
67114056f2SAnders Carlsson
68c732aba9SDouglas Gregor template<typename U>
f(U)69c732aba9SDouglas Gregor void X0<U>::f(U) { }
70dc6d2c3cSAnders Carlsson
test_x0(X0<int> xi)71c732aba9SDouglas Gregor void test_x0(X0<int> xi) {
72c732aba9SDouglas Gregor xi.f();
73c732aba9SDouglas Gregor xi.f(17);
74c732aba9SDouglas Gregor }
75c732aba9SDouglas Gregor
76*e7cbb3edSCharles Li struct NotDefaultConstructible { // expected-note 2 {{candidate constructor (the implicit copy constructor) not viable}}
77*e7cbb3edSCharles Li #if __cplusplus >= 201103L // C++11 or later
78*e7cbb3edSCharles Li // expected-note@-2 2 {{candidate constructor (the implicit move constructor) not viable}}
79*e7cbb3edSCharles Li #endif
801bc688dcSDouglas Gregor NotDefaultConstructible(int); // expected-note 2{{candidate}}
81c732aba9SDouglas Gregor };
82c732aba9SDouglas Gregor
test_x0_not_default_constructible(X0<NotDefaultConstructible> xn)83c732aba9SDouglas Gregor void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) {
84c732aba9SDouglas Gregor xn.f(NotDefaultConstructible(17));
85c732aba9SDouglas Gregor xn.f(42);
86c732aba9SDouglas Gregor xn.f(); // expected-note{{in instantiation of default function argument}}
87c732aba9SDouglas Gregor }
8864621e6eSDouglas Gregor
8964621e6eSDouglas Gregor template<typename T>
9064621e6eSDouglas Gregor struct X1 {
9164621e6eSDouglas Gregor typedef T value_type;
9264621e6eSDouglas Gregor X1(const value_type& value = value_type());
9364621e6eSDouglas Gregor };
9464621e6eSDouglas Gregor
test_X1()9564621e6eSDouglas Gregor void test_X1() {
9664621e6eSDouglas Gregor X1<int> x1;
9764621e6eSDouglas Gregor }
98561f7938SAnders Carlsson
991bc688dcSDouglas Gregor template<typename T>
1001bc688dcSDouglas Gregor struct X2 {
1011bc688dcSDouglas Gregor void operator()(T = T()); // expected-error{{no matching}}
1021bc688dcSDouglas Gregor };
1031bc688dcSDouglas Gregor
test_x2(X2<int> x2i,X2<NotDefaultConstructible> x2n)1041bc688dcSDouglas Gregor void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) {
1051bc688dcSDouglas Gregor x2i();
1061bc688dcSDouglas Gregor x2i(17);
1071bc688dcSDouglas Gregor x2n(NotDefaultConstructible(17));
1081bc688dcSDouglas Gregor x2n(); // expected-note{{in instantiation of default function argument}}
1091bc688dcSDouglas Gregor }
1101bc688dcSDouglas Gregor
111561f7938SAnders Carlsson // PR5283
112561f7938SAnders Carlsson namespace PR5283 {
113561f7938SAnders Carlsson template<typename T> struct A {
1144f4946aaSDouglas Gregor A(T = 1); // expected-error 3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'int'}} \
1154f4946aaSDouglas Gregor // expected-note 3{{passing argument to parameter here}}
116561f7938SAnders Carlsson };
117561f7938SAnders Carlsson
118561f7938SAnders Carlsson struct B : A<int*> {
119561f7938SAnders Carlsson B();
120561f7938SAnders Carlsson };
B()121561f7938SAnders Carlsson B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
122561f7938SAnders Carlsson
123561f7938SAnders Carlsson struct C : virtual A<int*> {
124561f7938SAnders Carlsson C();
125561f7938SAnders Carlsson };
C()126561f7938SAnders Carlsson C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
127561f7938SAnders Carlsson
128561f7938SAnders Carlsson struct D {
129561f7938SAnders Carlsson D();
130561f7938SAnders Carlsson
131561f7938SAnders Carlsson A<int*> a;
132561f7938SAnders Carlsson };
D()133561f7938SAnders Carlsson D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
134561f7938SAnders Carlsson }
13514236c8eSSebastian Redl
13614236c8eSSebastian Redl // PR5301
13714236c8eSSebastian Redl namespace pr5301 {
13814236c8eSSebastian Redl void f(int, int = 0);
13914236c8eSSebastian Redl
14014236c8eSSebastian Redl template <typename T>
14114236c8eSSebastian Redl void g(T, T = 0);
14214236c8eSSebastian Redl
14314236c8eSSebastian Redl template <int I>
14414236c8eSSebastian Redl void i(int a = I);
14514236c8eSSebastian Redl
14614236c8eSSebastian Redl template <typename T>
h(T t)14714236c8eSSebastian Redl void h(T t) {
14814236c8eSSebastian Redl f(0);
14914236c8eSSebastian Redl g(1);
15014236c8eSSebastian Redl g(t);
15114236c8eSSebastian Redl i<2>();
15214236c8eSSebastian Redl }
15314236c8eSSebastian Redl
test()15414236c8eSSebastian Redl void test() {
15514236c8eSSebastian Redl h(0);
15614236c8eSSebastian Redl }
15714236c8eSSebastian Redl }
1581bc688dcSDouglas Gregor
15925ab25f3SDouglas Gregor // PR5810
16025ab25f3SDouglas Gregor namespace PR5810 {
16125ab25f3SDouglas Gregor template<typename T>
16225ab25f3SDouglas Gregor struct allocator {
allocatorPR5810::allocator163a92409c3SChandler Carruth allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}}
16425ab25f3SDouglas Gregor };
16525ab25f3SDouglas Gregor
16625ab25f3SDouglas Gregor template<typename T>
16725ab25f3SDouglas Gregor struct vector {
vectorPR5810::vector168033f675fSDouglas Gregor vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}}
16925ab25f3SDouglas Gregor };
17025ab25f3SDouglas Gregor
17125ab25f3SDouglas Gregor struct A { };
172033f675fSDouglas Gregor struct B { };
17325ab25f3SDouglas Gregor
17425ab25f3SDouglas Gregor template<typename>
FilterVTs()17525ab25f3SDouglas Gregor void FilterVTs() {
17625ab25f3SDouglas Gregor vector<A> Result;
17725ab25f3SDouglas Gregor }
17825ab25f3SDouglas Gregor
f()17925ab25f3SDouglas Gregor void f() {
18025ab25f3SDouglas Gregor vector<A> Result;
18125ab25f3SDouglas Gregor }
182033f675fSDouglas Gregor
183033f675fSDouglas Gregor template<typename T>
184033f675fSDouglas Gregor struct X {
185033f675fSDouglas Gregor vector<B> bs;
XPR5810::X186033f675fSDouglas Gregor X() { }
187033f675fSDouglas Gregor };
188033f675fSDouglas Gregor
f2()189033f675fSDouglas Gregor void f2() {
190033f675fSDouglas Gregor X<float> x; // expected-note{{member function}}
191033f675fSDouglas Gregor }
19225ab25f3SDouglas Gregor }
1938c702534SDouglas Gregor
1948c702534SDouglas Gregor template<typename T> void f4(T, int = 17);
1958c702534SDouglas Gregor template<> void f4<int>(int, int);
1968c702534SDouglas Gregor
f4_test(int i)1978c702534SDouglas Gregor void f4_test(int i) {
1988c702534SDouglas Gregor f4(i);
1998c702534SDouglas Gregor }
200d984815eSDouglas Gregor
201d984815eSDouglas Gregor // Instantiate for initialization
202d984815eSDouglas Gregor namespace InstForInit {
203d984815eSDouglas Gregor template<typename T>
204d984815eSDouglas Gregor struct Ptr {
205d984815eSDouglas Gregor typedef T* type;
206d984815eSDouglas Gregor Ptr(type);
207d984815eSDouglas Gregor };
208d984815eSDouglas Gregor
209d984815eSDouglas Gregor template<typename T>
210d984815eSDouglas Gregor struct Holder {
211d984815eSDouglas Gregor Holder(int i, Ptr<T> ptr = 0);
212d984815eSDouglas Gregor };
213d984815eSDouglas Gregor
test_holder(int i)214d984815eSDouglas Gregor void test_holder(int i) {
215d984815eSDouglas Gregor Holder<int> h(i);
216d984815eSDouglas Gregor }
217d984815eSDouglas Gregor };
2188a01b2a3SDouglas Gregor
2198a01b2a3SDouglas Gregor namespace PR5810b {
2208a01b2a3SDouglas Gregor template<typename T>
broken()2218a01b2a3SDouglas Gregor T broken() {
2228a01b2a3SDouglas Gregor T t;
2238a01b2a3SDouglas Gregor double**** not_it = t;
2248a01b2a3SDouglas Gregor }
2258a01b2a3SDouglas Gregor
2268a01b2a3SDouglas Gregor void f(int = broken<int>());
g()2278a01b2a3SDouglas Gregor void g() { f(17); }
2288a01b2a3SDouglas Gregor }
2298a01b2a3SDouglas Gregor
23032b3de51SDouglas Gregor namespace PR5810c {
23132b3de51SDouglas Gregor template<typename T>
23232b3de51SDouglas Gregor struct X {
XPR5810c::X23332b3de51SDouglas Gregor X() {
23432b3de51SDouglas Gregor T t;
23532b3de51SDouglas Gregor double *****p = t; // expected-error{{cannot initialize a variable of type 'double *****' with an lvalue of type 'int'}}
23632b3de51SDouglas Gregor }
XPR5810c::X23732b3de51SDouglas Gregor X(const X&) { }
23832b3de51SDouglas Gregor };
23932b3de51SDouglas Gregor
24032b3de51SDouglas Gregor struct Y : X<int> { // expected-note{{instantiation of}}
24132b3de51SDouglas Gregor };
24232b3de51SDouglas Gregor
24332b3de51SDouglas Gregor void f(Y y = Y());
24432b3de51SDouglas Gregor
g()24532b3de51SDouglas Gregor void g() { f(); }
24632b3de51SDouglas Gregor }
24732b3de51SDouglas Gregor
2488a01b2a3SDouglas Gregor namespace PR8127 {
2498a01b2a3SDouglas Gregor template< typename T > class PointerClass {
2508a01b2a3SDouglas Gregor public:
PointerClass(T * object_p)2518a01b2a3SDouglas Gregor PointerClass( T * object_p ) : p_( object_p ) {
2528a01b2a3SDouglas Gregor p_->acquire();
2538a01b2a3SDouglas Gregor }
2548a01b2a3SDouglas Gregor private:
2558a01b2a3SDouglas Gregor T * p_;
2568a01b2a3SDouglas Gregor };
2578a01b2a3SDouglas Gregor
2588a01b2a3SDouglas Gregor class ExternallyImplementedClass;
2598a01b2a3SDouglas Gregor
2608a01b2a3SDouglas Gregor class MyClass {
2618a01b2a3SDouglas Gregor void foo( PointerClass<ExternallyImplementedClass> = 0 );
2628a01b2a3SDouglas Gregor };
2638a01b2a3SDouglas Gregor }
2646ed2feebSDouglas Gregor
2656ed2feebSDouglas Gregor namespace rdar8427926 {
2666ed2feebSDouglas Gregor template<typename T>
2676ed2feebSDouglas Gregor struct Boom {
~Boomrdar8427926::Boom2686ed2feebSDouglas Gregor ~Boom() {
2696ed2feebSDouglas Gregor T t;
2706ed2feebSDouglas Gregor double *******ptr = t; // expected-error 2{{cannot initialize}}
2716ed2feebSDouglas Gregor }
2726ed2feebSDouglas Gregor };
2736ed2feebSDouglas Gregor
2746ed2feebSDouglas Gregor Boom<float> *bfp;
2756ed2feebSDouglas Gregor
2766ed2feebSDouglas Gregor struct X {
frdar8427926::X2776ed2feebSDouglas Gregor void f(Boom<int> = Boom<int>()) { } // expected-note{{requested here}}
2786ed2feebSDouglas Gregor void g(int x = (delete bfp, 0)); // expected-note{{requested here}}
2796ed2feebSDouglas Gregor };
2806ed2feebSDouglas Gregor
test(X * x)2816ed2feebSDouglas Gregor void test(X *x) {
2826ed2feebSDouglas Gregor x->f();
2836ed2feebSDouglas Gregor x->g();
2846ed2feebSDouglas Gregor }
2856ed2feebSDouglas Gregor }
286f0873f4cSDouglas Gregor
287f0873f4cSDouglas Gregor namespace PR8401 {
288f0873f4cSDouglas Gregor template<typename T>
289f0873f4cSDouglas Gregor struct A {
APR8401::A290f0873f4cSDouglas Gregor A() { T* x = 1; } // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
291f0873f4cSDouglas Gregor };
292f0873f4cSDouglas Gregor
293f0873f4cSDouglas Gregor template<typename T>
294f0873f4cSDouglas Gregor struct B {
295f0873f4cSDouglas Gregor B(const A<T>& a = A<T>()); // expected-note{{in instantiation of}}
296f0873f4cSDouglas Gregor };
297f0873f4cSDouglas Gregor
298f0873f4cSDouglas Gregor void f(B<int> b = B<int>());
299f0873f4cSDouglas Gregor
g()300f0873f4cSDouglas Gregor void g() {
301f0873f4cSDouglas Gregor f();
302f0873f4cSDouglas Gregor }
303f0873f4cSDouglas Gregor }
304c25372bbSEli Friedman
305c25372bbSEli Friedman namespace PR12581 {
306c25372bbSEli Friedman const int a = 0;
307c25372bbSEli Friedman template < typename > struct A;
308c25372bbSEli Friedman template < typename MatrixType, int =
309c25372bbSEli Friedman A < MatrixType >::Flags ? : A < MatrixType >::Flags & a > class B;
310c25372bbSEli Friedman void
fn1()311c25372bbSEli Friedman fn1 ()
312c25372bbSEli Friedman {
313c25372bbSEli Friedman }
314c25372bbSEli Friedman }
31590633e39SBenjamin Kramer
31690633e39SBenjamin Kramer namespace PR13758 {
31790633e39SBenjamin Kramer template <typename T> struct move_from {
318dd2ca571SRichard Smith T invalid;
31990633e39SBenjamin Kramer };
32090633e39SBenjamin Kramer template <class K>
32190633e39SBenjamin Kramer struct unordered_map {
32290633e39SBenjamin Kramer explicit unordered_map(int n = 42);
32390633e39SBenjamin Kramer unordered_map(move_from<K> other);
32490633e39SBenjamin Kramer };
32590633e39SBenjamin Kramer template<typename T>
StripedHashTable()32690633e39SBenjamin Kramer void StripedHashTable() {
327dd2ca571SRichard Smith new unordered_map<void>();
328dd2ca571SRichard Smith new unordered_map<void>;
32990633e39SBenjamin Kramer }
tt()33090633e39SBenjamin Kramer void tt() {
331dd2ca571SRichard Smith StripedHashTable<int>();
33290633e39SBenjamin Kramer }
33390633e39SBenjamin Kramer }
334