constant-expression-cxx11.cpp revision bc6abe93a5d6b1305411f8b6f54c2caa686ddc69
1a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 -pedantic %s -Wno-comment
2a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
3a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace StaticAssertFoldTest {
4a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
5a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinint x;
6a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(++x, "test"); // expected-error {{not an integral constant expression}}
7a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(false, "test"); // expected-error {{test}}
8a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
9a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
10a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
11a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkintemplate<typename T> constexpr T id(const T &t) { return t; } // expected-note {{here}}
12a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin// FIXME: support templates here.
13a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin//template<typename T> constexpr T min(const T &a, const T &b) {
14a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin//  return a < b ? a : b;
15a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin//}
16a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin//template<typename T> constexpr T max(const T &a, const T &b) {
17a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin//  return a < b ? b : a;
18a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin//}
19a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int min(const int &a, const int &b) { return a < b ? a : b; }
20a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int max(const int &a, const int &b) { return a < b ? b : a; }
21a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
22a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstruct MemberZero {
23a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr int zero() { return 0; }
24a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin};
25a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
26a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace DerivedToVBaseCast {
27a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
28a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct U { int n; };
29a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct V : U { int n; };
30a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct A : virtual V { int n; };
31a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct Aa { int n; };
32a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct B : virtual A, Aa {};
33a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct C : virtual A, Aa {};
34a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct D : B, C {};
35a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
36a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  D d;
37a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr B *p = &d;
38a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr C *q = &d;
39a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((void*)p != (void*)q, "");
40a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((A*)p == (A*)q, "");
41a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((Aa*)p != (Aa*)q, "");
42a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
43a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr B &pp = d;
44a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr C &qq = d;
45a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((void*)&pp != (void*)&qq, "");
46a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(&(A&)pp == &(A&)qq, "");
47a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(&(Aa&)pp != &(Aa&)qq, "");
48a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
49a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr V *v = p;
50a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr V *w = q;
51a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr V *x = (A*)p;
52a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(v == w, "");
53a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(v == x, "");
54a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
55a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((U*)&d == p, "");
56a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((U*)&d == q, "");
57a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((U*)&d == v, "");
58a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((U*)&d == w, "");
59a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((U*)&d == x, "");
60a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
61a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct X {};
62a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct Y1 : virtual X {};
63a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct Y2 : X {};
64a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct Z : Y1, Y2 {};
65a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  Z z;
66a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
67a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
68a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
69a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
70a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace ConstCast {
71a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
72a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int n1 = 0;
73a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int n2 = const_cast<int&>(n1);
74a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int *n3 = const_cast<int*>(&n1);
75a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int n4 = *const_cast<int*>(&n1);
76a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr const int * const *n5 = const_cast<const int* const*>(&n3);
77a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int **n6 = const_cast<int**>(&n3);
78a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int n7 = **n5;
79a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int n8 = **n6;
80a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
81a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
82a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
83a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace TemplateArgumentConversion {
84a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  template<int n> struct IntParam {};
85a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
86a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  using IntParam0 = IntParam<0>;
87a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  // FIXME: This should be accepted once we implement the new ICE rules.
88a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  using IntParam0 = IntParam<id(0)>; // expected-error {{not an integral constant expression}}
89a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}}
90a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
91a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
92a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace CaseStatements {
93a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  void f(int n) {
94a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    switch (n) {
95a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    // FIXME: Produce the 'add ()' fixit for this.
96a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}} expected-note {{non-literal type '<bound member function type>'}}
97a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    // FIXME: This should be accepted once we implement the new ICE rules.
98a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    case id(1): // expected-error {{not an integer constant expression}} expected-note {{undefined function}}
99a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin      return;
100a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    }
101a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  }
102a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
103a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
104a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinextern int &Recurse1;
105a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinint &Recurse2 = Recurse1; // expected-note 2{{declared here}} expected-note {{initializer of 'Recurse1' is not a constant expression}}
106a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinint &Recurse1 = Recurse2; // expected-note {{declared here}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
107a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
108a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
109a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinextern const int RecurseA;
110a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconst int RecurseB = RecurseA; // expected-note {{declared here}}
111a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconst int RecurseA = 10;
112a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
113a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
114a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace MemberEnum {
115a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct WithMemberEnum {
116a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    enum E { A = 42 };
117a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  } wme;
118a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
119a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(wme.A == 42, "");
120a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
121a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
122a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace DefaultArguments {
123a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
124a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconst int z = int();
125a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
126a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  return a + b + *c + d;
127a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
128a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconst int four = 4;
129a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int eight = 8;
130a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr const int twentyseven = 27;
131a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(Sum() == 0, "");
132a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(Sum(1) == 1, "");
133a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(Sum(1, four) == 5, "");
134a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(Sum(1, eight, &twentyseven) == 36, "");
135a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(Sum(1, 2, &four, eight) == 15, "");
136a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
137a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
138a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
139a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace Ellipsis {
140a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
141a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin// Note, values passed through an ellipsis can't actually be used.
142a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinconstexpr int F(int a, ...) { return a; }
143a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(F(0) == 0, "");
144a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(F(1, 0) == 1, "");
145a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(F(2, "test") == 2, "");
146a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(F(3, &F) == 3, "");
147a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinint k = 0;
148a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinstatic_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
149a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
150a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
151a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
152a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace Recursion {
153a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
154a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(fib(11) == 89, "");
155a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
156a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr int gcd_inner(int a, int b) {
157a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    return b == 0 ? a : gcd_inner(b, a % b);
158a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  }
159a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr int gcd(int a, int b) {
160a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    return gcd_inner(max(a, b), min(a, b));
161a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  }
162a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
163a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(gcd(1749237, 5628959) == 7, "");
164a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
165a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
166a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace FunctionCast {
167a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  // When folding, we allow functions to be cast to different types. Such
168a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  // cast functions cannot be called, even if they're constexpr.
169a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr int f() { return 1; }
170a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  typedef double (*DoubleFn)();
171a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  typedef int (*IntFn)();
172a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{extension}}
173a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  int b[(int)IntFn(f)()];    // ok
174a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin}
175a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
176a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkinnamespace StaticMemberFunction {
177a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  struct S {
178a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    static constexpr int k = 42;
179a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin    static constexpr int f(int n) { return n * k + 2; }
180a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  } s;
181a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
182a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  constexpr int n = s.f(19);
183a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(S::f(19) == 800, "");
184a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(s.f(19) == 800, "");
185a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin  static_assert(n == 800, "");
186a6451827d543eb00824bc95097e47d0aac51ae93Alexander Gutkin
187  constexpr int (*sf1)(int) = &S::f;
188  constexpr int (*sf2)(int) = &s.f;
189  constexpr const int *sk = &s.k;
190}
191
192namespace ParameterScopes {
193
194  const int k = 42;
195  constexpr const int &ObscureTheTruth(const int &a) { return a; } // expected-note 3{{reference to 'a' cannot be returned from a constexpr function}}
196  constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
197    return ObscureTheTruth(b ? a : k); // expected-note 2{{in call to 'ObscureTheTruth(a)'}}
198  }
199  static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
200  constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
201
202  constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}}
203    // If ObscureTheTruth returns a reference to 'a', the result is not a
204    // constant expression even though 'a' is still in scope.
205    return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}}
206  }
207  static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
208  constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnNonstaticRef(1, 0)'}}
209
210  constexpr int InternalReturnJunk(int n) {
211    // FIXME: We should reject this: it never produces a constant expression.
212    return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
213  }
214  constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
215
216  constexpr int LToR(int &n) { return n; }
217  constexpr int GrabCallersArgument(bool which, int a, int b) {
218    return LToR(which ? b : a);
219  }
220  static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
221  static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
222
223}
224
225namespace Pointers {
226
227  constexpr int f(int n, const int *a, const int *b, const int *c) {
228    return n == 0 ? 0 : *a + f(n-1, b, c, a);
229  }
230
231  const int x = 1, y = 10, z = 100;
232  static_assert(f(23, &x, &y, &z) == 788, "");
233
234  constexpr int g(int n, int a, int b, int c) {
235    return f(n, &a, &b, &c);
236  }
237  static_assert(g(23, x, y, z) == 788, "");
238
239}
240
241namespace FunctionPointers {
242
243  constexpr int Double(int n) { return 2 * n; }
244  constexpr int Triple(int n) { return 3 * n; }
245  constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
246  constexpr int Quadruple(int n) { return Twice(Double, n); }
247  constexpr auto Select(int n) -> int (*)(int) {
248    return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
249  }
250  constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
251
252  static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
253
254  constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}}
255
256}
257
258namespace PointerComparison {
259
260int x, y;
261static_assert(&x == &y, "false"); // expected-error {{false}}
262static_assert(&x != &y, "");
263constexpr bool g1 = &x == &y;
264constexpr bool g2 = &x != &y;
265constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
266constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
267constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
268constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
269
270struct S { int x, y; } s;
271static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
272static_assert(&s.x != &s.y, "");
273static_assert(&s.x <= &s.y, "");
274static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
275static_assert(&s.x < &s.y, "");
276static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
277
278static_assert(0 == &y, "false"); // expected-error {{false}}
279static_assert(0 != &y, "");
280constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
281constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
282constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
283constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
284
285static_assert(&x == 0, "false"); // expected-error {{false}}
286static_assert(&x != 0, "");
287constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
288constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
289constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
290constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
291
292static_assert(&x == &x, "");
293static_assert(&x != &x, "false"); // expected-error {{false}}
294static_assert(&x <= &x, "");
295static_assert(&x >= &x, "");
296static_assert(&x < &x, "false"); // expected-error {{false}}
297static_assert(&x > &x, "false"); // expected-error {{false}}
298
299constexpr S* sptr = &s;
300constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
301
302struct Str {
303  // FIXME: In C++ mode, we should say 'integral' not 'integer'
304  int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
305    expected-warning {{not integer constant expression}} \
306    expected-note {{dynamic_cast is not allowed in a constant expression}}
307  int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
308    expected-warning {{not integer constant expression}} \
309    expected-note {{reinterpret_cast is not allowed in a constant expression}}
310  int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
311    expected-warning {{not integer constant expression}} \
312    expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
313  int d : (S*)(42) == (S*)(42); // \
314    expected-warning {{not integer constant expression}} \
315    expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
316  int e : (Str*)(sptr) == (Str*)(sptr); // \
317    expected-warning {{not integer constant expression}} \
318    expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
319  int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \
320    expected-warning {{not integer constant expression}} \
321    expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
322  int g : (S*)(void*)(sptr) == sptr; // \
323    expected-warning {{not integer constant expression}} \
324    expected-note {{cast from 'void *' is not allowed in a constant expression}}
325};
326
327extern char externalvar[];
328constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
329constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
330static_assert(0 != "foo", "");
331
332}
333
334namespace MaterializeTemporary {
335
336constexpr int f(const int &r) { return r; }
337constexpr int n = f(1);
338
339constexpr bool same(const int &a, const int &b) { return &a == &b; }
340constexpr bool sameTemporary(const int &n) { return same(n, n); }
341
342static_assert(n, "");
343static_assert(!same(4, 4), "");
344static_assert(same(n, n), "");
345static_assert(sameTemporary(9), "");
346
347}
348
349constexpr int strcmp_ce(const char *p, const char *q) {
350  return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
351}
352
353namespace StringLiteral {
354
355// FIXME: Refactor this once we support constexpr templates.
356constexpr int MangleChars(const char *p) {
357  return *p + 3 * (*p ? MangleChars(p+1) : 0);
358}
359constexpr int MangleChars(const char16_t *p) {
360  return *p + 3 * (*p ? MangleChars(p+1) : 0);
361}
362constexpr int MangleChars(const char32_t *p) {
363  return *p + 3 * (*p ? MangleChars(p+1) : 0);
364}
365
366static_assert(MangleChars("constexpr!") == 1768383, "");
367static_assert(MangleChars(u"constexpr!") == 1768383, "");
368static_assert(MangleChars(U"constexpr!") == 1768383, "");
369
370constexpr char c0 = "nought index"[0];
371constexpr char c1 = "nice index"[10];
372constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}}
373constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}}
374constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}}
375
376constexpr const char *p = "test" + 2;
377static_assert(*p == 's', "");
378
379constexpr const char *max_iter(const char *a, const char *b) {
380  return *a < *b ? b : a;
381}
382constexpr const char *max_element(const char *a, const char *b) {
383  return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
384}
385
386constexpr const char *begin(const char (&arr)[45]) { return arr; }
387constexpr const char *end(const char (&arr)[45]) { return arr + 45; }
388
389constexpr char str[] = "the quick brown fox jumped over the lazy dog";
390constexpr const char *max = max_element(begin(str), end(str));
391static_assert(*max == 'z', "");
392static_assert(max == str + 38, "");
393
394static_assert(strcmp_ce("hello world", "hello world") == 0, "");
395static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
396static_assert(strcmp_ce("constexpr", "test") < 0, "");
397static_assert(strcmp_ce("", " ") < 0, "");
398
399}
400
401namespace Array {
402
403// FIXME: Use templates for these once we support constexpr templates.
404constexpr int Sum(const int *begin, const int *end) {
405  return begin == end ? 0 : *begin + Sum(begin+1, end);
406}
407constexpr const int *begin(const int (&xs)[5]) { return xs; }
408constexpr const int *end(const int (&xs)[5]) { return xs + 5; }
409
410constexpr int xs[] = { 1, 2, 3, 4, 5 };
411constexpr int ys[] = { 5, 4, 3, 2, 1 };
412constexpr int sum_xs = Sum(begin(xs), end(xs));
413static_assert(sum_xs == 15, "");
414
415constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
416                       const int *xs, const int *ys, int c) {
417  return n ? F(
418               *xs, // expected-note {{subexpression not valid}}
419               *ys,
420               ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
421      expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
422      expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
423           : c;
424}
425constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
426constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
427static_assert(InnerProduct == 35, "");
428
429constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
430constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
431static_assert(DiffProd == 8, "");
432static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
433      expected-error {{constant expression}} \
434      expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
435
436constexpr const int *p = xs + 3;
437constexpr int xs4 = p[1]; // ok
438constexpr int xs5 = p[2]; // expected-error {{constant expression}}
439constexpr int xs0 = p[-3]; // ok
440constexpr int xs_1 = p[-4]; // expected-error {{constant expression}}
441
442constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
443static_assert(zs[0][0][0][0] == 1, "");
444static_assert(zs[1][1][1][1] == 16, "");
445static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
446static_assert((&zs[0][0][0][2])[-1] == 2, "");
447static_assert(**(**(zs + 1) + 1) == 11, "");
448static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, "");
449
450constexpr int fail(const int &p) {
451  return (&p)[64]; // expected-note {{subexpression}}
452}
453static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1)) == 11, ""); // \
454expected-error {{static_assert expression is not an integral constant expression}} \
455expected-note {{in call to 'fail(zs[1][0][1][0])'}}
456
457constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{extension}}
458constexpr int SumNonzero(const int *p) {
459  return *p + (*p ? SumNonzero(p+1) : 0);
460}
461constexpr int CountZero(const int *p, const int *q) {
462  return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
463}
464static_assert(SumNonzero(arr) == 6, "");
465static_assert(CountZero(arr, arr + 40) == 36, "");
466
467struct ArrayElem {
468  constexpr ArrayElem() : n(0) {}
469  int n;
470  constexpr int f() { return n; }
471};
472struct ArrayRVal {
473  constexpr ArrayRVal() {}
474  ArrayElem elems[10];
475};
476static_assert(ArrayRVal().elems[3].f() == 0, "");
477
478}
479
480namespace DependentValues {
481
482struct I { int n; typedef I V[10]; };
483I::V x, y;
484template<bool B> struct S {
485  int k;
486  void f() {
487    I::V &cells = B ? x : y;
488    I &i = cells[k];
489    switch (i.n) {}
490  }
491};
492
493}
494
495namespace Class {
496
497struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
498constexpr int fn(const A &a) { return a.k; }
499static_assert(fn(A(4,5)) == 9, "");
500
501struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
502struct C {
503  constexpr C(C *this_) : m(42), n(this_->m) {} // ok
504  int m, n;
505};
506struct D {
507  C c;
508  constexpr D() : c(&c) {}
509};
510static_assert(D().c.n == 42, "");
511
512struct E { // expected-note {{here}}
513  constexpr E() : p(&p) {} // expected-note {{pointer to temporary cannot be used to initialize a member in a constant expression}}
514  void *p;
515};
516constexpr const E &e1 = E(); // expected-error {{constant expression}} expected-note {{in call to 'E()'}} expected-note {{temporary created here}}
517// This is a constant expression if we elide the copy constructor call, and
518// is not a constant expression if we don't! But we do, so it is.
519// FIXME: The move constructor is not currently implicitly defined as constexpr.
520constexpr E e2 = E(); // unexpected-error {{constant expression}} unexpected-note {{here}} unexpected-note {{non-constexpr constructor 'E' cannot be used in a constant expression}}
521static_assert(e2.p == &e2.p, ""); // unexpected-error {{constant expression}} unexpected-note {{initializer of 'e2' is not a constant expression}}
522constexpr E e3;
523static_assert(e3.p == &e3.p, "");
524
525extern const class F f;
526struct F {
527  constexpr F() : p(&f.p) {}
528  const void *p;
529};
530constexpr F f;
531
532struct G {
533  struct T {
534    constexpr T(T *p) : u1(), u2(p) {}
535    union U1 {
536      constexpr U1() {}
537      int a, b = 42;
538    } u1;
539    union U2 {
540      constexpr U2(T *p) : c(p->u1.b) {}
541      int c, d;
542    } u2;
543  } t;
544  constexpr G() : t(&t) {}
545} constexpr g;
546
547static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
548static_assert(g.t.u1.b == 42, "");
549static_assert(g.t.u2.c == 42, "");
550static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
551
552struct S {
553  int a, b;
554  const S *p;
555  double d;
556  const char *q;
557
558  constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
559};
560
561S global(43, &global);
562
563static_assert(S(15, &global).b == 15, "");
564
565constexpr bool CheckS(const S &s) {
566  return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
567}
568static_assert(CheckS(S(27, &global)), "");
569
570struct Arr {
571  char arr[3];
572  constexpr Arr() : arr{'x', 'y', 'z'} {}
573};
574constexpr int hash(Arr &&a) {
575  return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
576}
577constexpr int k = hash(Arr());
578static_assert(k == 0x007a7978, "");
579
580
581struct AggregateInit {
582  const char &c;
583  int n;
584  double d;
585  int arr[5];
586  void *p;
587};
588
589constexpr AggregateInit agg1 = { "hello"[0] };
590
591static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
592static_assert(agg1.n == 0, "");
593static_assert(agg1.d == 0.0, "");
594static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
595static_assert(agg1.arr[0] == 0, "");
596static_assert(agg1.arr[4] == 0, "");
597static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
598static_assert(agg1.p == nullptr, "");
599
600namespace SimpleDerivedClass {
601
602struct B {
603  constexpr B(int n) : a(n) {}
604  int a;
605};
606struct D : B {
607  constexpr D(int n) : B(n) {}
608};
609constexpr D d(3);
610static_assert(d.a == 3, "");
611
612}
613
614struct Bottom { constexpr Bottom() {} };
615struct Base : Bottom {
616  constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
617  int a;
618  const char *b;
619};
620struct Base2 : Bottom {
621  constexpr Base2(const int &r) : r(r) {}
622  int q = 123;
623  const int &r;
624};
625struct Derived : Base, Base2 {
626  constexpr Derived() : Base(76), Base2(a) {}
627  int c = r + b[1];
628};
629
630constexpr bool operator==(const Base &a, const Base &b) {
631  return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
632}
633
634constexpr Base base;
635constexpr Base base2(76);
636constexpr Derived derived;
637static_assert(derived.a == 76, "");
638static_assert(derived.b[2] == 's', "");
639static_assert(derived.c == 76 + 'e', "");
640static_assert(derived.q == 123, "");
641static_assert(derived.r == 76, "");
642static_assert(&derived.r == &derived.a, "");
643
644static_assert(!(derived == base), "");
645static_assert(derived == base2, "");
646
647constexpr Bottom &bot1 = (Base&)derived;
648constexpr Bottom &bot2 = (Base2&)derived;
649static_assert(&bot1 != &bot2, "");
650
651constexpr Bottom *pb1 = (Base*)&derived;
652constexpr Bottom *pb2 = (Base2*)&derived;
653static_assert(pb1 != pb2, "");
654static_assert(pb1 == &bot1, "");
655static_assert(pb2 == &bot2, "");
656
657constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}}
658constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}}
659constexpr Base2 &ok2 = (Base2&)bot2;
660static_assert(&ok2 == &derived, "");
661
662constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}}
663constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}}
664constexpr Base2 *pok2 = (Base2*)pb2;
665static_assert(pok2 == &derived, "");
666static_assert(&ok2 == pok2, "");
667static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
668static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
669
670constexpr Base *nullB = 42 - 6 * 7;
671static_assert((Bottom*)nullB == 0, "");
672static_assert((Derived*)nullB == 0, "");
673static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
674
675}
676
677namespace Temporaries {
678
679struct S {
680  constexpr S() {}
681  constexpr int f();
682};
683struct T : S {
684  constexpr T(int n) : S(), n(n) {}
685  int n;
686};
687constexpr int S::f() {
688  // 'this' must be the postfix-expression in a class member access expression,
689  // so we can't just use
690  //   return static_cast<T*>(this)->n;
691  return this->*(int(S::*))&T::n;
692}
693// The T temporary is implicitly cast to an S subobject, but we can recover the
694// T full-object via a base-to-derived cast, or a derived-to-base-casted member
695// pointer.
696static_assert(T(3).f() == 3, "");
697
698constexpr int f(const S &s) {
699  return static_cast<const T&>(s).n;
700}
701constexpr int n = f(T(5));
702static_assert(f(T(5)) == 5, "");
703
704}
705
706namespace Union {
707
708union U {
709  int a;
710  int b;
711};
712
713constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{extension}}
714static_assert(u[0].a == 0, "");
715static_assert(u[0].b, ""); // expected-error {{constant expression}}
716static_assert(u[1].b == 1, "");
717static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
718static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
719static_assert((&(u[1]) + 1 + 1)->b == 3, "");
720
721}
722
723namespace MemberPointer {
724  struct A {
725    constexpr A(int n) : n(n) {}
726    int n;
727    constexpr int f() { return n + 3; }
728  };
729  constexpr A a(7);
730  static_assert(A(5).*&A::n == 5, "");
731  static_assert((&a)->*&A::n == 7, "");
732  static_assert((A(8).*&A::f)() == 11, "");
733  static_assert(((&a)->*&A::f)() == 10, "");
734
735  struct B : A {
736    constexpr B(int n, int m) : A(n), m(m) {}
737    int m;
738    constexpr int g() { return n + m + 1; }
739  };
740  constexpr B b(9, 13);
741  static_assert(B(4, 11).*&A::n == 4, "");
742  static_assert(B(4, 11).*&B::m == 11, "");
743  static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
744  static_assert((&b)->*&A::n == 9, "");
745  static_assert((&b)->*&B::m == 13, "");
746  static_assert((&b)->*(int(A::*))&B::m == 13, "");
747  static_assert((B(4, 11).*&A::f)() == 7, "");
748  static_assert((B(4, 11).*&B::g)() == 16, "");
749  static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
750  static_assert(((&b)->*&A::f)() == 12, "");
751  static_assert(((&b)->*&B::g)() == 23, "");
752  static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
753
754  struct S {
755    constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
756      m(m), n(n), pf(pf), pn(pn) {}
757    constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
758
759    constexpr int f() { return this->*pn; }
760    virtual int g() const;
761
762    int m, n;
763    int (S::*pf)() const;
764    int S::*pn;
765  };
766
767  constexpr int S::*pm = &S::m;
768  constexpr int S::*pn = &S::n;
769  constexpr int (S::*pf)() const = &S::f;
770  constexpr int (S::*pg)() const = &S::g;
771
772  constexpr S s(2, 5, &S::f, &S::m);
773
774  static_assert((s.*&S::f)() == 2, "");
775  static_assert((s.*s.pf)() == 2, "");
776
777  template<int n> struct T : T<n-1> {};
778  template<> struct T<0> { int n; };
779  template<> struct T<30> : T<29> { int m; };
780
781  T<17> t17;
782  T<30> t30;
783
784  constexpr int (T<10>::*deepn) = &T<0>::n;
785  static_assert(&(t17.*deepn) == &t17.n, "");
786
787  constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
788  constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
789  static_assert(&(t30.*deepm) == &t30.m, "");
790
791  constexpr T<5> *p17_5 = &t17;
792  constexpr T<13> *p17_13 = (T<13>*)p17_5;
793  constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}}
794  static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
795  static_assert(&(p17_13->*deepn) == &t17.n, "");
796  constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
797
798  constexpr T<5> *p30_5 = &t30;
799  constexpr T<23> *p30_23 = (T<23>*)p30_5;
800  constexpr T<13> *p30_13 = p30_23;
801  static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
802  static_assert(&(p30_13->*deepn) == &t30.n, "");
803  static_assert(&(p30_23->*deepn) == &t30.n, "");
804  static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
805  static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
806  static_assert(&(p30_23->*deepm) == &t30.m, "");
807}
808
809namespace ArrayBaseDerived {
810
811  struct Base {
812    constexpr Base() {}
813    int n = 0;
814  };
815  struct Derived : Base {
816    constexpr Derived() {}
817    constexpr const int *f() { return &n; }
818  };
819
820  constexpr Derived a[10];
821  constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
822  constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
823  static_assert(pb3 == pd3, "");
824
825  // pb3 does not point to an array element.
826  constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
827  constexpr int pb4n = pb4->n; // expected-error {{constant expression}}
828  constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this.
829  constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}}
830  constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this.
831  constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}}
832  constexpr Base *pb3a = pb4 - 1;
833
834  // pb4 does not point to a Derived.
835  constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}}
836  constexpr Derived *pd3a = (Derived*)pb3a;
837  constexpr int pd3n = pd3a->n;
838
839  // pd3a still points to the Derived array.
840  constexpr Derived *pd6 = pd3a + 3;
841  static_assert(pd6 == &a[6], "");
842  constexpr Derived *pd9 = pd6 + 3;
843  constexpr Derived *pd10 = pd6 + 4;
844  constexpr int pd9n = pd9->n; // ok
845  constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}}
846  constexpr int pd0n = pd10[-10].n;
847  constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}}
848
849  constexpr Base *pb9 = pd9;
850  constexpr const int *(Base::*pfb)() const =
851      static_cast<const int *(Base::*)() const>(&Derived::f);
852  static_assert((pb9->*pfb)() == &a[9].n, "");
853}
854
855namespace Complex {
856
857class complex {
858  int re, im;
859public:
860  constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
861  constexpr complex(const complex &o) : re(o.re), im(o.im) {}
862  constexpr complex operator-() const { return complex(-re, -im); }
863  friend constexpr complex operator+(const complex &l, const complex &r) {
864    return complex(l.re + r.re, l.im + r.im);
865  }
866  friend constexpr complex operator-(const complex &l, const complex &r) {
867    return l + -r;
868  }
869  friend constexpr complex operator*(const complex &l, const complex &r) {
870    return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
871  }
872  friend constexpr bool operator==(const complex &l, const complex &r) {
873    return l.re == r.re && l.im == r.im;
874  }
875  constexpr bool operator!=(const complex &r) const {
876    return re != r.re || im != r.im;
877  }
878  constexpr int real() const { return re; }
879  constexpr int imag() const { return im; }
880};
881
882constexpr complex i = complex(0, 1);
883constexpr complex k = (3 + 4*i) * (6 - 4*i);
884static_assert(complex(1,0).real() == 1, "");
885static_assert(complex(1,0).imag() == 0, "");
886static_assert(((complex)1).imag() == 0, "");
887static_assert(k.real() == 34, "");
888static_assert(k.imag() == 12, "");
889static_assert(k - 34 == 12*i, "");
890static_assert((complex)1 == complex(1), "");
891static_assert((complex)1 != complex(0, 1), "");
892static_assert(complex(1) == complex(1), "");
893static_assert(complex(1) != complex(0, 1), "");
894constexpr complex makeComplex(int re, int im) { return complex(re, im); }
895static_assert(makeComplex(1,0) == complex(1), "");
896static_assert(makeComplex(1,0) != complex(0, 1), "");
897
898class complex_wrap : public complex {
899public:
900  constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
901  constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
902};
903
904static_assert((complex_wrap)1 == complex(1), "");
905static_assert((complex)1 != complex_wrap(0, 1), "");
906static_assert(complex(1) == complex_wrap(1), "");
907static_assert(complex_wrap(1) != complex(0, 1), "");
908constexpr complex_wrap makeComplexWrap(int re, int im) {
909  return complex_wrap(re, im);
910}
911static_assert(makeComplexWrap(1,0) == complex(1), "");
912static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
913
914}
915
916namespace PR11595 {
917  struct A { constexpr bool operator==(int x) { return true; } };
918  struct B { B(); A& x; };
919  static_assert(B().x == 3, "");  // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
920
921  constexpr bool f(int k) {
922    return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
923  }
924  constexpr int n = f(1); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'f(1)'}}
925}
926
927namespace ExprWithCleanups {
928  struct A { A(); ~A(); int get(); };
929  constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
930  constexpr int n = get(false);
931}
932