p2-0x.cpp revision f72fccf533bca206af8e75d041c29db99e6a7f2c
1// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128
2
3// A conditional-expression is a core constant expression unless it involves one
4// of the following as a potentially evaluated subexpression [...]:
5
6// - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
7//   expression, function invocation substitution (7.1.5 [dcl.constexpr])
8//   replaces each occurrence of this in a constexpr member function with a
9//   pointer to the class object. -end note];
10struct This {
11  int this1 : this1; // expected-error {{undeclared}}
12  int this2 : this->this1; // expected-error {{invalid}}
13  void this3() {
14    int n1[this->this1]; // expected-warning {{variable length array}}
15    int n2[this1]; // expected-warning {{variable length array}}
16    (void)n1, (void)n2;
17  }
18};
19
20// - an invocation of a function other than a constexpr constructor for a
21//   literal class or a constexpr function [ Note: Overload resolution (13.3)
22//   is applied as usual - end note ];
23struct NonConstexpr1 {
24  static int f() { return 1; } // expected-note {{here}}
25  int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
26};
27struct NonConstexpr2 {
28  constexpr NonConstexpr2(); // expected-note {{here}}
29  int n;
30};
31struct NonConstexpr3 {
32  NonConstexpr3();
33  int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
34};
35struct NonConstexpr4 {
36  NonConstexpr4(); // expected-note {{declared here}}
37  int n;
38};
39struct NonConstexpr5 {
40  int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}}
41};
42
43// - an invocation of an undefined constexpr function or an undefined
44//   constexpr constructor;
45struct UndefinedConstexpr {
46  constexpr UndefinedConstexpr();
47  static constexpr int undefinedConstexpr1(); // expected-note {{here}}
48  int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
49};
50
51// - an invocation of a constexpr function with arguments that, when substituted
52//   by function invocation substitution (7.1.5), do not produce a constant
53//   expression;
54namespace NonConstExprReturn {
55  static constexpr const int &id_ref(const int &n) {
56    return n; // expected-note {{reference to temporary cannot be returned from a constexpr function}}
57  }
58  struct NonConstExprFunction {
59    int n : id_ref( // expected-error {{constant expression}} expected-note {{in call to 'id_ref(16)'}}
60        16 // expected-note {{temporary created here}}
61        );
62  };
63  constexpr const int *address_of(const int &a) {
64    return &a; // expected-note {{pointer to 'n' cannot be returned from a constexpr function}}
65  }
66  constexpr const int *return_param(int n) { // expected-note {{declared here}}
67    return address_of(n); // expected-note {{in call to 'address_of(n)'}}
68  }
69  struct S {
70    int n : *return_param(0); // expected-error {{constant expression}} expected-note {{in call to 'return_param(0)'}}
71  };
72}
73
74// - an invocation of a constexpr constructor with arguments that, when
75//   substituted by function invocation substitution (7.1.5), do not produce all
76//   constant expressions for the constructor calls and full-expressions in the
77//   mem-initializers (including conversions);
78namespace NonConstExprCtor {
79  struct T {
80    constexpr T(const int &r) :
81      r(r) { // expected-note 2{{reference to temporary cannot be used to initialize a member in a constant expression}}
82    }
83    const int &r;
84  };
85  constexpr int n = 0;
86  constexpr T t1(n); // ok
87  constexpr T t2(0); // expected-error {{must be initialized by a constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(0)'}}
88
89  struct S {
90    int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(4)'}}
91  };
92}
93
94// - an invocation of a constexpr function or a constexpr constructor that would
95//   exceed the implementation-defined recursion limits (see Annex B);
96namespace RecursionLimits {
97  constexpr int RecurseForever(int n) {
98    return n + RecurseForever(n+1); // expected-note {{constexpr evaluation exceeded maximum depth of 128 calls}} expected-note 9{{in call to 'RecurseForever(}} expected-note {{skipping 118 calls}}
99  }
100  struct AlsoRecurseForever {
101    constexpr AlsoRecurseForever(int n) :
102      n(AlsoRecurseForever(n+1).n) // expected-note {{constexpr evaluation exceeded maximum depth of 128 calls}} expected-note 9{{in call to 'AlsoRecurseForever(}} expected-note {{skipping 118 calls}}
103    {}
104    int n;
105  };
106  struct S {
107    int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
108    int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
109  };
110}
111
112// FIXME:
113// - an operation that would have undefined behavior [Note: including, for
114//   example, signed integer overflow (Clause 5 [expr]), certain pointer
115//   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
116//   shift operations (5.8 [expr.shift]) -end note];
117namespace UndefinedBehavior {
118  void f(int n) {
119    switch (n) {
120    case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
121    case (int)10000000000ll: // expected-error {{constant expression}} expected-note {{value 10000000000 is outside the range of representable values of type 'int'}} expected-note {{here}}
122    case (int)0x80000000u: // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of representable values of type 'int'}}
123    case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
124    case (int)(unsigned)(long long)4.4e9: // ok
125    case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}}
126    case (int)((float)1e37 / 1e30): // ok
127    case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
128      break;
129    }
130  }
131
132  struct S {
133    int m;
134  };
135  constexpr S s = { 5 }; // expected-note {{declared here}}
136  constexpr const int *p = &s.m + 1;
137  constexpr const int &f(const int *q) {
138    return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
139  }
140  constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
141  struct T {
142    int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
143  };
144
145  namespace Ptr {
146    struct A {};
147    struct B : A { int n; };
148    B a[3][3];
149    constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
150    B b = {};
151    constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
152    constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
153    constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
154    constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
155
156    constexpr A *na = nullptr;
157    constexpr B *nb = nullptr;
158    constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
159    constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
160    static_assert((A*)nb == 0, "");
161    static_assert((B*)na == 0, "");
162    constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
163    constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
164  }
165}
166
167// - a lambda-expression (5.1.2);
168struct Lambda {
169  // FIXME: clang crashes when trying to parse this! Revisit this check once
170  // lambdas are fully implemented.
171  //int n : []{ return 1; }();
172};
173
174// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
175namespace LValueToRValue {
176  // - a non-volatile glvalue of integral or enumeration type that refers to a
177  //   non-volatile const object with a preceding initialization, initialized
178  //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
179  //   corresponds to an array of such objects. -end note], or
180  volatile const int vi = 1; // expected-note {{here}}
181  const int ci = 1;
182  volatile const int &vrci = ci;
183  static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
184  static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
185  static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
186
187  // - a non-volatile glvalue of literal type that refers to a non-volatile
188  //   object defined with constexpr, or that refers to a sub-object of such an
189  //   object, or
190  struct S {
191    constexpr S(int=0) : i(1), v(1) {}
192    constexpr S(const S &s) : i(2), v(2) {}
193    int i;
194    volatile int v;
195  };
196  constexpr S s;
197  constexpr volatile S vs; // expected-note {{here}}
198  constexpr const volatile S &vrs = s;
199  static_assert(s.i, "");
200  static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
201  static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
202  static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
203  static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
204
205  // - a non-volatile glvalue of literal type that refers to a non-volatile
206  //   temporary object whose lifetime has not ended, initialized with a
207  //   constant expression;
208  constexpr volatile S f() { return S(); }
209  static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
210  static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
211}
212
213// FIXME:
214//
215// DR1312: The proposed wording for this defect has issues, so we ignore this
216// bullet and instead prohibit casts from pointers to cv void (see core-20842
217// and core-20845).
218//
219// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
220// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
221// are neither the same type nor similar types (4.4 [conv.qual]);
222
223// FIXME:
224// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
225// refers to a non-active member of a union or a subobject thereof;
226
227// - an id-expression that refers to a variable or data member of reference type
228//   unless the reference has a preceding initialization, initialized with a
229//   constant expression;
230namespace References {
231  const int a = 2;
232  int &b = *const_cast<int*>(&a);
233  int c = 10; // expected-note 2 {{here}}
234  int &d = c;
235  constexpr int e = 42;
236  int &f = const_cast<int&>(e);
237  extern int &g;
238  constexpr int &h(); // expected-note 2{{here}}
239  int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
240  constexpr int &j() { return b; }
241  int &k = j();
242
243  struct S {
244    int A : a;
245    int B : b;
246    int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
247    int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
248    int D2 : &d - &c + 1;
249    int E : e / 2;
250    int F : f - 11;
251    int G : g; // expected-error {{constant expression}}
252    int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
253    int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
254    int J : j();
255    int K : k;
256  };
257}
258
259// - a dynamic_cast (5.2.7);
260namespace DynamicCast {
261  struct S { int n; };
262  constexpr S s { 16 };
263  struct T {
264    int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
265  };
266}
267
268// - a reinterpret_cast (5.2.10);
269namespace ReinterpretCast {
270  struct S { int n; };
271  constexpr S s { 16 };
272  struct T {
273    int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
274  };
275  struct U {
276    int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
277  };
278}
279
280// - a pseudo-destructor call (5.2.4);
281namespace PseudoDtor {
282  int k;
283  typedef int I;
284  struct T {
285    int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}}
286  };
287}
288
289// - increment or decrement operations (5.2.6, 5.3.2);
290namespace IncDec {
291  int k = 2;
292  struct T {
293    int n : ++k; // expected-error {{constant expression}}
294    int m : --k; // expected-error {{constant expression}}
295  };
296}
297
298// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
299namespace std {
300  struct type_info {
301    virtual ~type_info();
302    const char *name;
303  };
304}
305namespace TypeId {
306  struct S { virtual void f(); };
307  constexpr S *p = 0;
308  constexpr const std::type_info &ti1 = typeid(*p); // expected-error {{must be initialized by a constant expression}} expected-note {{typeid applied to expression of polymorphic type 'TypeId::S'}}
309
310  struct T {} t;
311  constexpr const std::type_info &ti2 = typeid(t);
312}
313
314// - a new-expression (5.3.4);
315// - a delete-expression (5.3.5);
316namespace NewDelete {
317  int *p = 0;
318  struct T {
319    int n : *new int(4); // expected-error {{constant expression}} expected-note {{subexpression}}
320    int m : (delete p, 2); // expected-error {{constant expression}} expected-note {{subexpression}}
321  };
322}
323
324// - a relational (5.9) or equality (5.10) operator where the result is
325//   unspecified;
326namespace UnspecifiedRelations {
327  int a, b;
328  constexpr int *p = &a, *q = &b;
329  // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
330  // different objects that are not members of the same array or to different
331  // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
332  // and p>=q are unspecified.
333  constexpr bool u1 = p < q; // expected-error {{constant expression}}
334  constexpr bool u2 = p > q; // expected-error {{constant expression}}
335  constexpr bool u3 = p <= q; // expected-error {{constant expression}}
336  constexpr bool u4 = p >= q; // expected-error {{constant expression}}
337  constexpr bool u5 = p < 0; // expected-error {{constant expression}}
338  constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
339  constexpr bool u7 = p > 0; // expected-error {{constant expression}}
340  constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
341  constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
342  constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
343  constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
344  constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
345  void f(), g();
346
347  constexpr void (*pf)() = &f, (*pg)() = &g;
348  constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
349  constexpr bool u14 = pf == pg;
350
351  // FIXME:
352  // If two pointers point to non-static data members of the same object with
353  // different access control, the result is unspecified.
354
355  // FIXME:
356  // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
357  // represent the same address or are both the null pointer [...]; otherwise
358  // the result is unspecified.
359
360  // FIXME: Implement comparisons of pointers to members.
361  // [expr.eq]p2: If either is a pointer to a virtual member function and
362  // neither is null, the result is unspecified.
363}
364
365// - an assignment or a compound assignment (5.17); or
366namespace Assignment {
367  int k;
368  struct T {
369    int n : (k = 9); // expected-error {{constant expression}}
370    int m : (k *= 2); // expected-error {{constant expression}}
371  };
372
373  struct Literal {
374    constexpr Literal(const char *name) : name(name) {}
375    const char *name;
376  };
377  struct Expr {
378    constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
379    bool IsLiteral;
380    union {
381      Literal l;
382      // ...
383    };
384  };
385  struct MulEq {
386    constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
387    Expr LHS;
388    Expr RHS;
389  };
390  constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
391  Literal a("a");
392  Literal b("b");
393  MulEq c = a *= b; // ok
394}
395
396// - a throw-expression (15.1)
397namespace Throw {
398  struct S {
399    int n : (throw "hello", 10); // expected-error {{constant expression}} expected-note {{subexpression}}
400  };
401}
402
403// PR9999
404template<unsigned int v>
405class bitWidthHolding {
406public:
407  static const
408  unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
409};
410
411static const int width=bitWidthHolding<255>::width;
412
413template<bool b>
414struct always_false {
415  static const bool value = false;
416};
417
418template<bool b>
419struct and_or {
420  static const bool and_value = b && and_or<always_false<b>::value>::and_value;
421  static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
422};
423
424static const bool and_value = and_or<true>::and_value;
425static const bool or_value = and_or<true>::or_value;
426
427static_assert(and_value == false, "");
428static_assert(or_value == true, "");
429