blob: 4c2f5465dc8bd2033fc893e256a044cfad4ee4fe [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Anders Carlsson9351c172009-08-25 03:18:48 +00002
Douglas Gregord85cef52009-09-17 19:51:30 +00003template<typename T>
4class C { C(int a0 = 0); };
5
6template<>
7C<char>::C(int a0);
8
Anders Carlsson9351c172009-08-25 03:18:48 +00009struct S { };
10
11template<typename T> void f1(T a, T b = 10) { } // expected-error{{cannot initialize 'b' with an rvalue of type 'int'}}
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 ('struct S' and 'struct S')}}
16
17void g() {
18 f1(10);
Anders Carlsson25cae7f2009-09-05 05:14:19 +000019 f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<struct S>' required here}}
Anders Carlsson9351c172009-08-25 03:18:48 +000020
21 f2(10);
22 f2(S());
23
24 f3(10);
Anders Carlsson25cae7f2009-09-05 05:14:19 +000025 f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<struct S>' required here}}
Anders Carlsson9351c172009-08-25 03:18:48 +000026}
Anders Carlsson8644aec2009-08-25 13:07:08 +000027
28template<typename T> struct F {
Anders Carlsson21e1c4e2009-09-06 16:54:02 +000029 F(T t = 10); // expected-error{{cannot initialize 't' with an rvalue of type 'int'}}
Anders Carlsson6bc107b2009-09-05 05:38:54 +000030 void f(T t = 10); // expected-error{{cannot initialize 't' with an rvalue of type 'int'}}
Anders Carlsson8644aec2009-08-25 13:07:08 +000031};
32
Douglas Gregor0b84a532009-08-25 15:24:38 +000033struct FD : F<int> { };
34
Anders Carlsson8644aec2009-08-25 13:07:08 +000035void g2() {
Anders Carlsson25cae7f2009-09-05 05:14:19 +000036 F<int> f;
Douglas Gregor0b84a532009-08-25 15:24:38 +000037 FD fd;
Anders Carlsson8644aec2009-08-25 13:07:08 +000038}
Anders Carlsson5653ca52009-08-25 13:46:13 +000039
Anders Carlsson6bc107b2009-09-05 05:38:54 +000040void g3(F<int> f, F<struct S> s) {
41 f.f();
42 s.f(); // expected-note{{in instantiation of default function argument expression for 'f<struct S>' required here}}
Anders Carlsson21e1c4e2009-09-06 16:54:02 +000043
44 F<int> f2;
45 F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<struct S>' required here}}
Anders Carlsson6bc107b2009-09-05 05:38:54 +000046}
47
Anders Carlsson5653ca52009-08-25 13:46:13 +000048template<typename T> struct G {
Anders Carlsson25cae7f2009-09-05 05:14:19 +000049 G(T) {}
Anders Carlsson5653ca52009-08-25 13:46:13 +000050};
51
52void s(G<int> flags = 10) { }
53
Douglas Gregor6cc15182009-09-11 18:44:32 +000054// Test default arguments
55template<typename T>
56struct X0 {
57 void f(T = T()); // expected-error{{no matching}}
58};
Anders Carlsson5653ca52009-08-25 13:46:13 +000059
Douglas Gregor6cc15182009-09-11 18:44:32 +000060template<typename U>
61void X0<U>::f(U) { }
Anders Carlsson6bc107b2009-09-05 05:38:54 +000062
Douglas Gregor6cc15182009-09-11 18:44:32 +000063void test_x0(X0<int> xi) {
64 xi.f();
65 xi.f(17);
66}
67
Douglas Gregord47c47d2009-11-09 19:27:57 +000068struct NotDefaultConstructible { // expected-note 2{{candidate}}
69 NotDefaultConstructible(int); // expected-note 2{{candidate}}
Douglas Gregor6cc15182009-09-11 18:44:32 +000070};
71
72void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) {
73 xn.f(NotDefaultConstructible(17));
74 xn.f(42);
75 xn.f(); // expected-note{{in instantiation of default function argument}}
76}
Douglas Gregore95b4092009-09-16 18:34:49 +000077
78template<typename T>
79struct X1 {
80 typedef T value_type;
81 X1(const value_type& value = value_type());
82};
83
84void test_X1() {
85 X1<int> x1;
86}
Anders Carlsson0ebb6d32009-10-29 15:46:07 +000087
Douglas Gregord47c47d2009-11-09 19:27:57 +000088template<typename T>
89struct X2 {
90 void operator()(T = T()); // expected-error{{no matching}}
91};
92
93void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) {
94 x2i();
95 x2i(17);
96 x2n(NotDefaultConstructible(17));
97 x2n(); // expected-note{{in instantiation of default function argument}}
98}
99
Anders Carlsson0ebb6d32009-10-29 15:46:07 +0000100// PR5283
101namespace PR5283 {
102template<typename T> struct A {
103 A(T = 1); // expected-error 3 {{incompatible type initializing 'int', expected 'int *'}}
104};
105
106struct B : A<int*> {
107 B();
108};
109B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
110
111struct C : virtual A<int*> {
112 C();
113};
114C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
115
116struct D {
117 D();
118
119 A<int*> a;
120};
121D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
122}
Sebastian Redla29e51b2009-11-08 13:56:19 +0000123
124// PR5301
125namespace pr5301 {
126 void f(int, int = 0);
127
128 template <typename T>
129 void g(T, T = 0);
130
131 template <int I>
132 void i(int a = I);
133
134 template <typename T>
135 void h(T t) {
136 f(0);
137 g(1);
138 g(t);
139 i<2>();
140 }
141
142 void test() {
143 h(0);
144 }
145}
Douglas Gregord47c47d2009-11-09 19:27:57 +0000146