summaryrefslogtreecommitdiffstats
path: root/test/SemaTemplate/instantiate-expr-4.cpp
blob: e3da45e65841034242d8984a482895835361a23f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// RUN: %clang_cc1 -fsyntax-only -verify %s

// ---------------------------------------------------------------------
// C++ Functional Casts
// ---------------------------------------------------------------------
template<int N>
struct ValueInit0 {
  int f() {
    return int();
  }
};

template struct ValueInit0<5>;

template<int N>
struct FunctionalCast0 {
  int f() {
    return int(N);
  }
};

template struct FunctionalCast0<5>;

struct X { // expected-note 2 {{candidate function}}
  X(int, int); // expected-note 2 {{candidate function}}
};

template<int N, int M>
struct BuildTemporary0 {
  X f() {
    return X(N, M);
  }
};

template struct BuildTemporary0<5, 7>;

template<int N, int M>
struct Temporaries0 {
  void f() {
    (void)X(N, M);
  }
};

template struct Temporaries0<5, 7>;

// ---------------------------------------------------------------------
// new/delete expressions
// ---------------------------------------------------------------------
struct Y { };

template<typename T>
struct New0 {
  T* f(bool x) {
    if (x)
      return new T; // expected-error{{no matching}}
    else
      return new T();
  }
};

template struct New0<int>;
template struct New0<Y>;
template struct New0<X>; // expected-note{{instantiation}}

template<typename T, typename Arg1>
struct New1 {
  T* f(bool x, Arg1 a1) {
    return new T(a1); // expected-error{{no matching}}
  }
};

template struct New1<int, float>;
template struct New1<Y, Y>;
template struct New1<X, Y>; // expected-note{{instantiation}}

template<typename T, typename Arg1, typename Arg2>
struct New2 {
  T* f(bool x, Arg1 a1, Arg2 a2) {
    return new T(a1, a2); // expected-error{{no matching}}
  }
};

template struct New2<X, int, float>;
template struct New2<X, int, int*>; // expected-note{{instantiation}}
// FIXME: template struct New2<int, int, float>;

template<typename T>
struct Delete0 {
  void f(T t) {
    delete t; // expected-error{{cannot delete}}
    ::delete [] t;
  }
};

template struct Delete0<int*>;
template struct Delete0<X*>;
template struct Delete0<int>; // expected-note{{instantiation}}

namespace PR5755 {
  template <class T>
  void Foo() {
    char* p = 0;
    delete[] p;
  }
  
  void Test() {
    Foo<int>();
  }
}

// ---------------------------------------------------------------------
// throw expressions
// ---------------------------------------------------------------------
template<typename T>
struct Throw1 {
  void f(T t) {
    throw;
    throw t; // expected-error{{incomplete type}}
  }
};

struct Incomplete; // expected-note{{forward}}

template struct Throw1<int>;
template struct Throw1<int*>;
template struct Throw1<Incomplete*>; // expected-note{{instantiation}}

// ---------------------------------------------------------------------
// typeid expressions
// ---------------------------------------------------------------------

// FIXME: This should really include <typeinfo>, but we don't have that yet.
namespace std {
  class type_info;
}

template<typename T>
struct TypeId0 {
  const std::type_info &f(T* ptr) {
    if (ptr)
      return typeid(ptr);
    else
      return typeid(T);
  }
};

struct Abstract {
  virtual void f() = 0;
};

template struct TypeId0<int>;
template struct TypeId0<Incomplete>;
template struct TypeId0<Abstract>;

// ---------------------------------------------------------------------
// type traits
// ---------------------------------------------------------------------
template<typename T>
struct is_pod {
  static const bool value = __is_pod(T);
};

static const int is_pod0[is_pod<X>::value? -1 : 1];
static const int is_pod1[is_pod<Y>::value? 1 : -1];

// ---------------------------------------------------------------------
// initializer lists
// ---------------------------------------------------------------------
template<typename T, typename Val1>
struct InitList1 {
  void f(Val1 val1) { 
    T x = { val1 };
  }
};

struct APair {
  int *x;
  const float *y;
};

template struct InitList1<int[1], float>;
template struct InitList1<APair, int*>;

template<typename T, typename Val1, typename Val2>
struct InitList2 {
  void f(Val1 val1, Val2 val2) { 
    T x = { val1, val2 }; // expected-error{{incompatible}}
  }
};

template struct InitList2<APair, int*, float*>;
template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}}

// ---------------------------------------------------------------------
// member references
// ---------------------------------------------------------------------
template<typename T, typename Result>
struct DotMemRef0 {
  void f(T t) {
    Result result = t.m; // expected-error{{non-const lvalue reference to type}}
  }
};

struct MemInt {
  int m;
};

struct InheritsMemInt : MemInt { };

struct MemIntFunc {
  static int m(int);
};

template struct DotMemRef0<MemInt, int&>;
template struct DotMemRef0<InheritsMemInt, int&>;
template struct DotMemRef0<MemIntFunc, int (*)(int)>;
template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}}

template<typename T, typename Result>
struct ArrowMemRef0 {
  void f(T t) {
    Result result = t->m; // expected-error 2{{non-const lvalue reference}}
  }
};

template<typename T>
struct ArrowWrapper {
  T operator->();
};

template struct ArrowMemRef0<MemInt*, int&>;
template struct ArrowMemRef0<InheritsMemInt*, int&>;
template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>;
template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}}

template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>;
template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>;
template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>;
template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}}
template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>;

// FIXME: we should be able to return a MemInt without the reference!
MemInt &createMemInt(int);

template<int N>
struct NonDepMemberExpr0 {
  void f() {
    createMemInt(N).m = N;
  }
};

template struct NonDepMemberExpr0<0>; 

template<typename T, typename Result>
struct MemberFuncCall0 {
  void f(T t) {
    Result result = t.f();
  }
};

template<typename T>
struct HasMemFunc0 {
  T f();
};


template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>;

template<typename Result>
struct ThisMemberFuncCall0 {
  Result g();

  void f() {
    Result r1 = g();
    Result r2 = this->g();
  }
};

template struct ThisMemberFuncCall0<int&>;

template<typename T>
struct NonDepMemberCall0 {
  void foo(HasMemFunc0<int&> x) {
    T result = x.f(); // expected-error{{non-const lvalue reference}}
  }
};

template struct NonDepMemberCall0<int&>;
template struct NonDepMemberCall0<const int&>;
template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}}


template<typename T>
struct QualifiedDeclRef0 {
  T f() {
    return is_pod<X>::value; // expected-error{{initialized}}
  }
};

template struct QualifiedDeclRef0<bool>;
template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}}