p2-0x.cpp revision 9ec7197796a2730d54ae7f632553b5311b2ba3b5
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
112c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an operation that would have undefined behavior [Note: including, for
113c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   example, signed integer overflow (Clause 5 [expr]), certain pointer
114c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
115c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   shift operations (5.8 [expr.shift]) -end note];
116c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UndefinedBehavior {
117c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  void f(int n) {
118c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    switch (n) {
119c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard 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'}}
120395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith    case (int)0x80000000u: // ok
121395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith    case (int)10000000000ll: // expected-note {{here}}
122f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
123c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)(unsigned)(long long)4.4e9: // ok
1248ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard 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'}}
125c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)((float)1e37 / 1e30): // ok
126c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
127c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      break;
128c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
129c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
130c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
131789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int int_min = ~0x7fffffff;
132789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
133789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
134789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
135789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
1363df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith  constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
137789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
138789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
139789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_0 = 0 << 0; // ok
140789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_31 = 0 << 31; // ok
141789f9b6be5df6e5151ac35e68416cdf550db1196Richard 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}}
142789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
143789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_into_sign = 1u << 31; // ok
144789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_unsigned_overflow = 1024u << 31; // ok
145789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
146789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_ok = 1 << 30; // ok
147789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_into_sign = 1 << 31; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
148789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_overflow = 1024 << 31; // expected-error {{constant expression}} expected-note {{value 2199023255552 is outside the range}} expected-warning {{requires 43 bits to represent}}
149789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shl_signed_ok2 = 1024 << 20; // ok
150789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
151789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
152789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_0 = 0 >> 0; // ok
153789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_31 = 0 >> 31; // ok
154789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith  constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
155789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
156c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
157c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m;
158c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
159c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S s = { 5 }; // expected-note {{declared here}}
160c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int *p = &s.m + 1;
161c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr const int &f(const int *q) {
162c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
163c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
16461e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith  constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
165c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
166244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
167c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
168b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
169b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  namespace Ptr {
170b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    struct A {};
171b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    struct B : A { int n; };
172b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    B a[3][3];
173b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
174b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    B b = {};
175b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
176b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
177b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
178b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
179b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
180b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A *na = nullptr;
181b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B *nb = nullptr;
182b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
183b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
184b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    static_assert((A*)nb == 0, "");
185b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    static_assert((B*)na == 0, "");
186b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
187b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
188b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith
189b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    struct C {
190b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith      constexpr int f() { return 0; }
191b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    } constexpr c = C();
192b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    constexpr int k1 = c.f(); // ok
193b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}}
194b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}}
195b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    C c2;
196b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    constexpr int k4 = c2.f(); // ok!
197f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
198f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff1 = &a[2] - &a[0];
199f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff2 = &a[1][3] - &a[1][0];
200f15fda02e9c8c82b4a716618f4010b9af8bff796Richard 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}}
201f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    static_assert(&a[2][0] == &a[1][3], "");
202f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff4 = (&b + 1) - &b;
203f15fda02e9c8c82b4a716618f4010b9af8bff796Richard 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}}
204f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr int diff6 = &a[1][2].n - &a[1][2].n;
205f15fda02e9c8c82b4a716618f4010b9af8bff796Richard 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}}
206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
2077b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
2087b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  namespace Overflow {
2097b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    // Signed int overflow.
2107b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
2117b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
2127b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n3 = n1 + 1; // ok
2137b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
2147b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n5 = -65536 * 32768; // ok
2157b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
2167b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n7 = -n3 + -1; // ok
2177b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
2187b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n9 = n3 - 0; // ok
2197b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
2207b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n11 = -1 - n3; // ok
2217b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
2227b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
2237b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
2247b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
2257b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr signed char c1 = 100 * 2; // ok
2267b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr signed char c2 = '\x64' * '\2'; // also ok
2277b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll1 = 0x7fffffffffffffff; // ok
2287b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
2297b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll3 = -ll1 - 1; // ok
2307b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
2317b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
2327b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
23315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    // Yikes.
23415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    char melchizedek[2200000000];
23515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
23615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
23715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
23815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
23915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith    constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
24015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
2417b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    // Unsigned int overflow.
2427b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    static_assert(65536u * 65536u == 0u, ""); // ok
2437b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    static_assert(4294967295u + 1u == 0u, ""); // ok
2447b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    static_assert(0u - 1u == 4294967295u, ""); // ok
2457b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    static_assert(~0u * ~0u == 1u, ""); // ok
2467b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
2477b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    // Floating-point overflow and NaN.
2487b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f1 = 1e38f * 3.4028f; // ok
2497b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
2507b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f3 = 1e38f / -.2939f; // ok
2517b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
2527b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
2537b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
2547b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}}
2557b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  }
256c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
257c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
258c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a lambda-expression (5.1.2);
259c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct Lambda {
260c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // FIXME: clang crashes when trying to parse this! Revisit this check once
261c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // lambdas are fully implemented.
262c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  //int n : []{ return 1; }();
263c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith};
264c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
265c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
2667098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithnamespace LValueToRValue {
2677098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of integral or enumeration type that refers to a
2687098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   non-volatile const object with a preceding initialization, initialized
2697098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
2707098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   corresponds to an array of such objects. -end note], or
2719ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  volatile const int vi = 1; // expected-note {{here}}
2727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  const int ci = 1;
2737098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  volatile const int &vrci = ci;
2749ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2757098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
2769ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2777098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
2787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of literal type that refers to a non-volatile
2797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   object defined with constexpr, or that refers to a sub-object of such an
2807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   object, or
2817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  struct S {
2827098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    constexpr S(int=0) : i(1), v(1) {}
2837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    constexpr S(const S &s) : i(2), v(2) {}
2847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int i;
285282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    volatile int v; // expected-note {{here}}
2867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  };
2877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  constexpr S s;
2889ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  constexpr volatile S vs; // expected-note {{here}}
2897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  constexpr const volatile S &vrs = s;
2907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(s.i, "");
2919ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2929ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile member 'v'}}
2939ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2947098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
2959ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith  static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
2967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
2977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // - a non-volatile glvalue of literal type that refers to a non-volatile
2987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   temporary object whose lifetime has not ended, initialized with a
2997098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  //   constant expression;
3007098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  constexpr volatile S f() { return S(); }
3017098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
302282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}}
3037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith}
304c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
3057098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// DR1312: The proposed wording for this defect has issues, so we ignore this
3067098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// bullet and instead prohibit casts from pointers to cv void (see core-20842
3077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// and core-20845).
308c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//
309c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
310c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
311c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// are neither the same type nor similar types (4.4 [conv.qual]);
312c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
313c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
314c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// refers to a non-active member of a union or a subobject thereof;
315f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithnamespace LValueToRValueUnion {
316f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
317f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // of this.
318f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  union U { int a, b; } constexpr u = U();
319f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  static_assert(u.a == 0, "");
320f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *bp = &u.b;
321f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
322f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
323f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  extern const U pu;
324f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *pua = &pu.a;
325f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int *pub = &pu.b;
326f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr U pu = { .b = 1 }; // expected-warning {{C99 feature}}
327f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
328f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  constexpr const int b2 = *pub; // ok
329f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
330c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
331c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an id-expression that refers to a variable or data member of reference type
332c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   unless the reference has a preceding initialization, initialized with a
333c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   constant expression;
334c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace References {
335c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  const int a = 2;
336c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &b = *const_cast<int*>(&a);
3377098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  int c = 10; // expected-note 2 {{here}}
338c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &d = c;
339c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int e = 42;
340c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &f = const_cast<int&>(e);
341c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  extern int &g;
342099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  constexpr int &h(); // expected-note 2{{here}}
343099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
344c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int &j() { return b; }
345c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int &k = j();
346c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
347c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
348c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int A : a;
349c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int B : b;
3507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
3517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
352c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int D2 : &d - &c + 1;
353c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int E : e / 2;
354c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int F : f - 11;
355c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int G : g; // expected-error {{constant expression}}
356c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
357099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
358c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int J : j();
359c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int K : k;
360c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
361c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
362c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
363c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a dynamic_cast (5.2.7);
364c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace DynamicCast {
365c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { int n; };
366c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S s { 16 };
367c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
368c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
369c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
370c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
371c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
372c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a reinterpret_cast (5.2.10);
373c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace ReinterpretCast {
374c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { int n; };
375c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S s { 16 };
376c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
377c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
378c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
379c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct U {
380c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
381c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
382c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
383c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
384c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a pseudo-destructor call (5.2.4);
385c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace PseudoDtor {
386c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int k;
387c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  typedef int I;
388c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
389282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int n : (k.~I(), 0); // expected-error {{constant expression}}
390c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
391c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
392c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
393c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - increment or decrement operations (5.2.6, 5.3.2);
394c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace IncDec {
395c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int k = 2;
396c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
397c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : ++k; // expected-error {{constant expression}}
398c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m : --k; // expected-error {{constant expression}}
399c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
400c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
401c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
402c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
403c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace std {
404c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct type_info {
405c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    virtual ~type_info();
406c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const char *name;
407c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
408c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
409c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace TypeId {
410c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S { virtual void f(); };
411c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr S *p = 0;
41247d2145675099893d702be4bc06bd9f26d8ddd13Richard 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'}}
413c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
414c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {} t;
41547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  constexpr const std::type_info &ti2 = typeid(t);
416c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
417c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
418c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a new-expression (5.3.4);
419c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a delete-expression (5.3.5);
420c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NewDelete {
421c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int *p = 0;
422c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
423282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int n : *new int(4); // expected-error {{constant expression}}
424282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int m : (delete p, 2); // expected-error {{constant expression}}
425c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
426c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
427c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
428c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a relational (5.9) or equality (5.10) operator where the result is
429c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith//   unspecified;
430c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UnspecifiedRelations {
431c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int a, b;
432c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr int *p = &a, *q = &b;
433c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
434c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // different objects that are not members of the same array or to different
435c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
436c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // and p>=q are unspecified.
437c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u1 = p < q; // expected-error {{constant expression}}
438c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u2 = p > q; // expected-error {{constant expression}}
439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u3 = p <= q; // expected-error {{constant expression}}
440c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u4 = p >= q; // expected-error {{constant expression}}
441c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u5 = p < 0; // expected-error {{constant expression}}
442c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
443c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u7 = p > 0; // expected-error {{constant expression}}
444c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
445c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
446c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
447c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
448c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
449c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  void f(), g();
450c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
451c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr void (*pf)() = &f, (*pg)() = &g;
452c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
453c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr bool u14 = pf == pg;
454c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
455c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // If two pointers point to non-static data members of the same object with
456c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // different access control, the result is unspecified.
457f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct A {
458f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  public:
459f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    constexpr A() : a(0), b(0) {}
460f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int a;
461f15fda02e9c8c82b4a716618f4010b9af8bff796Richard 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}}
462f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  private:
463f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int b;
464f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
465f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  class B {
466f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  public:
467f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    A a;
468f15fda02e9c8c82b4a716618f4010b9af8bff796Richard 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}}
469f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  protected:
470f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    A b;
471f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
472f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
473f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // If two pointers point to different base sub-objects of the same object, or
474f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // one points to a base subobject and the other points to a member, the result
475f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // of the comparison is unspecified. This is not explicitly called out by
476f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // [expr.rel]p2, but is covered by 'Other pointer comparisons are
477f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // unspecified'.
478f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct C {
479f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int c[2];
480f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
481f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct D {
482f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    int d;
483f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
484f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct E : C, D {
485f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    struct Inner {
486f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      int f;
487f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    } e;
488f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  } e;
489f15fda02e9c8c82b4a716618f4010b9af8bff796Richard 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}}
490f15fda02e9c8c82b4a716618f4010b9af8bff796Richard 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}}
491f15fda02e9c8c82b4a716618f4010b9af8bff796Richard 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}}
492c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
493c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
494c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // represent the same address or are both the null pointer [...]; otherwise
495c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // the result is unspecified.
49682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  struct S { int a, b; } s;
49782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr void *null = 0;
49882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr void *pv = (void*)&s.a;
49982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr void *qv = (void*)&s.b;
50082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v1 = null < 0;
50182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v2 = null < pv; // expected-error {{constant expression}}
50282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v3 = null == pv; // ok
50382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v4 = qv == pv; // ok
50482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
50582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v6 = qv > null; // expected-error {{constant expression}}
50682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v7 = qv <= (void*)&s.b; // ok
50782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith  constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
508c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
509c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
510c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an assignment or a compound assignment (5.17); or
511c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace Assignment {
512c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  int k;
513c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct T {
514c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int n : (k = 9); // expected-error {{constant expression}}
515c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    int m : (k *= 2); // expected-error {{constant expression}}
516c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
517c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
518c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct Literal {
519c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr Literal(const char *name) : name(name) {}
520c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const char *name;
521c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
522c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct Expr {
523c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
524c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    bool IsLiteral;
525c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    union {
526c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Literal l;
527c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      // ...
528c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    };
529c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
530c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct MulEq {
531c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
532c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Expr LHS;
533c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Expr RHS;
534c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
535c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
536c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Literal a("a");
537c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Literal b("b");
538c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  MulEq c = a *= b; // ok
539c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
540c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
541c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a throw-expression (15.1)
542c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace Throw {
543c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  struct S {
544282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    int n : (throw "hello", 10); // expected-error {{constant expression}}
545c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
546c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
54763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
54863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor// PR9999
5498ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<unsigned int v>
55063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorclass bitWidthHolding {
55163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorpublic:
55263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  static const
55363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
55463fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor};
55563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
55663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const int width=bitWidthHolding<255>::width;
55763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
55863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregortemplate<bool b>
55963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstruct always_false {
56063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  static const bool value = false;
56163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor};
56263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
56363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregortemplate<bool b>
56463fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstruct and_or {
56563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  static const bool and_value = b && and_or<always_false<b>::value>::and_value;
56663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor  static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
56763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor};
56863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
56963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const bool and_value = and_or<true>::and_value;
57063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const bool or_value = and_or<true>::or_value;
5718ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
5728ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(and_value == false, "");
5738ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(or_value == true, "");
574