p2-0x.cpp revision 864b1cf13b288c5099911e1265431ffdcac060a4
1// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu
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// DR1458: taking the address of an object of incomplete class type
113namespace IncompleteClassTypeAddr {
114  struct S;
115  extern S s;
116  constexpr S *p = &s; // ok
117  static_assert(p, "");
118
119  extern S sArr[];
120  constexpr S (*p2)[] = &sArr; // ok
121
122  struct S {
123    constexpr S *operator&() { return nullptr; }
124  };
125  constexpr S *q = &s; // ok
126  static_assert(!q, "");
127}
128
129// - an operation that would have undefined behavior [Note: including, for
130//   example, signed integer overflow (Clause 5 [expr]), certain pointer
131//   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
132//   shift operations (5.8 [expr.shift]) -end note];
133namespace UndefinedBehavior {
134  void f(int n) {
135    switch (n) {
136    case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
137    case (int)0x80000000u: // ok
138    case (int)10000000000ll: // expected-note {{here}}
139    case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
140    case (int)(unsigned)(long long)4.4e9: // ok
141    case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}}
142    case (int)((float)1e37 / 1e30): // ok
143    case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
144      break;
145    }
146  }
147
148  constexpr int int_min = ~0x7fffffff;
149  constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
150  constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
151  constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
152  constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
153  constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
154
155  constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
156  constexpr int shl_0 = 0 << 0; // ok
157  constexpr int shl_31 = 0 << 31; // ok
158  constexpr int shl_32 = 0 << 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type 'int' (32}} expected-warning {{>= width of type}}
159  constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
160  constexpr int shl_unsigned_into_sign = 1u << 31; // ok
161  constexpr int shl_unsigned_overflow = 1024u << 31; // ok
162  constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
163  constexpr int shl_signed_ok = 1 << 30; // ok
164  constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457)
165  constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457)
166  constexpr int shl_signed_off_end = 2 << 31; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{signed shift result (0x100000000) requires 34 bits to represent, but 'int' only has 32 bits}}
167  constexpr int shl_signed_off_end_2 = 0x7fffffff << 2; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{signed shift result (0x1FFFFFFFC) requires 34 bits to represent, but 'int' only has 32 bits}}
168  constexpr int shl_signed_overflow = 1024 << 31; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{requires 43 bits to represent}}
169  constexpr int shl_signed_ok2 = 1024 << 20; // ok
170
171  constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
172  constexpr int shr_0 = 0 >> 0; // ok
173  constexpr int shr_31 = 0 >> 31; // ok
174  constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
175
176  struct S {
177    int m;
178  };
179  constexpr S s = { 5 }; // expected-note {{declared here}}
180  constexpr const int *p = &s.m + 1;
181  constexpr const int &f(const int *q) {
182    return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
183  }
184  constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
185  struct T {
186    int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
187  };
188
189  namespace Ptr {
190    struct A {};
191    struct B : A { int n; };
192    B a[3][3];
193    constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
194    B b = {};
195    constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
196    constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
197    constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
198    constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
199
200    constexpr A *na = nullptr;
201    constexpr B *nb = nullptr;
202    constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
203    constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
204    static_assert((A*)nb == 0, "");
205    static_assert((B*)na == 0, "");
206    constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
207    constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
208
209    struct C {
210      constexpr int f() { return 0; }
211    } constexpr c = C();
212    constexpr int k1 = c.f(); // ok
213    constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}}
214    constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}}
215    C c2;
216    constexpr int k4 = c2.f(); // ok!
217
218    constexpr int diff1 = &a[2] - &a[0];
219    constexpr int diff2 = &a[1][3] - &a[1][0];
220    constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
221    static_assert(&a[2][0] == &a[1][3], "");
222    constexpr int diff4 = (&b + 1) - &b;
223    constexpr int diff5 = &a[1][2].n - &a[1][0].n; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
224    constexpr int diff6 = &a[1][2].n - &a[1][2].n;
225    constexpr int diff7 = (A*)&a[0][1] - (A*)&a[0][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
226  }
227
228  namespace Overflow {
229    // Signed int overflow.
230    constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
231    constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
232    constexpr int n3 = n1 + 1; // ok
233    constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
234    constexpr int n5 = -65536 * 32768; // ok
235    constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
236    constexpr int n7 = -n3 + -1; // ok
237    constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
238    constexpr int n9 = n3 - 0; // ok
239    constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
240    constexpr int n11 = -1 - n3; // ok
241    constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
242    constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
243    constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
244    constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
245    constexpr signed char c1 = 100 * 2; // ok
246    constexpr signed char c2 = '\x64' * '\2'; // also ok
247    constexpr long long ll1 = 0x7fffffffffffffff; // ok
248    constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
249    constexpr long long ll3 = -ll1 - 1; // ok
250    constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
251    constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
252
253    // Yikes.
254    char melchizedek[2200000000];
255    typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
256    constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
257    constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
258    constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
259    constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
260
261    // Unsigned int overflow.
262    static_assert(65536u * 65536u == 0u, ""); // ok
263    static_assert(4294967295u + 1u == 0u, ""); // ok
264    static_assert(0u - 1u == 4294967295u, ""); // ok
265    static_assert(~0u * ~0u == 1u, ""); // ok
266
267    // Floating-point overflow and NaN.
268    constexpr float f1 = 1e38f * 3.4028f; // ok
269    constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
270    constexpr float f3 = 1e38f / -.2939f; // ok
271    constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
272    constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
273    constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
274    constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}}
275  }
276}
277
278// - a lambda-expression (5.1.2);
279struct Lambda {
280  // FIXME: clang crashes when trying to parse this! Revisit this check once
281  // lambdas are fully implemented.
282  //int n : []{ return 1; }();
283};
284
285// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
286namespace LValueToRValue {
287  // - a non-volatile glvalue of integral or enumeration type that refers to a
288  //   non-volatile const object with a preceding initialization, initialized
289  //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
290  //   corresponds to an array of such objects. -end note], or
291  volatile const int vi = 1; // expected-note {{here}}
292  const int ci = 1;
293  volatile const int &vrci = ci;
294  static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
295  static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
296  static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
297
298  // - a non-volatile glvalue of literal type that refers to a non-volatile
299  //   object defined with constexpr, or that refers to a sub-object of such an
300  //   object, or
301  struct S {
302    constexpr S(int=0) : i(1), v(1) {}
303    constexpr S(const S &s) : i(2), v(2) {}
304    int i;
305    volatile int v; // expected-note {{here}}
306  };
307  constexpr S s;
308  constexpr volatile S vs; // expected-note {{here}}
309  constexpr const volatile S &vrs = s;
310  static_assert(s.i, "");
311  static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
312  static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile member 'v'}}
313  static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
314  static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
315  static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
316
317  // - a non-volatile glvalue of literal type that refers to a non-volatile
318  //   temporary object whose lifetime has not ended, initialized with a
319  //   constant expression;
320  constexpr volatile S f() { return S(); }
321  static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
322  static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}}
323}
324
325// DR1312: The proposed wording for this defect has issues, so we ignore this
326// bullet and instead prohibit casts from pointers to cv void (see core-20842
327// and core-20845).
328//
329// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
330// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
331// are neither the same type nor similar types (4.4 [conv.qual]);
332
333// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
334// refers to a non-active member of a union or a subobject thereof;
335namespace LValueToRValueUnion {
336  // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
337  // of this.
338  union U { int a, b; } constexpr u = U();
339  static_assert(u.a == 0, "");
340  constexpr const int *bp = &u.b;
341  constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
342
343  extern const U pu;
344  constexpr const int *pua = &pu.a;
345  constexpr const int *pub = &pu.b;
346  constexpr U pu = { .b = 1 }; // expected-warning {{C99 feature}}
347  constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
348  constexpr const int b2 = *pub; // ok
349}
350
351// - an id-expression that refers to a variable or data member of reference type
352//   unless the reference has a preceding initialization, initialized with a
353//   constant expression;
354namespace References {
355  const int a = 2;
356  int &b = *const_cast<int*>(&a);
357  int c = 10; // expected-note 2 {{here}}
358  int &d = c;
359  constexpr int e = 42;
360  int &f = const_cast<int&>(e);
361  extern int &g;
362  constexpr int &h(); // expected-note 2{{here}}
363  int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
364  constexpr int &j() { return b; }
365  int &k = j();
366
367  struct S {
368    int A : a;
369    int B : b;
370    int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
371    int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
372    int D2 : &d - &c + 1;
373    int E : e / 2;
374    int F : f - 11;
375    int G : g; // expected-error {{constant expression}}
376    int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
377    int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
378    int J : j();
379    int K : k;
380  };
381}
382
383// - a dynamic_cast (5.2.7);
384namespace DynamicCast {
385  struct S { int n; };
386  constexpr S s { 16 };
387  struct T {
388    int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
389  };
390}
391
392// - a reinterpret_cast (5.2.10);
393namespace ReinterpretCast {
394  struct S { int n; };
395  constexpr S s { 16 };
396  struct T {
397    int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
398  };
399  struct U {
400    int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
401  };
402}
403
404// - a pseudo-destructor call (5.2.4);
405namespace PseudoDtor {
406  int k;
407  typedef int I;
408  struct T {
409    int n : (k.~I(), 0); // expected-error {{constant expression}}
410  };
411}
412
413// - increment or decrement operations (5.2.6, 5.3.2);
414namespace IncDec {
415  int k = 2;
416  struct T {
417    int n : ++k; // expected-error {{constant expression}}
418    int m : --k; // expected-error {{constant expression}}
419  };
420}
421
422// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
423namespace std {
424  struct type_info {
425    virtual ~type_info();
426    const char *name;
427  };
428}
429namespace TypeId {
430  struct S { virtual void f(); };
431  constexpr S *p = 0;
432  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'}}
433
434  struct T {} t;
435  constexpr const std::type_info &ti2 = typeid(t);
436}
437
438// - a new-expression (5.3.4);
439// - a delete-expression (5.3.5);
440namespace NewDelete {
441  int *p = 0;
442  struct T {
443    int n : *new int(4); // expected-error {{constant expression}}
444    int m : (delete p, 2); // expected-error {{constant expression}}
445  };
446}
447
448// - a relational (5.9) or equality (5.10) operator where the result is
449//   unspecified;
450namespace UnspecifiedRelations {
451  int a, b;
452  constexpr int *p = &a, *q = &b;
453  // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
454  // different objects that are not members of the same array or to different
455  // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
456  // and p>=q are unspecified.
457  constexpr bool u1 = p < q; // expected-error {{constant expression}}
458  constexpr bool u2 = p > q; // expected-error {{constant expression}}
459  constexpr bool u3 = p <= q; // expected-error {{constant expression}}
460  constexpr bool u4 = p >= q; // expected-error {{constant expression}}
461  constexpr bool u5 = p < 0; // expected-error {{constant expression}}
462  constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
463  constexpr bool u7 = p > 0; // expected-error {{constant expression}}
464  constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
465  constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
466  constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
467  constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
468  constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
469  void f(), g();
470
471  constexpr void (*pf)() = &f, (*pg)() = &g;
472  constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
473  constexpr bool u14 = pf == pg;
474
475  // If two pointers point to non-static data members of the same object with
476  // different access control, the result is unspecified.
477  struct A {
478  public:
479    constexpr A() : a(0), b(0) {}
480    int a;
481    constexpr bool cmp() { return &a < &b; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{comparison of address of fields 'a' and 'b' of 'A' with differing access specifiers (public vs private) has unspecified value}}
482  private:
483    int b;
484  };
485  class B {
486  public:
487    A a;
488    constexpr bool cmp() { return &a.a < &b.a; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{comparison of address of fields 'a' and 'b' of 'B' with differing access specifiers (public vs protected) has unspecified value}}
489  protected:
490    A b;
491  };
492
493  // If two pointers point to different base sub-objects of the same object, or
494  // one points to a base subobject and the other points to a member, the result
495  // of the comparison is unspecified. This is not explicitly called out by
496  // [expr.rel]p2, but is covered by 'Other pointer comparisons are
497  // unspecified'.
498  struct C {
499    int c[2];
500  };
501  struct D {
502    int d;
503  };
504  struct E : C, D {
505    struct Inner {
506      int f;
507    } e;
508  } e;
509  constexpr bool base1 = &e.c[0] < &e.d; // expected-error {{constant expression}} expected-note {{comparison of addresses of subobjects of different base classes has unspecified value}}
510  constexpr bool base2 = &e.c[1] < &e.e.f; // expected-error {{constant expression}} expected-note {{comparison of address of base class subobject 'C' of class 'E' to field 'e' has unspecified value}}
511  constexpr bool base3 = &e.e.f < &e.d; // expected-error {{constant expression}} expected-note {{comparison of address of base class subobject 'D' of class 'E' to field 'e' has unspecified value}}
512
513  // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
514  // represent the same address or are both the null pointer [...]; otherwise
515  // the result is unspecified.
516  struct S { int a, b; } s;
517  constexpr void *null = 0;
518  constexpr void *pv = (void*)&s.a;
519  constexpr void *qv = (void*)&s.b;
520  constexpr bool v1 = null < 0;
521  constexpr bool v2 = null < pv; // expected-error {{constant expression}}
522  constexpr bool v3 = null == pv; // ok
523  constexpr bool v4 = qv == pv; // ok
524  constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
525  constexpr bool v6 = qv > null; // expected-error {{constant expression}}
526  constexpr bool v7 = qv <= (void*)&s.b; // ok
527  constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
528}
529
530// - an assignment or a compound assignment (5.17); or
531namespace Assignment {
532  int k;
533  struct T {
534    int n : (k = 9); // expected-error {{constant expression}}
535    int m : (k *= 2); // expected-error {{constant expression}}
536  };
537
538  struct Literal {
539    constexpr Literal(const char *name) : name(name) {}
540    const char *name;
541  };
542  struct Expr {
543    constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
544    bool IsLiteral;
545    union {
546      Literal l;
547      // ...
548    };
549  };
550  struct MulEq {
551    constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
552    Expr LHS;
553    Expr RHS;
554  };
555  constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
556  Literal a("a");
557  Literal b("b");
558  MulEq c = a *= b; // ok
559}
560
561// - a throw-expression (15.1)
562namespace Throw {
563  struct S {
564    int n : (throw "hello", 10); // expected-error {{constant expression}}
565  };
566}
567
568// PR9999
569template<unsigned int v>
570class bitWidthHolding {
571public:
572  static const
573  unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
574};
575
576static const int width=bitWidthHolding<255>::width;
577
578template<bool b>
579struct always_false {
580  static const bool value = false;
581};
582
583template<bool b>
584struct and_or {
585  static const bool and_value = b && and_or<always_false<b>::value>::and_value;
586  static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
587};
588
589static const bool and_value = and_or<true>::and_value;
590static const bool or_value = and_or<true>::or_value;
591
592static_assert(and_value == false, "");
593static_assert(or_value == true, "");
594