p2-0x.cpp revision 86c3ae46250cdcc57778c27826060779a92f3815
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
52c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   by function invocation substitution (7.1.5), do not produce a constant
53c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   expression;
54c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NonConstExprReturn {
55c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  static constexpr const int &id_ref(const int &n) {
56c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return n; // expected-note {{reference to temporary cannot be returned from a constexpr function}}
57c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
58c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct NonConstExprFunction {
5908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    int n : id_ref( // expected-error {{constant expression}} expected-note {{in call to 'id_ref(16)'}}
60c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        16 // expected-note {{temporary created here}}
61c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        );
62c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
63c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int *address_of(const int &a) {
64c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return &a; // expected-note {{pointer to 'n' cannot be returned from a constexpr function}}
65c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
66c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int *return_param(int n) { // expected-note {{declared here}}
6708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    return address_of(n); // expected-note {{in call to 'address_of(n)'}}
68c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
69c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
7008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    int n : *return_param(0); // expected-error {{constant expression}} expected-note {{in call to 'return_param(0)'}}
71c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
72c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
73c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
74c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a constexpr constructor with arguments that, when
75c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   substituted by function invocation substitution (7.1.5), do not produce all
76c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   constant expressions for the constructor calls and full-expressions in the
77c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   mem-initializers (including conversions);
78c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NonConstExprCtor {
79c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
80c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr T(const int &r) :
81099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith      r(r) { // expected-note 2{{reference to temporary cannot be used to initialize a member in a constant expression}}
82c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
83c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const int &r;
84c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
85c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int n = 0;
86c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr T t1(n); // ok
87099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  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)'}}
88c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
89c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
9008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(4)'}}
91c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
92c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
93c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
94c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a constexpr function or a constexpr constructor that would
95c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   exceed the implementation-defined recursion limits (see Annex B);
96c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace RecursionLimits {
97c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int RecurseForever(int n) {
981e7fc3d31e17fbe314f86de96aac6e9a2f297167Richard 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}}
99c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
100c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct AlsoRecurseForever {
101c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr AlsoRecurseForever(int n) :
1021e7fc3d31e17fbe314f86de96aac6e9a2f297167Richard 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}}
103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    {}
104c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n;
105c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
106c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
10708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
10808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
1122fd5983e0da447291a651a347c206aee37a1de5fRichard Smith// DR1458: taking the address of an object of incomplete class type
1132fd5983e0da447291a651a347c206aee37a1de5fRichard Smithnamespace IncompleteClassTypeAddr {
114864b1cf13b288c5099911e1265431ffdcac060a4Richard Smith  struct S;
1152fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  extern S s;
116864b1cf13b288c5099911e1265431ffdcac060a4Richard Smith  constexpr S *p = &s; // ok
117864b1cf13b288c5099911e1265431ffdcac060a4Richard Smith  static_assert(p, "");
1182fd5983e0da447291a651a347c206aee37a1de5fRichard Smith
1192fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  extern S sArr[];
1202fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  constexpr S (*p2)[] = &sArr; // ok
1212fd5983e0da447291a651a347c206aee37a1de5fRichard Smith
1222fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  struct S {
1232fd5983e0da447291a651a347c206aee37a1de5fRichard Smith    constexpr S *operator&() { return nullptr; }
1242fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  };
125864b1cf13b288c5099911e1265431ffdcac060a4Richard Smith  constexpr S *q = &s; // ok
1262fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  static_assert(!q, "");
1272fd5983e0da447291a651a347c206aee37a1de5fRichard Smith}
1282fd5983e0da447291a651a347c206aee37a1de5fRichard Smith
129c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an operation that would have undefined behavior [Note: including, for
130c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   example, signed integer overflow (Clause 5 [expr]), certain pointer
131c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
132c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   shift operations (5.8 [expr.shift]) -end note];
133c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UndefinedBehavior {
134c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  void f(int n) {
135c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    switch (n) {
136c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
137395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith    case (int)0x80000000u: // ok
138395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith    case (int)10000000000ll: // expected-note {{here}}
139f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
140c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)(unsigned)(long long)4.4e9: // ok
1418ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}}
142c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)((float)1e37 / 1e30): // ok
143c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
144c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      break;
145c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
146c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
147c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
148789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int int_min = ~0x7fffffff;
149789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
150789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
151789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
152789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
1533df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith  constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
154789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
155789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
156789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_0 = 0 << 0; // ok
157789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_31 = 0 << 31; // ok
158789f9b6be5df6e5151ac35e68416cdf550db1196Richard 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}}
159789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
160789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_into_sign = 1u << 31; // ok
161789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_overflow = 1024u << 31; // ok
162789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
163789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_ok = 1 << 30; // ok
164925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith  constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457)
165925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith  constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457)
166925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard 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}}
167925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard 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}}
168925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard 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}}
169789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_ok2 = 1024 << 20; // ok
170789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
171789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
172789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_0 = 0 >> 0; // ok
173789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_31 = 0 >> 31; // ok
174789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
175789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
176c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
177c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m;
178c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
179c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S s = { 5 }; // expected-note {{declared here}}
180c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int *p = &s.m + 1;
181c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int &f(const int *q) {
182c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
183c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
18461e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith  constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
185c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
186244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
187c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
188b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
189b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  namespace Ptr {
190b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    struct A {};
191b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    struct B : A { int n; };
192b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    B a[3][3];
193b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
194b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    B b = {};
195b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
196b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
197b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
198b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
199b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
200b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A *na = nullptr;
201b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *nb = nullptr;
202b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
203b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
204b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    static_assert((A*)nb == 0, "");
205b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    static_assert((B*)na == 0, "");
206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
207b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
208b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith
209b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    struct C {
210b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith      constexpr int f() { 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 {
280c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // FIXME: clang crashes when trying to parse this! Revisit this check once
281c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // lambdas are fully implemented.
282c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  //int n : []{ return 1; }();
283c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
284c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
285c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
2867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithnamespace LValueToRValue {
2877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of integral or enumeration type that refers to a
2887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   non-volatile const object with a preceding initialization, initialized
2897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
2907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   corresponds to an array of such objects. -end note], or
29186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  volatile const int vi = 1; // expected-note 2{{here}}
2927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  const int ci = 1;
2937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  volatile const int &vrci = ci;
2949ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
2969ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
2987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of literal type that refers to a non-volatile
2997098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   object defined with constexpr, or that refers to a sub-object of such an
3007098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   object, or
30186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  struct V {
30286c3ae46250cdcc57778c27826060779a92f3815Richard Smith    constexpr V() : v(1) {}
30386c3ae46250cdcc57778c27826060779a92f3815Richard Smith    volatile int v; // expected-note {{not literal because}}
30486c3ae46250cdcc57778c27826060779a92f3815Richard Smith  };
30586c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr V v; // expected-error {{non-literal type}}
3067098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  struct S {
30786c3ae46250cdcc57778c27826060779a92f3815Richard Smith    constexpr S(int=0) : i(1), v(const_cast<volatile int&>(vi)) {}
30886c3ae46250cdcc57778c27826060779a92f3815Richard Smith    constexpr S(const S &s) : i(2), v(const_cast<volatile int&>(vi)) {}
3097098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int i;
31086c3ae46250cdcc57778c27826060779a92f3815Richard Smith    volatile int &v;
3117098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  };
31286c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr S s; // ok
3139ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  constexpr volatile S vs; // expected-note {{here}}
31486c3ae46250cdcc57778c27826060779a92f3815Richard Smith  constexpr const volatile S &vrs = s; // ok
3157098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(s.i, "");
3169ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
31786c3ae46250cdcc57778c27826060779a92f3815Richard Smith  static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
3189ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
3197098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
3209ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
3217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
3227098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of literal type that refers to a non-volatile
3237098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   temporary object whose lifetime has not ended, initialized with a
3247098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   constant expression;
3257098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  constexpr volatile S f() { return S(); }
3267098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
327282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}}
3287098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith}
329c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
3307098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// DR1312: The proposed wording for this defect has issues, so we ignore this
3317098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// bullet and instead prohibit casts from pointers to cv void (see core-20842
3327098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// and core-20845).
333c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//
334c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
335c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
336c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// are neither the same type nor similar types (4.4 [conv.qual]);
337c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
338c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
339c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// refers to a non-active member of a union or a subobject thereof;
340f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithnamespace LValueToRValueUnion {
341f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
342f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // of this.
343f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  union U { int a, b; } constexpr u = U();
344f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  static_assert(u.a == 0, "");
345f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *bp = &u.b;
346f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
347f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
348f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  extern const U pu;
349f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *pua = &pu.a;
350f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *pub = &pu.b;
351f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr U pu = { .b = 1 }; // expected-warning {{C99 feature}}
352f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
353f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int b2 = *pub; // ok
354f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
355c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
356c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an id-expression that refers to a variable or data member of reference type
357c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   unless the reference has a preceding initialization, initialized with a
358c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   constant expression;
359c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace References {
360c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  const int a = 2;
361c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &b = *const_cast<int*>(&a);
3627098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  int c = 10; // expected-note 2 {{here}}
363c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &d = c;
364c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int e = 42;
365c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &f = const_cast<int&>(e);
366c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  extern int &g;
367099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  constexpr int &h(); // expected-note 2{{here}}
368099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
369c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int &j() { return b; }
370c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &k = j();
371c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
372c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
373c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int A : a;
374c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int B : b;
3757098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
3767098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
377c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int D2 : &d - &c + 1;
378c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int E : e / 2;
379c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int F : f - 11;
380c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int G : g; // expected-error {{constant expression}}
381c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
382099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
383c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int J : j();
384c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int K : k;
385c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
386c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
387c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
388c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a dynamic_cast (5.2.7);
389c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace DynamicCast {
390c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { int n; };
391c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S s { 16 };
392c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
393c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
394c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
395c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
396c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
397c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a reinterpret_cast (5.2.10);
398c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace ReinterpretCast {
399c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { int n; };
400c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S s { 16 };
401c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
402c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
403c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
404c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct U {
405c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
406c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
407c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
408c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
409c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a pseudo-destructor call (5.2.4);
410c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace PseudoDtor {
411c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int k;
412c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  typedef int I;
413c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
414282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int n : (k.~I(), 0); // expected-error {{constant expression}}
415c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
416c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
417c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
418c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - increment or decrement operations (5.2.6, 5.3.2);
419c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace IncDec {
420c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int k = 2;
421c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
422c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : ++k; // expected-error {{constant expression}}
423c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m : --k; // expected-error {{constant expression}}
424c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
425c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
426c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
427c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
428c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace std {
429c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct type_info {
430c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    virtual ~type_info();
431c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const char *name;
432c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
433c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
434c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace TypeId {
435c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { virtual void f(); };
436c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S *p = 0;
43747d2145675099893d702be4bc06bd9f26d8ddd13Richard 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'}}
438c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {} t;
44047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  constexpr const std::type_info &ti2 = typeid(t);
441c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
442c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
443c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a new-expression (5.3.4);
444c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a delete-expression (5.3.5);
445c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NewDelete {
446c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int *p = 0;
447c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
448282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int n : *new int(4); // expected-error {{constant expression}}
449282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int m : (delete p, 2); // expected-error {{constant expression}}
450c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
451c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
452c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
453c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a relational (5.9) or equality (5.10) operator where the result is
454c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   unspecified;
455c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UnspecifiedRelations {
456c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int a, b;
457c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int *p = &a, *q = &b;
458c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
459c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // different objects that are not members of the same array or to different
460c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
461c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // and p>=q are unspecified.
462c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u1 = p < q; // expected-error {{constant expression}}
463c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u2 = p > q; // expected-error {{constant expression}}
464c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u3 = p <= q; // expected-error {{constant expression}}
465c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u4 = p >= q; // expected-error {{constant expression}}
466c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u5 = p < 0; // expected-error {{constant expression}}
467c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
468c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u7 = p > 0; // expected-error {{constant expression}}
469c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
470c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
471c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
472c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
473c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
474c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  void f(), g();
475c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
476c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr void (*pf)() = &f, (*pg)() = &g;
477c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
478c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u14 = pf == pg;
479c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
480c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // If two pointers point to non-static data members of the same object with
481c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // different access control, the result is unspecified.
482f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct A {
483f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  public:
484f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr A() : a(0), b(0) {}
485f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int a;
486f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr bool cmp() { return &a < &b; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{comparison of address of fields 'a' and 'b' of 'A' with differing access specifiers (public vs private) has unspecified value}}
487f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  private:
488f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int b;
489f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
490f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  class B {
491f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  public:
492f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    A a;
493f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr bool cmp() { return &a.a < &b.a; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{comparison of address of fields 'a' and 'b' of 'B' with differing access specifiers (public vs protected) has unspecified value}}
494f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  protected:
495f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    A b;
496f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
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, "");
599