xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/default-arguments.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc template<typename T, int N = 2> struct X; // expected-note{{template is declared here}}
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc X<int, 1> *x1;
5*f4a2713aSLionel Sambuc X<int> *x2;
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc X<> *x3; // expected-error{{too few template arguments for class template 'X'}}
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc template<typename U = float, int M> struct X;
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc X<> *x4;
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc template<typename T = int> struct Z { };
14*f4a2713aSLionel Sambuc template struct Z<>;
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc // PR4362
17*f4a2713aSLionel Sambuc template<class T> struct a { };
18*f4a2713aSLionel Sambuc template<> struct a<int> { static const bool v = true; };
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc template<class T, bool = a<T>::v> struct p { }; // expected-error {{no member named 'v'}}
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc template struct p<bool>; // expected-note {{in instantiation of default argument for 'p<bool>' required here}}
23*f4a2713aSLionel Sambuc template struct p<int>;
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc // PR5187
26*f4a2713aSLionel Sambuc template<typename T, typename U>
27*f4a2713aSLionel Sambuc struct A;
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc template<typename T, typename U = T>
30*f4a2713aSLionel Sambuc struct A;
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc template<typename T, typename U>
33*f4a2713aSLionel Sambuc struct A {
34*f4a2713aSLionel Sambuc   void f(A<T>);
35*f4a2713aSLionel Sambuc };
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc template<typename T>
38*f4a2713aSLionel Sambuc struct B { };
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc template<>
41*f4a2713aSLionel Sambuc struct B<void> {
42*f4a2713aSLionel Sambuc   typedef B<void*> type;
43*f4a2713aSLionel Sambuc };
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc // Nested default arguments for template parameters.
46*f4a2713aSLionel Sambuc template<typename T> struct X1 { };
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc template<typename T>
49*f4a2713aSLionel Sambuc struct X2 {
50*f4a2713aSLionel Sambuc   template<typename U = typename X1<T>::type> // expected-error{{no type named 'type' in 'X1<int>'}} \
51*f4a2713aSLionel Sambuc                                               // expected-error{{no type named 'type' in 'X1<char>'}}
52*f4a2713aSLionel Sambuc   struct Inner1 { }; // expected-note{{template is declared here}}
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc   template<T Value = X1<T>::value> // expected-error{{no member named 'value' in 'X1<int>'}} \
55*f4a2713aSLionel Sambuc                                    // expected-error{{no member named 'value' in 'X1<char>'}}
56*f4a2713aSLionel Sambuc   struct NonType1 { }; // expected-note{{template is declared here}}
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   template<T Value>
59*f4a2713aSLionel Sambuc   struct Inner2 { };
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc   template<typename U>
62*f4a2713aSLionel Sambuc   struct Inner3 {
63*f4a2713aSLionel Sambuc     template<typename X = T, typename V = U>
64*f4a2713aSLionel Sambuc     struct VeryInner { };
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc     template<T Value1 = sizeof(T), T Value2 = sizeof(U),
67*f4a2713aSLionel Sambuc              T Value3 = Value1 + Value2>
68*f4a2713aSLionel Sambuc     struct NonType2 { };
69*f4a2713aSLionel Sambuc   };
70*f4a2713aSLionel Sambuc };
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc X2<int> x2i; // expected-note{{in instantiation of template class 'X2<int>' requested here}}
73*f4a2713aSLionel Sambuc X2<int>::Inner1<float> x2iif;
74*f4a2713aSLionel Sambuc 
75*f4a2713aSLionel Sambuc X2<int>::Inner1<> x2bad; // expected-error{{too few template arguments for class template 'Inner1'}}
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc X2<int>::NonType1<'a'> x2_nontype1;
78*f4a2713aSLionel Sambuc X2<int>::NonType1<> x2_nontype1_bad; // expected-error{{too few template arguments for class template 'NonType1'}}
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc // Check multi-level substitution into template type arguments
81*f4a2713aSLionel Sambuc X2<int>::Inner3<float>::VeryInner<> vi;
82*f4a2713aSLionel Sambuc X2<char>::Inner3<int>::NonType2<> x2_deep_nontype; // expected-note{{in instantiation of template class 'X2<char>' requested here}}
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc template<typename T, typename U>
85*f4a2713aSLionel Sambuc struct is_same { static const bool value = false; };
86*f4a2713aSLionel Sambuc 
87*f4a2713aSLionel Sambuc template<typename T>
88*f4a2713aSLionel Sambuc struct is_same<T, T> { static const bool value = true; };
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc int array1[is_same<__typeof__(vi),
91*f4a2713aSLionel Sambuc                X2<int>::Inner3<float>::VeryInner<int, float> >::value? 1 : -1];
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc int array2[is_same<__typeof(x2_deep_nontype),
94*f4a2713aSLionel Sambuc                    X2<char>::Inner3<int>::NonType2<sizeof(char), sizeof(int),
95*f4a2713aSLionel Sambuc                                     sizeof(char)+sizeof(int)> >::value? 1 : -1];
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc // Template template parameter defaults
98*f4a2713aSLionel Sambuc template<template<typename T> class X = X2> struct X3 { };
99*f4a2713aSLionel Sambuc int array3[is_same<X3<>, X3<X2> >::value? 1 : -1];
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc struct add_pointer {
102*f4a2713aSLionel Sambuc   template<typename T>
103*f4a2713aSLionel Sambuc   struct apply {
104*f4a2713aSLionel Sambuc     typedef T* type;
105*f4a2713aSLionel Sambuc   };
106*f4a2713aSLionel Sambuc };
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc template<typename T, template<typename> class X = T::template apply>
109*f4a2713aSLionel Sambuc   struct X4;
110*f4a2713aSLionel Sambuc int array4[is_same<X4<add_pointer>,
111*f4a2713aSLionel Sambuc                    X4<add_pointer, add_pointer::apply> >::value? 1 : -1];
112*f4a2713aSLionel Sambuc 
113*f4a2713aSLionel Sambuc template<int> struct X5 {}; // expected-note{{has a different type 'int'}}
114*f4a2713aSLionel Sambuc template<long> struct X5b {};
115*f4a2713aSLionel Sambuc template<typename T,
116*f4a2713aSLionel Sambuc          template<T> class B = X5> // expected-error{{template template argument has different}} \
117*f4a2713aSLionel Sambuc                                    // expected-note{{previous non-type template parameter}}
118*f4a2713aSLionel Sambuc   struct X6 {};
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc X6<int> x6a;
121*f4a2713aSLionel Sambuc X6<long> x6b; // expected-note{{while checking a default template argument}}
122*f4a2713aSLionel Sambuc X6<long, X5b> x6c;
123*f4a2713aSLionel Sambuc 
124*f4a2713aSLionel Sambuc 
125*f4a2713aSLionel Sambuc template<template<class> class X = B<int> > struct X7; // expected-error{{must be a class template}}
126*f4a2713aSLionel Sambuc 
127*f4a2713aSLionel Sambuc namespace PR9643 {
128*f4a2713aSLionel Sambuc   template<typename T> class allocator {};
129*f4a2713aSLionel Sambuc   template<typename T, typename U = allocator<T> > class vector {};
130*f4a2713aSLionel Sambuc 
131*f4a2713aSLionel Sambuc   template<template<typename U, typename = allocator<U> > class container,
132*f4a2713aSLionel Sambuc            typename DT>
initializer(const DT & d)133*f4a2713aSLionel Sambuc   container<DT> initializer(const DT& d) {
134*f4a2713aSLionel Sambuc     return container<DT>();
135*f4a2713aSLionel Sambuc   }
136*f4a2713aSLionel Sambuc 
f()137*f4a2713aSLionel Sambuc   void f() {
138*f4a2713aSLionel Sambuc     vector<int, allocator<int> > v = initializer<vector>(5);
139*f4a2713aSLionel Sambuc   }
140*f4a2713aSLionel Sambuc }
141*f4a2713aSLionel Sambuc 
142*f4a2713aSLionel Sambuc namespace PR16288 {
143*f4a2713aSLionel Sambuc   template<typename X>
144*f4a2713aSLionel Sambuc   struct S {
145*f4a2713aSLionel Sambuc     template<typename T = int, typename U> // expected-warning {{C++11}}
146*f4a2713aSLionel Sambuc     void f();
147*f4a2713aSLionel Sambuc   };
148*f4a2713aSLionel Sambuc   template<typename X>
149*f4a2713aSLionel Sambuc   template<typename T, typename U>
f()150*f4a2713aSLionel Sambuc   void S<X>::f() {}
151*f4a2713aSLionel Sambuc }
152*f4a2713aSLionel Sambuc 
153*f4a2713aSLionel Sambuc namespace DR1635 {
154*f4a2713aSLionel Sambuc   template <class T> struct X {
fDR1635::X155*f4a2713aSLionel Sambuc     template <class U = typename T::type> static void f(int) {} // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} \
156*f4a2713aSLionel Sambuc                                                                 // expected-warning {{C++11}}
fDR1635::X157*f4a2713aSLionel Sambuc     static void f(...) {}
158*f4a2713aSLionel Sambuc   };
159*f4a2713aSLionel Sambuc 
g()160*f4a2713aSLionel Sambuc   int g() { X<int>::f(0); } // expected-note {{in instantiation of template class 'DR1635::X<int>' requested here}}
161*f4a2713aSLionel Sambuc }
162