p2-0x.cpp revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu
2c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
3c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// A conditional-expression is a core constant expression unless it involves one
4c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// of the following as a potentially evaluated subexpression [...]:
5c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
6c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
7c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   expression, function invocation substitution (7.1.5 [dcl.constexpr])
8c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   replaces each occurrence of this in a constexpr member function with a
9c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   pointer to the class object. -end note];
10c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct This {
11c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int this1 : this1; // expected-error {{undeclared}}
12c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int this2 : this->this1; // expected-error {{invalid}}
13c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  void this3() {
14c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n1[this->this1]; // expected-warning {{variable length array}}
15c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n2[this1]; // expected-warning {{variable length array}}
16c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    (void)n1, (void)n2;
17c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
18c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
19c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
20c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a function other than a constexpr constructor for a
21c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   literal class or a constexpr function [ Note: Overload resolution (13.3)
22c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   is applied as usual - end note ];
23c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr1 {
24c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  static int f() { return 1; } // expected-note {{here}}
25c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
26c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
27c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr2 {
28c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr NonConstexpr2(); // expected-note {{here}}
29c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int n;
30c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
31c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr3 {
32c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  NonConstexpr3();
33c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
34c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
35c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr4 {
3651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  NonConstexpr4(); // expected-note {{declared here}}
37c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int n;
38c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
39c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr5 {
4051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}}
41c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
42c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
43c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of an undefined constexpr function or an undefined
44c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   constexpr constructor;
45c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct UndefinedConstexpr {
46c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr UndefinedConstexpr();
47c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  static constexpr int undefinedConstexpr1(); // expected-note {{here}}
48c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
49c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
50c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
51c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a constexpr function with arguments that, when substituted
5283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith//   by function invocation substitution (7.1.5), do not produce a core constant
53c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   expression;
54c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NonConstExprReturn {
55c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  static constexpr const int &id_ref(const int &n) {
5683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return n;
57c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
58c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct NonConstExprFunction {
5983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    int n : id_ref(16); // ok
60c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
61c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int *address_of(const int &a) {
6283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return &a;
63c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
64c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int *return_param(int n) { // expected-note {{declared here}}
6583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return address_of(n);
66c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
67c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
6883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    int n : *return_param(0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
69c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
70c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
71c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
72c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a constexpr constructor with arguments that, when
73c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   substituted by function invocation substitution (7.1.5), do not produce all
74c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   constant expressions for the constructor calls and full-expressions in the
75c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   mem-initializers (including conversions);
76c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NonConstExprCtor {
77c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
78c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr T(const int &r) :
7983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      r(r) {
80c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
81c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const int &r;
82c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
83c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int n = 0;
84c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr T t1(n); // ok
8583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  constexpr T t2(0); // expected-error {{must be initialized by a constant expression}} expected-note {{temporary created here}} expected-note {{reference to temporary is not a constant expression}}
86c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
87c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
8883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    int n : T(4).r; // ok
89c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
90c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
91c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
92c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a constexpr function or a constexpr constructor that would
93c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   exceed the implementation-defined recursion limits (see Annex B);
94c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace RecursionLimits {
95c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int RecurseForever(int n) {
961e7fc3d31e17fbe314f86de96aac6e9a2f297167Richard Smith    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}}
97c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
98c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct AlsoRecurseForever {
99c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr AlsoRecurseForever(int n) :
1001e7fc3d31e17fbe314f86de96aac6e9a2f297167Richard Smith      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}}
101c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    {}
102c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n;
103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
104c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
10508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
10608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
107c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
108c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
1102fd5983e0da447291a651a347c206aee37a1de5fRichard Smith// DR1458: taking the address of an object of incomplete class type
1112fd5983e0da447291a651a347c206aee37a1de5fRichard Smithnamespace IncompleteClassTypeAddr {
112864b1cf13b288c5099911e1265431ffdcac060a4Richard Smith  struct S;
1132fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  extern S s;
114864b1cf13b288c5099911e1265431ffdcac060a4Richard Smith  constexpr S *p = &s; // ok
115864b1cf13b288c5099911e1265431ffdcac060a4Richard Smith  static_assert(p, "");
1162fd5983e0da447291a651a347c206aee37a1de5fRichard Smith
1172fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  extern S sArr[];
1182fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  constexpr S (*p2)[] = &sArr; // ok
1192fd5983e0da447291a651a347c206aee37a1de5fRichard Smith
1202fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  struct S {
121840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith    constexpr S *operator&() const { return nullptr; }
1222fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  };
123864b1cf13b288c5099911e1265431ffdcac060a4Richard Smith  constexpr S *q = &s; // ok
1242fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  static_assert(!q, "");
1252fd5983e0da447291a651a347c206aee37a1de5fRichard Smith}
1262fd5983e0da447291a651a347c206aee37a1de5fRichard Smith
127c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an operation that would have undefined behavior [Note: including, for
128c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   example, signed integer overflow (Clause 5 [expr]), certain pointer
129c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
130c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   shift operations (5.8 [expr.shift]) -end note];
131c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UndefinedBehavior {
132c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  void f(int n) {
133c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    switch (n) {
13426dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman    case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}} expected-note {{previous case defined here}}
135395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith    case (int)0x80000000u: // ok
136395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith    case (int)10000000000ll: // expected-note {{here}}
137f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
138c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)(unsigned)(long long)4.4e9: // ok
13926dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman    case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}} expected-error {{duplicate case value '2147483647'}} expected-note {{previous case defined here}}
140c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)((float)1e37 / 1e30): // ok
141651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type '__fp16'}} expected-error {{duplicate case value '2147483647'}}
142c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      break;
143c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
144c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
145c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
146789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int int_min = ~0x7fffffff;
147789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
148789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
149789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
150789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
1513df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith  constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
152789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
153789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
154789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_0 = 0 << 0; // ok
155789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_31 = 0 << 31; // ok
156789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  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}}
157789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
158789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_into_sign = 1u << 31; // ok
159789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_overflow = 1024u << 31; // ok
160789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
161789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_ok = 1 << 30; // ok
162925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith  constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457)
163925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith  constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457)
164925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith  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}}
165925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith  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}}
166925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith  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}}
167789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_ok2 = 1024 << 20; // ok
168789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
169789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
170789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_0 = 0 >> 0; // ok
171789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_31 = 0 >> 31; // ok
172789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
173789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
174c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
175c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m;
176c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
17783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  constexpr S s = { 5 };
178c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int *p = &s.m + 1;
179c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int &f(const int *q) {
18083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return q[0];
181c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
18283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  constexpr int n = (f(p), 0); // ok
183c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
184244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
185c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
186b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
187b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  namespace Ptr {
188b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    struct A {};
189b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    struct B : A { int n; };
190b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    B a[3][3];
191b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
192b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    B b = {};
193b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
194b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
195b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
196b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
197b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
198b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A *na = nullptr;
199b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *nb = nullptr;
200b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
201b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
202b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    static_assert((A*)nb == 0, "");
203b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    static_assert((B*)na == 0, "");
204b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
2056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    constexpr const int *np1 = (int*)nullptr + 0; // ok
2066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok
2076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    constexpr const int *np3 = &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}}
208b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith
209b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    struct C {
210840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith      constexpr int f() const { return 0; }
211b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    } constexpr c = C();
212b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    constexpr int k1 = c.f(); // ok
213b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}}
214b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}}
215b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    C c2;
216b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    constexpr int k4 = c2.f(); // ok!
217f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
218f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff1 = &a[2] - &a[0];
219f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff2 = &a[1][3] - &a[1][0];
220f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
221f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    static_assert(&a[2][0] == &a[1][3], "");
222f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff4 = (&b + 1) - &b;
223f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    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}}
224f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff6 = &a[1][2].n - &a[1][2].n;
225f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    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}}
226b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
2277b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
2287b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  namespace Overflow {
2297b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    // Signed int overflow.
2307b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
2317b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
2327b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n3 = n1 + 1; // ok
2337b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
2347b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n5 = -65536 * 32768; // ok
2357b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
2367b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n7 = -n3 + -1; // ok
2377b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
2387b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n9 = n3 - 0; // ok
2397b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
2407b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n11 = -1 - n3; // ok
2417b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
2427b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
2437b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
2447b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
2457b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr signed char c1 = 100 * 2; // ok
2467b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr signed char c2 = '\x64' * '\2'; // also ok
2477b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll1 = 0x7fffffffffffffff; // ok
2487b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
2497b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll3 = -ll1 - 1; // ok
2507b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
2517b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
2527b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
25315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    // Yikes.
25415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    char melchizedek[2200000000];
25515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
25615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
25715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
25815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
25915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
26015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
2617b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    // Unsigned int overflow.
2627b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    static_assert(65536u * 65536u == 0u, ""); // ok
2637b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    static_assert(4294967295u + 1u == 0u, ""); // ok
2647b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    static_assert(0u - 1u == 4294967295u, ""); // ok
2657b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    static_assert(~0u * ~0u == 1u, ""); // ok
2667b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
2677b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    // Floating-point overflow and NaN.
2687b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f1 = 1e38f * 3.4028f; // ok
2697b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
2707b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f3 = 1e38f / -.2939f; // ok
2717b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
2727b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
2737b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
2747b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}}
2757b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  }
276c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
277c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
278c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a lambda-expression (5.1.2);
279c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct Lambda {
2809d33c40838367ffcc3206a7120a0ce32922b66d8David Majnemer  int n : []{ return 1; }(); // expected-error {{constant expression}} expected-error {{integral constant expression}}
281c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
282c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
283c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
2847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithnamespace LValueToRValue {
2857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of integral or enumeration type that refers to a
2867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   non-volatile const object with a preceding initialization, initialized
2877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
2887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   corresponds to an array of such objects. -end note], or
28986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  volatile const int vi = 1; // expected-note 2{{here}}
2907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  const int ci = 1;
2917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  volatile const int &vrci = ci;
2929ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
2949ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
2967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of literal type that refers to a non-volatile
2977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   object defined with constexpr, or that refers to a sub-object of such an
2987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   object, or
29986c3ae46250cdcc57778c27826060779a92f3815Richard Smith  struct V {
30086c3ae46250cdcc57778c27826060779a92f3815Richard Smith    constexpr V() : v(1) {}
30186c3ae46250cdcc57778c27826060779a92f3815Richard Smith    volatile int v; // expected-note {{not literal because}}
30286c3ae46250cdcc57778c27826060779a92f3815Richard Smith  };
30386c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr V v; // expected-error {{non-literal type}}
3047098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  struct S {
30586c3ae46250cdcc57778c27826060779a92f3815Richard Smith    constexpr S(int=0) : i(1), v(const_cast<volatile int&>(vi)) {}
30686c3ae46250cdcc57778c27826060779a92f3815Richard Smith    constexpr S(const S &s) : i(2), v(const_cast<volatile int&>(vi)) {}
3077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int i;
30886c3ae46250cdcc57778c27826060779a92f3815Richard Smith    volatile int &v;
3097098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  };
31086c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr S s; // ok
3119ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  constexpr volatile S vs; // expected-note {{here}}
31286c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr const volatile S &vrs = s; // ok
3137098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(s.i, "");
3149ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
31586c3ae46250cdcc57778c27826060779a92f3815Richard Smith  static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
3169ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
3177098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
3189ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
3197098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
3207098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of literal type that refers to a non-volatile
3217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   temporary object whose lifetime has not ended, initialized with a
3227098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   constant expression;
3237098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  constexpr volatile S f() { return S(); }
3247098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
32541cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith  static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
3267098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith}
327c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
3287098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// DR1312: The proposed wording for this defect has issues, so we ignore this
3297098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// bullet and instead prohibit casts from pointers to cv void (see core-20842
3307098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// and core-20845).
331c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//
332c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
333c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
334c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// are neither the same type nor similar types (4.4 [conv.qual]);
335c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
336c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
337c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// refers to a non-active member of a union or a subobject thereof;
338f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithnamespace LValueToRValueUnion {
339f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
340f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // of this.
341f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  union U { int a, b; } constexpr u = U();
342f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  static_assert(u.a == 0, "");
343f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *bp = &u.b;
344f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
345f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
346f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  extern const U pu;
347f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *pua = &pu.a;
348f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *pub = &pu.b;
349f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr U pu = { .b = 1 }; // expected-warning {{C99 feature}}
350f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
351f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int b2 = *pub; // ok
352f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
353c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
354c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an id-expression that refers to a variable or data member of reference type
355c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   unless the reference has a preceding initialization, initialized with a
356c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   constant expression;
357c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace References {
358c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  const int a = 2;
359c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &b = *const_cast<int*>(&a);
3607098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  int c = 10; // expected-note 2 {{here}}
361c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &d = c;
362c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int e = 42;
363c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &f = const_cast<int&>(e);
364c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  extern int &g;
36516581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  constexpr int &h(); // expected-note {{here}}
36616581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  int &i = h(); // expected-note {{here}}
367c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int &j() { return b; }
368c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &k = j();
369c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
370c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
371c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int A : a;
372c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int B : b;
3737098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
3747098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
375c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int D2 : &d - &c + 1;
376c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int E : e / 2;
377c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int F : f - 11;
378c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int G : g; // expected-error {{constant expression}}
379c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
380099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
381c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int J : j();
382c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int K : k;
383c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
384c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
385c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
386c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a dynamic_cast (5.2.7);
387c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace DynamicCast {
388c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { int n; };
389c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S s { 16 };
390c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
391c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
392c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
393c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
394c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
395c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a reinterpret_cast (5.2.10);
396c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace ReinterpretCast {
397c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { int n; };
398c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S s { 16 };
399c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
400c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
401c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
402c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct U {
403c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
404c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
405c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
406c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
407c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a pseudo-destructor call (5.2.4);
408c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace PseudoDtor {
409c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int k;
410c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  typedef int I;
411c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
412282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int n : (k.~I(), 0); // expected-error {{constant expression}}
413c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
414c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
415c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
416c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - increment or decrement operations (5.2.6, 5.3.2);
417c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace IncDec {
418c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int k = 2;
419c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
420c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : ++k; // expected-error {{constant expression}}
421c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m : --k; // expected-error {{constant expression}}
422c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
423c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
424c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
425c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
426c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace std {
427c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct type_info {
428c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    virtual ~type_info();
429c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const char *name;
430c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
431c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
432c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace TypeId {
433c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { virtual void f(); };
434c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S *p = 0;
43547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  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'}}
436c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
437c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {} t;
43847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  constexpr const std::type_info &ti2 = typeid(t);
439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
440c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
441c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a new-expression (5.3.4);
442c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a delete-expression (5.3.5);
443c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NewDelete {
444c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int *p = 0;
445c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
446282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int n : *new int(4); // expected-error {{constant expression}}
447282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int m : (delete p, 2); // expected-error {{constant expression}}
448c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
449c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
450c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
451c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a relational (5.9) or equality (5.10) operator where the result is
452c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   unspecified;
453c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UnspecifiedRelations {
454c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int a, b;
455c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int *p = &a, *q = &b;
456c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
457c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // different objects that are not members of the same array or to different
458c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
459c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // and p>=q are unspecified.
460c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u1 = p < q; // expected-error {{constant expression}}
461c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u2 = p > q; // expected-error {{constant expression}}
462c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u3 = p <= q; // expected-error {{constant expression}}
463c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u4 = p >= q; // expected-error {{constant expression}}
464c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u5 = p < 0; // expected-error {{constant expression}}
465c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
466c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u7 = p > 0; // expected-error {{constant expression}}
467c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
468c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
469c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
470c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
471c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
472c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  void f(), g();
473c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
474c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr void (*pf)() = &f, (*pg)() = &g;
475c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
476c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u14 = pf == pg;
477c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
478c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // If two pointers point to non-static data members of the same object with
479c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // different access control, the result is unspecified.
480f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct A {
481f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  public:
482f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr A() : a(0), b(0) {}
483f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int a;
4848a66bf78becf05a24e8251379f3843d1fceb627fRichard Smith    constexpr bool cmp() const { return &a < &b; } // expected-note {{comparison of address of fields 'a' and 'b' of 'A' with differing access specifiers (public vs private) has unspecified value}}
485f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  private:
486f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int b;
487f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
4888a66bf78becf05a24e8251379f3843d1fceb627fRichard Smith  static_assert(A().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
489f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  class B {
490f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  public:
491f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    A a;
4928a66bf78becf05a24e8251379f3843d1fceb627fRichard Smith    constexpr bool cmp() const { return &a.a < &b.a; } // expected-note {{comparison of address of fields 'a' and 'b' of 'B' with differing access specifiers (public vs protected) has unspecified value}}
493f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  protected:
494f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    A b;
495f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
4968a66bf78becf05a24e8251379f3843d1fceb627fRichard Smith  static_assert(B().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
497f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
498f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // If two pointers point to different base sub-objects of the same object, or
499f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // one points to a base subobject and the other points to a member, the result
500f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // of the comparison is unspecified. This is not explicitly called out by
501f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // [expr.rel]p2, but is covered by 'Other pointer comparisons are
502f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // unspecified'.
503f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct C {
504f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int c[2];
505f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
506f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct D {
507f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int d;
508f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
509f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct E : C, D {
510f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    struct Inner {
511f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      int f;
512f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    } e;
513f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  } e;
514f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  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}}
515f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  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}}
516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  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}}
517c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
518c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
519c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // represent the same address or are both the null pointer [...]; otherwise
520c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // the result is unspecified.
52182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  struct S { int a, b; } s;
52282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr void *null = 0;
52382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr void *pv = (void*)&s.a;
52482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr void *qv = (void*)&s.b;
52582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v1 = null < 0;
52682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v2 = null < pv; // expected-error {{constant expression}}
52782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v3 = null == pv; // ok
52882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v4 = qv == pv; // ok
52982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
53082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v6 = qv > null; // expected-error {{constant expression}}
53182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v7 = qv <= (void*)&s.b; // ok
53282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
533c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
534c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
535c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an assignment or a compound assignment (5.17); or
536c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace Assignment {
537c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int k;
538c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
539c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : (k = 9); // expected-error {{constant expression}}
540c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m : (k *= 2); // expected-error {{constant expression}}
541c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
542c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
543c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct Literal {
544c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr Literal(const char *name) : name(name) {}
545c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const char *name;
546c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
547c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct Expr {
548c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
549c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    bool IsLiteral;
550c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    union {
551c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Literal l;
552c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      // ...
553c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    };
554c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
555c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct MulEq {
556c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
557c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Expr LHS;
558c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Expr RHS;
559c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
560c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
561c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Literal a("a");
562c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Literal b("b");
563c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  MulEq c = a *= b; // ok
564c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
565c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
566c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a throw-expression (15.1)
567c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace Throw {
568c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
569282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int n : (throw "hello", 10); // expected-error {{constant expression}}
570c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
571c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
57263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
57363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor// PR9999
5748ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<unsigned int v>
57563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorclass bitWidthHolding {
57663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorpublic:
57763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  static const
57863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
57963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor};
58063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
58163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const int width=bitWidthHolding<255>::width;
58263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
58363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregortemplate<bool b>
58463fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstruct always_false {
58563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  static const bool value = false;
58663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor};
58763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
58863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregortemplate<bool b>
58963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstruct and_or {
59063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  static const bool and_value = b && and_or<always_false<b>::value>::and_value;
59163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
59263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor};
59363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
59463fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const bool and_value = and_or<true>::and_value;
59563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const bool or_value = and_or<true>::or_value;
5968ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
5978ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(and_value == false, "");
5988ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(or_value == true, "");
599f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor
600f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregornamespace rdar13090123 {
601f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor  typedef __INTPTR_TYPE__ intptr_t;
602f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor
603f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor  constexpr intptr_t f(intptr_t x) {
604f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor    return (((x) >> 21) * 8); // expected-note{{subexpression not valid in a constant expression}}
605f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor  }
606f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor
607f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor  extern "C" int foo;
608f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor
609f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor  constexpr intptr_t i = f((intptr_t)&foo - 10); // expected-error{{constexpr variable 'i' must be initialized by a constant expression}} \
610f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor  // expected-note{{in call to 'f((char*)&foo + -10)'}}
611f727e1c6cc382c1b5fe23b38ba04df2d4a2f358aDouglas Gregor}
612