xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/default-expr-arguments.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc template<typename T>
3*f4a2713aSLionel Sambuc class C { C(int a0 = 0); };
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc template<>
6*f4a2713aSLionel Sambuc C<char>::C(int a0);
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
9*f4a2713aSLionel Sambuc 
f1(T a,T b=10)10*f4a2713aSLionel Sambuc template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \
11*f4a2713aSLionel Sambuc // expected-note{{passing argument to parameter 'b' here}}
12*f4a2713aSLionel Sambuc 
f2(T a,T b=T ())13*f4a2713aSLionel Sambuc template<typename T> void f2(T a, T b = T()) { }
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}}
16*f4a2713aSLionel Sambuc 
g()17*f4a2713aSLionel Sambuc void g() {
18*f4a2713aSLionel Sambuc   f1(10);
19*f4a2713aSLionel Sambuc   f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}}
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc   f2(10);
22*f4a2713aSLionel Sambuc   f2(S());
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc   f3(10);
25*f4a2713aSLionel Sambuc   f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}}
26*f4a2713aSLionel Sambuc }
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc template<typename T> struct F {
29*f4a2713aSLionel Sambuc   F(T t = 10); // expected-error{{no viable conversion}} \
30*f4a2713aSLionel Sambuc   // expected-note{{passing argument to parameter 't' here}}
31*f4a2713aSLionel Sambuc   void f(T t = 10); // expected-error{{no viable conversion}} \
32*f4a2713aSLionel Sambuc   // expected-note{{passing argument to parameter 't' here}}
33*f4a2713aSLionel Sambuc };
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc struct FD : F<int> { };
36*f4a2713aSLionel Sambuc 
g2()37*f4a2713aSLionel Sambuc void g2() {
38*f4a2713aSLionel Sambuc   F<int> f;
39*f4a2713aSLionel Sambuc   FD fd;
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
g3(F<int> f,F<struct S> s)42*f4a2713aSLionel Sambuc void g3(F<int> f, F<struct S> s) {
43*f4a2713aSLionel Sambuc   f.f();
44*f4a2713aSLionel Sambuc   s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}}
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc   F<int> f2;
47*f4a2713aSLionel Sambuc   F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}}
48*f4a2713aSLionel Sambuc }
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc template<typename T> struct G {
GG51*f4a2713aSLionel Sambuc   G(T) {}
52*f4a2713aSLionel Sambuc };
53*f4a2713aSLionel Sambuc 
s(G<int> flags=10)54*f4a2713aSLionel Sambuc void s(G<int> flags = 10) { }
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc // Test default arguments
57*f4a2713aSLionel Sambuc template<typename T>
58*f4a2713aSLionel Sambuc struct X0 {
59*f4a2713aSLionel Sambuc   void f(T = T()); // expected-error{{no matching}}
60*f4a2713aSLionel Sambuc };
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc template<typename U>
f(U)63*f4a2713aSLionel Sambuc void X0<U>::f(U) { }
64*f4a2713aSLionel Sambuc 
test_x0(X0<int> xi)65*f4a2713aSLionel Sambuc void test_x0(X0<int> xi) {
66*f4a2713aSLionel Sambuc   xi.f();
67*f4a2713aSLionel Sambuc   xi.f(17);
68*f4a2713aSLionel Sambuc }
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc struct NotDefaultConstructible { // expected-note 2{{candidate}}
71*f4a2713aSLionel Sambuc   NotDefaultConstructible(int); // expected-note 2{{candidate}}
72*f4a2713aSLionel Sambuc };
73*f4a2713aSLionel Sambuc 
test_x0_not_default_constructible(X0<NotDefaultConstructible> xn)74*f4a2713aSLionel Sambuc void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) {
75*f4a2713aSLionel Sambuc   xn.f(NotDefaultConstructible(17));
76*f4a2713aSLionel Sambuc   xn.f(42);
77*f4a2713aSLionel Sambuc   xn.f(); // expected-note{{in instantiation of default function argument}}
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc template<typename T>
81*f4a2713aSLionel Sambuc struct X1 {
82*f4a2713aSLionel Sambuc   typedef T value_type;
83*f4a2713aSLionel Sambuc   X1(const value_type& value = value_type());
84*f4a2713aSLionel Sambuc };
85*f4a2713aSLionel Sambuc 
test_X1()86*f4a2713aSLionel Sambuc void test_X1() {
87*f4a2713aSLionel Sambuc   X1<int> x1;
88*f4a2713aSLionel Sambuc }
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc template<typename T>
91*f4a2713aSLionel Sambuc struct X2 {
92*f4a2713aSLionel Sambuc   void operator()(T = T()); // expected-error{{no matching}}
93*f4a2713aSLionel Sambuc };
94*f4a2713aSLionel Sambuc 
test_x2(X2<int> x2i,X2<NotDefaultConstructible> x2n)95*f4a2713aSLionel Sambuc void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) {
96*f4a2713aSLionel Sambuc   x2i();
97*f4a2713aSLionel Sambuc   x2i(17);
98*f4a2713aSLionel Sambuc   x2n(NotDefaultConstructible(17));
99*f4a2713aSLionel Sambuc   x2n(); // expected-note{{in instantiation of default function argument}}
100*f4a2713aSLionel Sambuc }
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc // PR5283
103*f4a2713aSLionel Sambuc namespace PR5283 {
104*f4a2713aSLionel Sambuc template<typename T> struct A {
105*f4a2713aSLionel Sambuc   A(T = 1); // expected-error 3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'int'}} \
106*f4a2713aSLionel Sambuc   // expected-note 3{{passing argument to parameter here}}
107*f4a2713aSLionel Sambuc };
108*f4a2713aSLionel Sambuc 
109*f4a2713aSLionel Sambuc struct B : A<int*> {
110*f4a2713aSLionel Sambuc   B();
111*f4a2713aSLionel Sambuc };
B()112*f4a2713aSLionel Sambuc B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc struct C : virtual A<int*> {
115*f4a2713aSLionel Sambuc   C();
116*f4a2713aSLionel Sambuc };
C()117*f4a2713aSLionel Sambuc C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
118*f4a2713aSLionel Sambuc 
119*f4a2713aSLionel Sambuc struct D {
120*f4a2713aSLionel Sambuc   D();
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc   A<int*> a;
123*f4a2713aSLionel Sambuc };
D()124*f4a2713aSLionel Sambuc D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
125*f4a2713aSLionel Sambuc }
126*f4a2713aSLionel Sambuc 
127*f4a2713aSLionel Sambuc // PR5301
128*f4a2713aSLionel Sambuc namespace pr5301 {
129*f4a2713aSLionel Sambuc   void f(int, int = 0);
130*f4a2713aSLionel Sambuc 
131*f4a2713aSLionel Sambuc   template <typename T>
132*f4a2713aSLionel Sambuc   void g(T, T = 0);
133*f4a2713aSLionel Sambuc 
134*f4a2713aSLionel Sambuc   template <int I>
135*f4a2713aSLionel Sambuc   void i(int a = I);
136*f4a2713aSLionel Sambuc 
137*f4a2713aSLionel Sambuc   template <typename T>
h(T t)138*f4a2713aSLionel Sambuc   void h(T t) {
139*f4a2713aSLionel Sambuc     f(0);
140*f4a2713aSLionel Sambuc     g(1);
141*f4a2713aSLionel Sambuc     g(t);
142*f4a2713aSLionel Sambuc     i<2>();
143*f4a2713aSLionel Sambuc   }
144*f4a2713aSLionel Sambuc 
test()145*f4a2713aSLionel Sambuc   void test() {
146*f4a2713aSLionel Sambuc     h(0);
147*f4a2713aSLionel Sambuc   }
148*f4a2713aSLionel Sambuc }
149*f4a2713aSLionel Sambuc 
150*f4a2713aSLionel Sambuc // PR5810
151*f4a2713aSLionel Sambuc namespace PR5810 {
152*f4a2713aSLionel Sambuc   template<typename T>
153*f4a2713aSLionel Sambuc   struct allocator {
allocatorPR5810::allocator154*f4a2713aSLionel Sambuc     allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}}
155*f4a2713aSLionel Sambuc   };
156*f4a2713aSLionel Sambuc 
157*f4a2713aSLionel Sambuc   template<typename T>
158*f4a2713aSLionel Sambuc   struct vector {
vectorPR5810::vector159*f4a2713aSLionel Sambuc     vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}}
160*f4a2713aSLionel Sambuc   };
161*f4a2713aSLionel Sambuc 
162*f4a2713aSLionel Sambuc   struct A { };
163*f4a2713aSLionel Sambuc   struct B { };
164*f4a2713aSLionel Sambuc 
165*f4a2713aSLionel Sambuc   template<typename>
FilterVTs()166*f4a2713aSLionel Sambuc   void FilterVTs() {
167*f4a2713aSLionel Sambuc     vector<A> Result;
168*f4a2713aSLionel Sambuc   }
169*f4a2713aSLionel Sambuc 
f()170*f4a2713aSLionel Sambuc   void f() {
171*f4a2713aSLionel Sambuc     vector<A> Result;
172*f4a2713aSLionel Sambuc   }
173*f4a2713aSLionel Sambuc 
174*f4a2713aSLionel Sambuc   template<typename T>
175*f4a2713aSLionel Sambuc   struct X {
176*f4a2713aSLionel Sambuc     vector<B> bs;
XPR5810::X177*f4a2713aSLionel Sambuc     X() { }
178*f4a2713aSLionel Sambuc   };
179*f4a2713aSLionel Sambuc 
f2()180*f4a2713aSLionel Sambuc   void f2() {
181*f4a2713aSLionel Sambuc     X<float> x; // expected-note{{member function}}
182*f4a2713aSLionel Sambuc   }
183*f4a2713aSLionel Sambuc }
184*f4a2713aSLionel Sambuc 
185*f4a2713aSLionel Sambuc template<typename T> void f4(T, int = 17);
186*f4a2713aSLionel Sambuc template<> void f4<int>(int, int);
187*f4a2713aSLionel Sambuc 
f4_test(int i)188*f4a2713aSLionel Sambuc void f4_test(int i) {
189*f4a2713aSLionel Sambuc   f4(i);
190*f4a2713aSLionel Sambuc }
191*f4a2713aSLionel Sambuc 
192*f4a2713aSLionel Sambuc // Instantiate for initialization
193*f4a2713aSLionel Sambuc namespace InstForInit {
194*f4a2713aSLionel Sambuc   template<typename T>
195*f4a2713aSLionel Sambuc   struct Ptr {
196*f4a2713aSLionel Sambuc     typedef T* type;
197*f4a2713aSLionel Sambuc     Ptr(type);
198*f4a2713aSLionel Sambuc   };
199*f4a2713aSLionel Sambuc 
200*f4a2713aSLionel Sambuc   template<typename T>
201*f4a2713aSLionel Sambuc   struct Holder {
202*f4a2713aSLionel Sambuc     Holder(int i, Ptr<T> ptr = 0);
203*f4a2713aSLionel Sambuc   };
204*f4a2713aSLionel Sambuc 
test_holder(int i)205*f4a2713aSLionel Sambuc   void test_holder(int i) {
206*f4a2713aSLionel Sambuc     Holder<int> h(i);
207*f4a2713aSLionel Sambuc   }
208*f4a2713aSLionel Sambuc };
209*f4a2713aSLionel Sambuc 
210*f4a2713aSLionel Sambuc namespace PR5810b {
211*f4a2713aSLionel Sambuc   template<typename T>
broken()212*f4a2713aSLionel Sambuc   T broken() {
213*f4a2713aSLionel Sambuc     T t;
214*f4a2713aSLionel Sambuc     double**** not_it = t;
215*f4a2713aSLionel Sambuc   }
216*f4a2713aSLionel Sambuc 
217*f4a2713aSLionel Sambuc   void f(int = broken<int>());
g()218*f4a2713aSLionel Sambuc   void g() { f(17); }
219*f4a2713aSLionel Sambuc }
220*f4a2713aSLionel Sambuc 
221*f4a2713aSLionel Sambuc namespace PR5810c {
222*f4a2713aSLionel Sambuc   template<typename T>
223*f4a2713aSLionel Sambuc   struct X {
XPR5810c::X224*f4a2713aSLionel Sambuc     X() {
225*f4a2713aSLionel Sambuc       T t;
226*f4a2713aSLionel Sambuc       double *****p = t; // expected-error{{cannot initialize a variable of type 'double *****' with an lvalue of type 'int'}}
227*f4a2713aSLionel Sambuc     }
XPR5810c::X228*f4a2713aSLionel Sambuc     X(const X&) { }
229*f4a2713aSLionel Sambuc   };
230*f4a2713aSLionel Sambuc 
231*f4a2713aSLionel Sambuc   struct Y : X<int> { // expected-note{{instantiation of}}
232*f4a2713aSLionel Sambuc   };
233*f4a2713aSLionel Sambuc 
234*f4a2713aSLionel Sambuc   void f(Y y = Y());
235*f4a2713aSLionel Sambuc 
g()236*f4a2713aSLionel Sambuc   void g() { f(); }
237*f4a2713aSLionel Sambuc }
238*f4a2713aSLionel Sambuc 
239*f4a2713aSLionel Sambuc namespace PR8127 {
240*f4a2713aSLionel Sambuc   template< typename T > class PointerClass {
241*f4a2713aSLionel Sambuc   public:
PointerClass(T * object_p)242*f4a2713aSLionel Sambuc     PointerClass( T * object_p ) : p_( object_p ) {
243*f4a2713aSLionel Sambuc       p_->acquire();
244*f4a2713aSLionel Sambuc     }
245*f4a2713aSLionel Sambuc   private:
246*f4a2713aSLionel Sambuc     T * p_;
247*f4a2713aSLionel Sambuc   };
248*f4a2713aSLionel Sambuc 
249*f4a2713aSLionel Sambuc   class ExternallyImplementedClass;
250*f4a2713aSLionel Sambuc 
251*f4a2713aSLionel Sambuc   class MyClass {
252*f4a2713aSLionel Sambuc     void foo( PointerClass<ExternallyImplementedClass> = 0 );
253*f4a2713aSLionel Sambuc   };
254*f4a2713aSLionel Sambuc }
255*f4a2713aSLionel Sambuc 
256*f4a2713aSLionel Sambuc namespace rdar8427926 {
257*f4a2713aSLionel Sambuc   template<typename T>
258*f4a2713aSLionel Sambuc   struct Boom {
~Boomrdar8427926::Boom259*f4a2713aSLionel Sambuc     ~Boom() {
260*f4a2713aSLionel Sambuc       T t;
261*f4a2713aSLionel Sambuc       double *******ptr = t; // expected-error 2{{cannot initialize}}
262*f4a2713aSLionel Sambuc     }
263*f4a2713aSLionel Sambuc   };
264*f4a2713aSLionel Sambuc 
265*f4a2713aSLionel Sambuc   Boom<float> *bfp;
266*f4a2713aSLionel Sambuc 
267*f4a2713aSLionel Sambuc   struct X {
frdar8427926::X268*f4a2713aSLionel Sambuc     void f(Boom<int> = Boom<int>()) { } // expected-note{{requested here}}
269*f4a2713aSLionel Sambuc     void g(int x = (delete bfp, 0)); // expected-note{{requested here}}
270*f4a2713aSLionel Sambuc   };
271*f4a2713aSLionel Sambuc 
test(X * x)272*f4a2713aSLionel Sambuc   void test(X *x) {
273*f4a2713aSLionel Sambuc     x->f();
274*f4a2713aSLionel Sambuc     x->g();
275*f4a2713aSLionel Sambuc   }
276*f4a2713aSLionel Sambuc }
277*f4a2713aSLionel Sambuc 
278*f4a2713aSLionel Sambuc namespace PR8401 {
279*f4a2713aSLionel Sambuc   template<typename T>
280*f4a2713aSLionel Sambuc   struct A {
APR8401::A281*f4a2713aSLionel Sambuc     A() { T* x = 1; } // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
282*f4a2713aSLionel Sambuc   };
283*f4a2713aSLionel Sambuc 
284*f4a2713aSLionel Sambuc   template<typename T>
285*f4a2713aSLionel Sambuc   struct B {
286*f4a2713aSLionel Sambuc     B(const A<T>& a = A<T>()); // expected-note{{in instantiation of}}
287*f4a2713aSLionel Sambuc   };
288*f4a2713aSLionel Sambuc 
289*f4a2713aSLionel Sambuc   void f(B<int> b = B<int>());
290*f4a2713aSLionel Sambuc 
g()291*f4a2713aSLionel Sambuc   void g() {
292*f4a2713aSLionel Sambuc     f();
293*f4a2713aSLionel Sambuc   }
294*f4a2713aSLionel Sambuc }
295*f4a2713aSLionel Sambuc 
296*f4a2713aSLionel Sambuc namespace PR12581 {
297*f4a2713aSLionel Sambuc   const int a = 0;
298*f4a2713aSLionel Sambuc   template < typename > struct A;
299*f4a2713aSLionel Sambuc   template < typename MatrixType, int =
300*f4a2713aSLionel Sambuc   A < MatrixType >::Flags ? : A < MatrixType >::Flags & a > class B;
301*f4a2713aSLionel Sambuc   void
fn1()302*f4a2713aSLionel Sambuc   fn1 ()
303*f4a2713aSLionel Sambuc   {
304*f4a2713aSLionel Sambuc   }
305*f4a2713aSLionel Sambuc }
306*f4a2713aSLionel Sambuc 
307*f4a2713aSLionel Sambuc namespace PR13758 {
308*f4a2713aSLionel Sambuc   template <typename T> struct move_from {
309*f4a2713aSLionel Sambuc     T invalid;
310*f4a2713aSLionel Sambuc   };
311*f4a2713aSLionel Sambuc   template <class K>
312*f4a2713aSLionel Sambuc   struct unordered_map {
313*f4a2713aSLionel Sambuc     explicit unordered_map(int n = 42);
314*f4a2713aSLionel Sambuc     unordered_map(move_from<K> other);
315*f4a2713aSLionel Sambuc   };
316*f4a2713aSLionel Sambuc   template<typename T>
StripedHashTable()317*f4a2713aSLionel Sambuc   void StripedHashTable() {
318*f4a2713aSLionel Sambuc     new unordered_map<void>();
319*f4a2713aSLionel Sambuc     new unordered_map<void>;
320*f4a2713aSLionel Sambuc   }
tt()321*f4a2713aSLionel Sambuc   void tt() {
322*f4a2713aSLionel Sambuc     StripedHashTable<int>();
323*f4a2713aSLionel Sambuc   }
324*f4a2713aSLionel Sambuc }
325