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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 256
// A conditional-expression is a core constant expression unless it involves one
// of the following as a potentially evaluated subexpression [...]:
// - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
// expression, function invocation substitution (7.1.5 [dcl.constexpr])
// replaces each occurrence of this in a constexpr member function with a
// pointer to the class object. -end note];
struct This {
int this1 : this1; // expected-error {{undeclared}}
int this2 : this->this1; // expected-error {{invalid}}
void this3() {
int n1[this->this1]; // expected-warning {{variable length array}}
int n2[this1]; // expected-warning {{variable length array}}
(void)n1, (void)n2;
}
};
// - an invocation of a function other than a constexpr constructor for a
// literal class or a constexpr function [ Note: Overload resolution (13.3)
// is applied as usual - end note ];
struct NonConstexpr1 {
static int f() { return 1; } // expected-note {{here}}
int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
};
struct NonConstexpr2 {
constexpr NonConstexpr2(); // expected-note {{here}}
int n;
};
struct NonConstexpr3 {
NonConstexpr3();
int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
};
struct NonConstexpr4 {
NonConstexpr4();
int n;
};
struct NonConstexpr5 {
int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-literal type 'NonConstexpr4' cannot be used in a constant expression}}
};
// - an invocation of an undefined constexpr function or an undefined
// constexpr constructor;
struct UndefinedConstexpr {
constexpr UndefinedConstexpr();
static constexpr int undefinedConstexpr1(); // expected-note {{here}}
int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
};
// - an invocation of a constexpr function with arguments that, when substituted
// by function invocation substitution (7.1.5), do not produce a constant
// expression;
namespace NonConstExprReturn {
static constexpr const int &id_ref(const int &n) {
return n; // expected-note {{reference to temporary cannot be returned from a constexpr function}}
}
struct NonConstExprFunction {
int n : id_ref( // expected-error {{constant expression}}
16 // expected-note {{temporary created here}}
);
};
constexpr const int *address_of(const int &a) {
return &a; // expected-note {{pointer to 'n' cannot be returned from a constexpr function}}
}
constexpr const int *return_param(int n) { // expected-note {{declared here}}
return address_of(n);
}
struct S {
int n : *return_param(0); // expected-error {{constant expression}}
};
}
// - an invocation of a constexpr constructor with arguments that, when
// substituted by function invocation substitution (7.1.5), do not produce all
// constant expressions for the constructor calls and full-expressions in the
// mem-initializers (including conversions);
namespace NonConstExprCtor {
struct T {
constexpr T(const int &r) :
r(r) { // expected-note {{reference to temporary cannot be used to initialize a member in a constant expression}}
}
const int &r;
};
constexpr int n = 0;
constexpr T t1(n); // ok
constexpr T t2(0); // expected-error {{must be initialized by a constant expression}}
struct S {
int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}}
};
}
// - an invocation of a constexpr function or a constexpr constructor that would
// exceed the implementation-defined recursion limits (see Annex B);
namespace RecursionLimits {
constexpr int RecurseForever(int n) {
return n + RecurseForever(n+1); // expected-note {{constexpr evaluation exceeded maximum depth of 256 calls}}
}
struct AlsoRecurseForever {
constexpr AlsoRecurseForever(int n) :
n(AlsoRecurseForever(n+1).n) // expected-note {{constexpr evaluation exceeded maximum depth of 256 calls}}
{}
int n;
};
struct S {
int k : RecurseForever(0); // expected-error {{constant expression}}
int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}}
};
}
// FIXME:
// - an operation that would have undefined behavior [Note: including, for
// example, signed integer overflow (Clause 5 [expr]), certain pointer
// arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
// shift operations (5.8 [expr.shift]) -end note];
namespace UndefinedBehavior {
void f(int n) {
switch (n) {
case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
case (int)(unsigned)(long long)4.4e9: // ok
case (float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}}
case (int)((float)1e37 / 1e30): // ok
case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
break;
}
}
struct S {
int m;
};
constexpr S s = { 5 }; // expected-note {{declared here}}
constexpr const int *p = &s.m + 1;
constexpr const int &f(const int *q) {
return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
}
struct T {
int n : f(p); // expected-error {{not an integer constant expression}}
};
}
// - a lambda-expression (5.1.2);
struct Lambda {
// FIXME: clang crashes when trying to parse this! Revisit this check once
// lambdas are fully implemented.
//int n : []{ return 1; }();
};
// FIXME:
// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
//
// - a non-volatile glvalue of integral or enumeration type that refers to a
// non-volatile const object with a preceding initialization, initialized with
// a constant expression [Note: a string literal (2.14.5 [lex.string])
// corresponds to an array of such objects. -end note], or
//
// - a non-volatile glvalue of literal type that refers to a non-volatile
// object defined with constexpr, or that refers to a sub-object of such an
// object, or
//
// - a non-volatile glvalue of literal type that refers to a non-volatile
// temporary object whose lifetime has not ended, initialized with a constant
// expression;
// FIXME:
//
// DR1312: The proposed wording for this defect has issues, so we instead
// prohibit casts from pointers to cv void (see core-20842 and core-20845).
//
// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
// are neither the same type nor similar types (4.4 [conv.qual]);
// FIXME:
// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
// refers to a non-active member of a union or a subobject thereof;
// FIXME:
// - an id-expression that refers to a variable or data member of reference type
// unless the reference has a preceding initialization, initialized with a
// constant expression;
namespace References {
const int a = 2;
int &b = *const_cast<int*>(&a);
int c = 10;
int &d = c;
constexpr int e = 42;
int &f = const_cast<int&>(e);
extern int &g;
constexpr int &h(); // expected-note {{here}}
int &i = h();
constexpr int &j() { return b; }
int &k = j();
struct S {
int A : a;
int B : b;
int C : c; // expected-error {{constant expression}}
int D : d; // expected-error {{constant expression}}
int D2 : &d - &c + 1;
int E : e / 2;
int F : f - 11;
int G : g; // expected-error {{constant expression}}
int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
int I : i; // expected-error {{constant expression}}
int J : j();
int K : k;
};
}
// - a dynamic_cast (5.2.7);
namespace DynamicCast {
struct S { int n; };
constexpr S s { 16 };
struct T {
int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
};
}
// - a reinterpret_cast (5.2.10);
namespace ReinterpretCast {
struct S { int n; };
constexpr S s { 16 };
struct T {
int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
};
struct U {
int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
};
}
// - a pseudo-destructor call (5.2.4);
namespace PseudoDtor {
int k;
typedef int I;
struct T {
int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}}
};
}
// - increment or decrement operations (5.2.6, 5.3.2);
namespace IncDec {
int k = 2;
struct T {
int n : ++k; // expected-error {{constant expression}}
int m : --k; // expected-error {{constant expression}}
};
}
// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
namespace std {
struct type_info {
virtual ~type_info();
const char *name;
};
}
namespace TypeId {
struct S { virtual void f(); };
constexpr S *p = 0;
constexpr const std::type_info &ti1 = typeid(*p); // expected-error {{must be initialized by a constant expression}}
// FIXME: Implement typeid evaluation.
struct T {} t;
constexpr const std::type_info &ti2 = typeid(t); // unexpected-er
|