p2-0x.cpp revision b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9
13a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128
23a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
33a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// A conditional-expression is a core constant expression unless it involves one
43a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// of the following as a potentially evaluated subexpression [...]:
53a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
63a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
73a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   expression, function invocation substitution (7.1.5 [dcl.constexpr])
83a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   replaces each occurrence of this in a constexpr member function with a
93a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   pointer to the class object. -end note];
103a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgstruct This {
113a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  int this1 : this1; // expected-error {{undeclared}}
123a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  int this2 : this->this1; // expected-error {{invalid}}
133a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  void this3() {
143a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int n1[this->this1]; // expected-warning {{variable length array}}
153a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int n2[this1]; // expected-warning {{variable length array}}
163a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    (void)n1, (void)n2;
173a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  }
183a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org};
193a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
203a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - an invocation of a function other than a constexpr constructor for a
213a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   literal class or a constexpr function [ Note: Overload resolution (13.3)
223a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   is applied as usual - end note ];
233a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgstruct NonConstexpr1 {
243a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static int f() { return 1; } // expected-note {{here}}
253a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
26760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org};
273a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgstruct NonConstexpr2 {
283a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr NonConstexpr2(); // expected-note {{here}}
293a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  int n;
303a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org};
313a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgstruct NonConstexpr3 {
323a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  NonConstexpr3();
333a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
343a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org};
353a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgstruct NonConstexpr4 {
363a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  NonConstexpr4(); // expected-note {{declared here}}
373a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  int n;
383a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org};
393a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgstruct NonConstexpr5 {
403a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}}
413a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org};
423a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
433a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - an invocation of an undefined constexpr function or an undefined
443a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   constexpr constructor;
453a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgstruct UndefinedConstexpr {
463a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr UndefinedConstexpr();
473a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static constexpr int undefinedConstexpr1(); // expected-note {{here}}
483a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
493a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org};
503a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
513a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - an invocation of a constexpr function with arguments that, when substituted
523a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   by function invocation substitution (7.1.5), do not produce a constant
533a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   expression;
543a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgnamespace NonConstExprReturn {
553a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static constexpr const int &id_ref(const int &n) {
563a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    return n; // expected-note {{reference to temporary cannot be returned from a constexpr function}}
573a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  }
583a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct NonConstExprFunction {
593a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int n : id_ref( // expected-error {{constant expression}} expected-note {{in call to 'id_ref(16)'}}
603a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org        16 // expected-note {{temporary created here}}
613a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org        );
623a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
633a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr const int *address_of(const int &a) {
643a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    return &a; // expected-note {{pointer to 'n' cannot be returned from a constexpr function}}
653a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  }
663a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr const int *return_param(int n) { // expected-note {{declared here}}
673a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    return address_of(n); // expected-note {{in call to 'address_of(n)'}}
683a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  }
693a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct S {
703a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int n : *return_param(0); // expected-error {{constant expression}} expected-note {{in call to 'return_param(0)'}}
713a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
723a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org}
733a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
743a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - an invocation of a constexpr constructor with arguments that, when
753a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   substituted by function invocation substitution (7.1.5), do not produce all
763a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   constant expressions for the constructor calls and full-expressions in the
773a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   mem-initializers (including conversions);
783a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgnamespace NonConstExprCtor {
793a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct T {
803a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr T(const int &r) :
813a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org      r(r) { // expected-note 2{{reference to temporary cannot be used to initialize a member in a constant expression}}
823a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    }
833a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    const int &r;
843a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
853a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int n = 0;
863a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr T t1(n); // ok
873a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  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)'}}
883a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
893a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct S {
903a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(4)'}}
913a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
923a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org}
933a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
943a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - an invocation of a constexpr function or a constexpr constructor that would
95760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org//   exceed the implementation-defined recursion limits (see Annex B);
963a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgnamespace RecursionLimits {
973a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int RecurseForever(int n) {
983a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    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}}
993a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  }
1003a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct AlsoRecurseForever {
1013a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr AlsoRecurseForever(int n) :
1023a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org      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}}
1033a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    {}
1043a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int n;
1053a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
1063a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct S {
1073a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
1083a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
1093a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
1103a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org}
1113a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
1123a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// FIXME:
1133a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - an operation that would have undefined behavior [Note: including, for
1143a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   example, signed integer overflow (Clause 5 [expr]), certain pointer
1153a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
1163a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org//   shift operations (5.8 [expr.shift]) -end note];
1173a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgnamespace UndefinedBehavior {
1183a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  void f(int n) {
1193a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    switch (n) {
1203a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
1213a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    case (int)0x80000000u: // ok
1223a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    case (int)10000000000ll: // expected-note {{here}}
1233a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
1243a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    case (int)(unsigned)(long long)4.4e9: // ok
1253a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}}
1263a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    case (int)((float)1e37 / 1e30): // ok
1273a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
1283a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org      break;
1293a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    }
1303a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  }
1313a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
1323a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int int_min = ~0x7fffffff;
1333a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
1343a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
1353a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
1363a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
1373a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
1383a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
1393a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
1403a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_0 = 0 << 0; // ok
1413a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_31 = 0 << 31; // ok
1423a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  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}}
1433a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
1443a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_unsigned_into_sign = 1u << 31; // ok
1453a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_unsigned_overflow = 1024u << 31; // ok
1463a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
1473a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_signed_ok = 1 << 30; // ok
1483a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_signed_into_sign = 1 << 31; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
1493a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_signed_overflow = 1024 << 31; // expected-error {{constant expression}} expected-note {{value 2199023255552 is outside the range}} expected-warning {{requires 43 bits to represent}}
1503a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shl_signed_ok2 = 1024 << 20; // ok
1513a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
1523a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
1533a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shr_0 = 0 >> 0; // ok
1543a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shr_31 = 0 >> 31; // ok
1553a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
1563a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
1573a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct S {
1583a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int m;
1593a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
1603a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr S s = { 5 }; // expected-note {{declared here}}
1613a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr const int *p = &s.m + 1;
1623a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr const int &f(const int *q) {
1633a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
1643a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  }
1653a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
1663a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct T {
1673a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
1683a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
1693a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
1703a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  namespace Ptr {
1713a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    struct A {};
1723a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    struct B : A { int n; };
1733a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    B a[3][3];
1743a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
1753a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    B b = {};
1763a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
1773a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
1783a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
179760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
1803a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
181760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    constexpr A *na = nullptr;
182760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    constexpr B *nb = nullptr;
183760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
184760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
185760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    static_assert((A*)nb == 0, "");
186760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    static_assert((B*)na == 0, "");
187760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
1883a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
1893a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
1903a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    struct C {
1913a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org      constexpr int f() { return 0; }
1923a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    } constexpr c = C();
1933a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr int k1 = c.f(); // ok
1943a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}}
1953a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}}
1963a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    C c2;
1973a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr int k4 = c2.f(); // ok!
198760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org  }
1993a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org}
2003a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
2013a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - a lambda-expression (5.1.2);
2023a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgstruct Lambda {
2033a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  // FIXME: clang crashes when trying to parse this! Revisit this check once
2043a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  // lambdas are fully implemented.
2053a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  //int n : []{ return 1; }();
2063a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org};
2073a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
2083a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
2093a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.orgnamespace LValueToRValue {
2103a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  // - a non-volatile glvalue of integral or enumeration type that refers to a
2113a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  //   non-volatile const object with a preceding initialization, initialized
2123a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
2133a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  //   corresponds to an array of such objects. -end note], or
2143a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  volatile const int vi = 1; // expected-note {{here}}
2153a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  const int ci = 1;
2163a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  volatile const int &vrci = ci;
2173a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
2183a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
2193a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2203a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
2213a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  // - a non-volatile glvalue of literal type that refers to a non-volatile
2223a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  //   object defined with constexpr, or that refers to a sub-object of such an
2233a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  //   object, or
2243a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  struct S {
2253a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    constexpr S(int=0) : i(1), v(1) {}
226760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org    constexpr S(const S &s) : i(2), v(2) {}
2273a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    int i;
2283a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org    volatile int v;
2293a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  };
2303a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr S s;
2313a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr volatile S vs; // expected-note {{here}}
2323a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  constexpr const volatile S &vrs = s;
2333a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static_assert(s.i, "");
2343a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2353a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2363a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
2373a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2383a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org
2393a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  // - a non-volatile glvalue of literal type that refers to a non-volatile
2403a0db227ffe90888ad760c61a63226988c974e0apatrick@chromium.org  //   temporary object whose lifetime has not ended, initialized with a
241  //   constant expression;
242  constexpr volatile S f() { return S(); }
243  static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
244  static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
245}
246
247// FIXME:
248//
249// DR1312: The proposed wording for this defect has issues, so we ignore this
250// bullet and instead prohibit casts from pointers to cv void (see core-20842
251// and core-20845).
252//
253// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
254// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
255// are neither the same type nor similar types (4.4 [conv.qual]);
256
257// FIXME:
258// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
259// refers to a non-active member of a union or a subobject thereof;
260
261// - an id-expression that refers to a variable or data member of reference type
262//   unless the reference has a preceding initialization, initialized with a
263//   constant expression;
264namespace References {
265  const int a = 2;
266  int &b = *const_cast<int*>(&a);
267  int c = 10; // expected-note 2 {{here}}
268  int &d = c;
269  constexpr int e = 42;
270  int &f = const_cast<int&>(e);
271  extern int &g;
272  constexpr int &h(); // expected-note 2{{here}}
273  int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
274  constexpr int &j() { return b; }
275  int &k = j();
276
277  struct S {
278    int A : a;
279    int B : b;
280    int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
281    int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
282    int D2 : &d - &c + 1;
283    int E : e / 2;
284    int F : f - 11;
285    int G : g; // expected-error {{constant expression}}
286    int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
287    int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
288    int J : j();
289    int K : k;
290  };
291}
292
293// - a dynamic_cast (5.2.7);
294namespace DynamicCast {
295  struct S { int n; };
296  constexpr S s { 16 };
297  struct T {
298    int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
299  };
300}
301
302// - a reinterpret_cast (5.2.10);
303namespace ReinterpretCast {
304  struct S { int n; };
305  constexpr S s { 16 };
306  struct T {
307    int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
308  };
309  struct U {
310    int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
311  };
312}
313
314// - a pseudo-destructor call (5.2.4);
315namespace PseudoDtor {
316  int k;
317  typedef int I;
318  struct T {
319    int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}}
320  };
321}
322
323// - increment or decrement operations (5.2.6, 5.3.2);
324namespace IncDec {
325  int k = 2;
326  struct T {
327    int n : ++k; // expected-error {{constant expression}}
328    int m : --k; // expected-error {{constant expression}}
329  };
330}
331
332// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
333namespace std {
334  struct type_info {
335    virtual ~type_info();
336    const char *name;
337  };
338}
339namespace TypeId {
340  struct S { virtual void f(); };
341  constexpr S *p = 0;
342  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'}}
343
344  struct T {} t;
345  constexpr const std::type_info &ti2 = typeid(t);
346}
347
348// - a new-expression (5.3.4);
349// - a delete-expression (5.3.5);
350namespace NewDelete {
351  int *p = 0;
352  struct T {
353    int n : *new int(4); // expected-error {{constant expression}} expected-note {{subexpression}}
354    int m : (delete p, 2); // expected-error {{constant expression}} expected-note {{subexpression}}
355  };
356}
357
358// - a relational (5.9) or equality (5.10) operator where the result is
359//   unspecified;
360namespace UnspecifiedRelations {
361  int a, b;
362  constexpr int *p = &a, *q = &b;
363  // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
364  // different objects that are not members of the same array or to different
365  // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
366  // and p>=q are unspecified.
367  constexpr bool u1 = p < q; // expected-error {{constant expression}}
368  constexpr bool u2 = p > q; // expected-error {{constant expression}}
369  constexpr bool u3 = p <= q; // expected-error {{constant expression}}
370  constexpr bool u4 = p >= q; // expected-error {{constant expression}}
371  constexpr bool u5 = p < 0; // expected-error {{constant expression}}
372  constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
373  constexpr bool u7 = p > 0; // expected-error {{constant expression}}
374  constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
375  constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
376  constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
377  constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
378  constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
379  void f(), g();
380
381  constexpr void (*pf)() = &f, (*pg)() = &g;
382  constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
383  constexpr bool u14 = pf == pg;
384
385  // FIXME:
386  // If two pointers point to non-static data members of the same object with
387  // different access control, the result is unspecified.
388
389  // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
390  // represent the same address or are both the null pointer [...]; otherwise
391  // the result is unspecified.
392  struct S { int a, b; } s;
393  constexpr void *null = 0;
394  constexpr void *pv = (void*)&s.a;
395  constexpr void *qv = (void*)&s.b;
396  constexpr bool v1 = null < 0;
397  constexpr bool v2 = null < pv; // expected-error {{constant expression}}
398  constexpr bool v3 = null == pv; // ok
399  constexpr bool v4 = qv == pv; // ok
400  constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
401  constexpr bool v6 = qv > null; // expected-error {{constant expression}}
402  constexpr bool v7 = qv <= (void*)&s.b; // ok
403  constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
404
405  // FIXME: Implement comparisons of pointers to members.
406  // [expr.eq]p2: If either is a pointer to a virtual member function and
407  // neither is null, the result is unspecified.
408}
409
410// - an assignment or a compound assignment (5.17); or
411namespace Assignment {
412  int k;
413  struct T {
414    int n : (k = 9); // expected-error {{constant expression}}
415    int m : (k *= 2); // expected-error {{constant expression}}
416  };
417
418  struct Literal {
419    constexpr Literal(const char *name) : name(name) {}
420    const char *name;
421  };
422  struct Expr {
423    constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
424    bool IsLiteral;
425    union {
426      Literal l;
427      // ...
428    };
429  };
430  struct MulEq {
431    constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
432    Expr LHS;
433    Expr RHS;
434  };
435  constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
436  Literal a("a");
437  Literal b("b");
438  MulEq c = a *= b; // ok
439}
440
441// - a throw-expression (15.1)
442namespace Throw {
443  struct S {
444    int n : (throw "hello", 10); // expected-error {{constant expression}} expected-note {{subexpression}}
445  };
446}
447
448// PR9999
449template<unsigned int v>
450class bitWidthHolding {
451public:
452  static const
453  unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
454};
455
456static const int width=bitWidthHolding<255>::width;
457
458template<bool b>
459struct always_false {
460  static const bool value = false;
461};
462
463template<bool b>
464struct and_or {
465  static const bool and_value = b && and_or<always_false<b>::value>::and_value;
466  static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
467};
468
469static const bool and_value = and_or<true>::and_value;
470static const bool or_value = and_or<true>::or_value;
471
472static_assert(and_value == false, "");
473static_assert(or_value == true, "");
474