xref: /llvm-project/clang/test/SemaTemplate/instantiate-expr-4.cpp (revision 85dabae6ad94925a9ec99cdf375c4e8768ea0e03)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 // ---------------------------------------------------------------------
4 // C++ Functional Casts
5 // ---------------------------------------------------------------------
6 template<int N>
7 struct ValueInit0 {
8   int f() {
9     return int();
10   }
11 };
12 
13 template struct ValueInit0<5>;
14 
15 template<int N>
16 struct FunctionalCast0 {
17   int f() {
18     return int(N);
19   }
20 };
21 
22 template struct FunctionalCast0<5>;
23 
24 struct X { // expected-note 3 {{candidate function}}
25   X(int, int); // expected-note 3 {{candidate function}}
26 };
27 
28 template<int N, int M>
29 struct BuildTemporary0 {
30   X f() {
31     return X(N, M);
32   }
33 };
34 
35 template struct BuildTemporary0<5, 7>;
36 
37 template<int N, int M>
38 struct Temporaries0 {
39   void f() {
40     (void)X(N, M);
41   }
42 };
43 
44 template struct Temporaries0<5, 7>;
45 
46 // ---------------------------------------------------------------------
47 // new/delete expressions
48 // ---------------------------------------------------------------------
49 struct Y { };
50 
51 template<typename T>
52 struct New0 {
53   T* f(bool x) {
54     if (x)
55       return new T; // expected-error{{no matching}}
56     else
57       return new T();
58   }
59 };
60 
61 template struct New0<int>;
62 template struct New0<Y>;
63 template struct New0<X>; // expected-note{{instantiation}}
64 
65 template<typename T, typename Arg1>
66 struct New1 {
67   T* f(bool x, Arg1 a1) {
68     return new T(a1); // expected-error{{no matching}}
69   }
70 };
71 
72 template struct New1<int, float>;
73 template struct New1<Y, Y>;
74 template struct New1<X, Y>; // expected-note{{instantiation}}
75 
76 template<typename T, typename Arg1, typename Arg2>
77 struct New2 {
78   T* f(bool x, Arg1 a1, Arg2 a2) {
79     return new T(a1, a2); // expected-error{{no matching}}
80   }
81 };
82 
83 template struct New2<X, int, float>;
84 template struct New2<X, int, int*>; // expected-note{{instantiation}}
85 // FIXME: template struct New2<int, int, float>;
86 
87 template<typename T>
88 struct Delete0 {
89   void f(T t) {
90     delete t; // expected-error{{cannot delete}}
91     ::delete [] t;
92   }
93 };
94 
95 template struct Delete0<int*>;
96 template struct Delete0<X*>;
97 template struct Delete0<int>; // expected-note{{instantiation}}
98 
99 namespace PR5755 {
100   template <class T>
101   void Foo() {
102     char* p = 0;
103     delete[] p;
104   }
105 
106   void Test() {
107     Foo<int>();
108   }
109 }
110 
111 // ---------------------------------------------------------------------
112 // throw expressions
113 // ---------------------------------------------------------------------
114 template<typename T>
115 struct Throw1 {
116   void f(T t) {
117     throw;
118     throw t; // expected-error{{incomplete type}}
119   }
120 };
121 
122 struct Incomplete; // expected-note{{forward}}
123 
124 template struct Throw1<int>;
125 template struct Throw1<int*>;
126 template struct Throw1<Incomplete*>; // expected-note{{instantiation}}
127 
128 // ---------------------------------------------------------------------
129 // typeid expressions
130 // ---------------------------------------------------------------------
131 
132 // FIXME: This should really include <typeinfo>, but we don't have that yet.
133 namespace std {
134   class type_info;
135 }
136 
137 template<typename T>
138 struct TypeId0 {
139   const std::type_info &f(T* ptr) {
140     if (ptr)
141       return typeid(ptr);
142     else
143       return typeid(T);
144   }
145 };
146 
147 struct Abstract {
148   virtual void f() = 0;
149 };
150 
151 template struct TypeId0<int>;
152 template struct TypeId0<Incomplete>;
153 template struct TypeId0<Abstract>;
154 
155 // ---------------------------------------------------------------------
156 // type traits
157 // ---------------------------------------------------------------------
158 template<typename T>
159 struct is_pod {
160   static const bool value = __is_pod(T);
161 };
162 
163 static const int is_pod0[is_pod<X>::value? -1 : 1];
164 static const int is_pod1[is_pod<Y>::value? 1 : -1];
165 
166 // ---------------------------------------------------------------------
167 // initializer lists
168 // ---------------------------------------------------------------------
169 template<typename T, typename Val1>
170 struct InitList1 {
171   void f(Val1 val1) {
172     T x = { val1 };
173   }
174 };
175 
176 struct APair {
177   int *x;
178   const float *y;
179 };
180 
181 template struct InitList1<int[1], float>;
182 template struct InitList1<APair, int*>;
183 
184 template<typename T, typename Val1, typename Val2>
185 struct InitList2 {
186   void f(Val1 val1, Val2 val2) {
187     T x = { val1, val2 }; // expected-error{{incompatible}}
188   }
189 };
190 
191 template struct InitList2<APair, int*, float*>;
192 template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}}
193 
194 // ---------------------------------------------------------------------
195 // member references
196 // ---------------------------------------------------------------------
197 template<typename T, typename Result>
198 struct DotMemRef0 {
199   void f(T t) {
200     Result result = t.m; // expected-error{{non-const lvalue reference to type}}
201   }
202 };
203 
204 struct MemInt {
205   int m;
206 };
207 
208 struct InheritsMemInt : MemInt { };
209 
210 struct MemIntFunc {
211   static int m(int);
212 };
213 
214 template struct DotMemRef0<MemInt, int&>;
215 template struct DotMemRef0<InheritsMemInt, int&>;
216 template struct DotMemRef0<MemIntFunc, int (*)(int)>;
217 template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}}
218 
219 template<typename T, typename Result>
220 struct ArrowMemRef0 {
221   void f(T t) {
222     Result result = t->m; // expected-error 2{{non-const lvalue reference}}
223   }
224 };
225 
226 template<typename T>
227 struct ArrowWrapper {
228   T operator->();
229 };
230 
231 template struct ArrowMemRef0<MemInt*, int&>;
232 template struct ArrowMemRef0<InheritsMemInt*, int&>;
233 template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>;
234 template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}}
235 
236 template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>;
237 template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>;
238 template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>;
239 template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}}
240 template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>;
241 
242 // FIXME: we should be able to return a MemInt without the reference!
243 MemInt &createMemInt(int);
244 
245 template<int N>
246 struct NonDepMemberExpr0 {
247   void f() {
248     createMemInt(N).m = N;
249   }
250 };
251 
252 template struct NonDepMemberExpr0<0>;
253 
254 template<typename T, typename Result>
255 struct MemberFuncCall0 {
256   void f(T t) {
257     Result result = t.f();
258   }
259 };
260 
261 template<typename T>
262 struct HasMemFunc0 {
263   T f();
264 };
265 
266 
267 template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>;
268 
269 template<typename Result>
270 struct ThisMemberFuncCall0 {
271   Result g();
272 
273   void f() {
274     Result r1 = g();
275     Result r2 = this->g();
276   }
277 };
278 
279 template struct ThisMemberFuncCall0<int&>;
280 
281 template<typename T>
282 struct NonDepMemberCall0 {
283   void foo(HasMemFunc0<int&> x) {
284     T result = x.f(); // expected-error{{non-const lvalue reference}}
285   }
286 };
287 
288 template struct NonDepMemberCall0<int&>;
289 template struct NonDepMemberCall0<const int&>;
290 template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}}
291 
292 
293 template<typename T>
294 struct QualifiedDeclRef0 {
295   T f() {
296     return is_pod<X>::value; // expected-error{{initialized}}
297   }
298 };
299 
300 template struct QualifiedDeclRef0<bool>;
301 template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}}
302