1651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// RUN: %clang_cc1 -triple i686-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -fsyntax-only -fcxx-exceptions -verify -std=c++11 -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace StaticAssertFoldTest {
459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithint x;
69eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(++x, "test"); // expected-error {{not an integral constant expression}}
79eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(false, "test"); // expected-error {{test}}
859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
1059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
111d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtypedef decltype(sizeof(char)) size_t;
121d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
131d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate<typename T> constexpr T id(const T &t) { return t; }
141d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate<typename T> constexpr T min(const T &a, const T &b) {
151d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  return a < b ? a : b;
161d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith}
171d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate<typename T> constexpr T max(const T &a, const T &b) {
181d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  return a < b ? b : a;
191d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith}
201d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; }
211d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; }
2259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct MemberZero {
24840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr int zero() const { return 0; }
2559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith};
2659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace DerivedToVBaseCast {
2859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct U { int n; };
3059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct V : U { int n; };
3159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct A : virtual V { int n; };
3259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct Aa { int n; };
3359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct B : virtual A, Aa {};
3459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct C : virtual A, Aa {};
3559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct D : B, C {};
3659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
3759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  D d;
3859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr B *p = &d;
3959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr C *q = &d;
40f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
419eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((void*)p != (void*)q, "");
429eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((A*)p == (A*)q, "");
439eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((Aa*)p != (Aa*)q, "");
4459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
4559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr B &pp = d;
4659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr C &qq = d;
479eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((void*)&pp != (void*)&qq, "");
489eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(&(A&)pp == &(A&)qq, "");
499eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(&(Aa&)pp != &(Aa&)qq, "");
5059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
5159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr V *v = p;
5259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr V *w = q;
5359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr V *x = (A*)p;
549eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(v == w, "");
559eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(v == x, "");
5659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
579eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((U*)&d == p, "");
589eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((U*)&d == q, "");
599eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((U*)&d == v, "");
609eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((U*)&d == w, "");
619eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((U*)&d == x, "");
6259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
6359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct X {};
6459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct Y1 : virtual X {};
6559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct Y2 : X {};
6659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct Z : Y1, Y2 {};
6759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  Z z;
689eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
6959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
7059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
71f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithnamespace ConstCast {
72f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith
73f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithconstexpr int n1 = 0;
74f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithconstexpr int n2 = const_cast<int&>(n1);
75f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithconstexpr int *n3 = const_cast<int*>(&n1);
76f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithconstexpr int n4 = *const_cast<int*>(&n1);
77f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithconstexpr const int * const *n5 = const_cast<const int* const*>(&n3);
78f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithconstexpr int **n6 = const_cast<int**>(&n3);
79f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithconstexpr int n7 = **n5;
80f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smithconstexpr int n8 = **n6;
81f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith
8241cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith// const_cast from prvalue to xvalue.
8341cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smithstruct A { int n; };
8441cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smithconstexpr int n9 = (const_cast<A&&>(A{123})).n;
8541cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smithstatic_assert(n9 == 123, "");
8641cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith
87f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith}
88f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith
8959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace TemplateArgumentConversion {
9059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  template<int n> struct IntParam {};
9159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
9259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  using IntParam0 = IntParam<0>;
931d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  using IntParam0 = IntParam<id(0)>;
946180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}}
9559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
9659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
9759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace CaseStatements {
9859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  void f(int n) {
9959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    switch (n) {
1008ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}}
1018ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case id(0): // expected-error {{duplicate case value '0'}}
10259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      return;
10359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    }
10459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
10559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
10659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
10759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithextern int &Recurse1;
10816581335fc32abcbc6ab14eda7af38cf759664b7Richard Smithint &Recurse2 = Recurse1; // expected-note {{declared here}}
10916581335fc32abcbc6ab14eda7af38cf759664b7Richard Smithint &Recurse1 = Recurse2;
110099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
111099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
112099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithextern const int RecurseA;
113099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconst int RecurseB = RecurseA; // expected-note {{declared here}}
114099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconst int RecurseA = 10;
115099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
11659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
11759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace MemberEnum {
11859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct WithMemberEnum {
11959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    enum E { A = 42 };
12059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  } wme;
12159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1229eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(wme.A == 42, "");
12359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
12459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
12559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace DefaultArguments {
12659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
12759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconst int z = int();
12859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
12959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return a + b + *c + d;
13059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
13159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconst int four = 4;
13259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int eight = 8;
13359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr const int twentyseven = 27;
1349eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(Sum() == 0, "");
1359eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(Sum(1) == 1, "");
1369eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(Sum(1, four) == 5, "");
1379eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(Sum(1, eight, &twentyseven) == 36, "");
1389eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(Sum(1, 2, &four, eight) == 15, "");
13959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
14059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
14159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
14259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace Ellipsis {
14359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
14459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith// Note, values passed through an ellipsis can't actually be used.
14559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int F(int a, ...) { return a; }
1469eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(F(0) == 0, "");
1479eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(F(1, 0) == 1, "");
1489eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(F(2, "test") == 2, "");
1499eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(F(3, &F) == 3, "");
1507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithint k = 0; // expected-note {{here}}
1517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithstatic_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
15259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
15359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
15459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
15559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace Recursion {
15659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
1579eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(fib(11) == 89, "");
15859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
15959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int gcd_inner(int a, int b) {
16059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return b == 0 ? a : gcd_inner(b, a % b);
16159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
16259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int gcd(int a, int b) {
16359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return gcd_inner(max(a, b), min(a, b));
16459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
16559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1669eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(gcd(1749237, 5628959) == 7, "");
16759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
16859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
16959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace FunctionCast {
17059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  // When folding, we allow functions to be cast to different types. Such
17159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  // cast functions cannot be called, even if they're constexpr.
17259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int f() { return 1; }
17359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  typedef double (*DoubleFn)();
17459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  typedef int (*IntFn)();
175d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smith  int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
17659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  int b[(int)IntFn(f)()];    // ok
17759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
17859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
17959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace StaticMemberFunction {
18059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct S {
18159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    static constexpr int k = 42;
18259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    static constexpr int f(int n) { return n * k + 2; }
18359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  } s;
18459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
18559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int n = s.f(19);
1869eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(S::f(19) == 800, "");
1879eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(s.f(19) == 800, "");
1889eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(n == 800, "");
1891bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith
1901bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  constexpr int (*sf1)(int) = &S::f;
1911bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  constexpr int (*sf2)(int) = &s.f;
1921bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  constexpr const int *sk = &s.k;
19359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
19459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
19559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace ParameterScopes {
19659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
19759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  const int k = 42;
19883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  constexpr const int &ObscureTheTruth(const int &a) { return a; }
199099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
20083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return ObscureTheTruth(b ? a : k);
20159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
2029eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
20383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
20459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
20583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
20683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return ObscureTheTruth(b ? a : k);
20759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
2089eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
20983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok
21059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
21159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int InternalReturnJunk(int n) {
21283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return MaybeReturnJunk(true, n); // expected-note {{read of variable whose lifetime has ended}}
21359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
214099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
21559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
21659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int LToR(int &n) { return n; }
21759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int GrabCallersArgument(bool which, int a, int b) {
21859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return LToR(which ? b : a);
21959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
2209eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
2219eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
22259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
22359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
22459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
22559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace Pointers {
22659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
22759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int f(int n, const int *a, const int *b, const int *c) {
22859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return n == 0 ? 0 : *a + f(n-1, b, c, a);
22959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
23059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
23159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  const int x = 1, y = 10, z = 100;
2329eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(f(23, &x, &y, &z) == 788, "");
23359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
23459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int g(int n, int a, int b, int c) {
23559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return f(n, &a, &b, &c);
23659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
2379eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(g(23, x, y, z) == 788, "");
23859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
23959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
24059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
24159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace FunctionPointers {
24259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
24359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int Double(int n) { return 2 * n; }
24459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int Triple(int n) { return 3 * n; }
24559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
24659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr int Quadruple(int n) { return Twice(Double, n); }
24759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr auto Select(int n) -> int (*)(int) {
24859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
24959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
250099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
25159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2529eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith  static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
25359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
254099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}}
25559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
25659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
25759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
25859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace PointerComparison {
25959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
26059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithint x, y;
2619eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x == &y, "false"); // expected-error {{false}}
2629eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x != &y, "");
26359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool g1 = &x == &y;
26459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool g2 = &x != &y;
26559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
26659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
26759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
26859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
26959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
27059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct S { int x, y; } s;
2719eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&s.x == &s.y, "false"); // expected-error {{false}}
2729eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&s.x != &s.y, "");
2739eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&s.x <= &s.y, "");
2749eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
2759eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&s.x < &s.y, "");
2769eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&s.x > &s.y, "false"); // expected-error {{false}}
2779eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smith
2789eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(0 == &y, "false"); // expected-error {{false}}
2799eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(0 != &y, "");
28059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
28159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
28259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
28359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
28459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2859eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x == 0, "false"); // expected-error {{false}}
2869eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x != 0, "");
28759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
28859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
28959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
29059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
29159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2929eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x == &x, "");
2939eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x != &x, "false"); // expected-error {{false}}
2949eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x <= &x, "");
2959eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x >= &x, "");
2969eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x < &x, "false"); // expected-error {{false}}
2979eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(&x > &x, "false"); // expected-error {{false}}
29859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
29959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr S* sptr = &s;
300099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
30159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
3022fd5983e0da447291a651a347c206aee37a1de5fRichard Smithstruct U {};
303c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smithstruct Str {
304c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
305244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    expected-warning {{not an integral constant expression}} \
3064cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    expected-note {{dynamic_cast is not allowed in a constant expression}}
307c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
308244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    expected-warning {{not an integral constant expression}} \
3094cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    expected-note {{reinterpret_cast is not allowed in a constant expression}}
310c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
311244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    expected-warning {{not an integral constant expression}} \
312bebf5b1bcfbf591dd3cd80c4aebd6486bb34f41cRichard Smith    expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
313c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  int d : (S*)(42) == (S*)(42); // \
314244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    expected-warning {{not an integral constant expression}} \
315bebf5b1bcfbf591dd3cd80c4aebd6486bb34f41cRichard Smith    expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
316c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  int e : (Str*)(sptr) == (Str*)(sptr); // \
317244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    expected-warning {{not an integral constant expression}} \
318bebf5b1bcfbf591dd3cd80c4aebd6486bb34f41cRichard Smith    expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
3192fd5983e0da447291a651a347c206aee37a1de5fRichard Smith  int f : &(U&)(*sptr) == &(U&)(*sptr); // \
320244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    expected-warning {{not an integral constant expression}} \
321bebf5b1bcfbf591dd3cd80c4aebd6486bb34f41cRichard Smith    expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
322c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  int g : (S*)(void*)(sptr) == sptr; // \
323244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith    expected-warning {{not an integral constant expression}} \
3244cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    expected-note {{cast from 'void *' is not allowed in a constant expression}}
325c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith};
326c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
32759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithextern char externalvar[];
3289eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithconstexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
32959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
3309eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(0 != "foo", "");
33159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
33259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
33359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
33459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace MaterializeTemporary {
33559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
33659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int f(const int &r) { return r; }
33759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int n = f(1);
33859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
33959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool same(const int &a, const int &b) { return &a == &b; }
34059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool sameTemporary(const int &n) { return same(n, n); }
34159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
3429eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(n, "");
3439eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(!same(4, 4), "");
3449eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(same(n, n), "");
3459eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(sameTemporary(9), "");
34659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
347211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithstruct A { int &&r; };
348211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithstruct B { A &&a1; A &&a2; };
349211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith
350211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithconstexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}}
351211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithstatic_assert(&b1.a1 != &b1.a2, "");
352211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithstatic_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
353211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith
354211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithconstexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}}
355211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithstatic_assert(&b1 != &b2, "");
356211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithstatic_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
357211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith
358211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithconstexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
359211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithvoid foo() {
360211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith  constexpr static B b1 { { 1 }, { 2 } }; // ok
361211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith  constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
362211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith  constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
363211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith}
364211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith
365211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithconstexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} }); // expected-warning 4{{unused}}
366211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithstatic_assert(&b4 != &b2, "");
367211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith
368211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith// Proposed DR: copy-elision doesn't trigger lifetime extension.
369211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithconstexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
370211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith
371f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smithnamespace NestedNonStatic {
372f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith  // Proposed DR: for a reference constant expression to refer to a static
373f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith  // storage duration temporary, that temporary must itself be initialized
374f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith  // by a constant expression (a core constant expression is not enough).
375f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith  struct A { int &&r; };
376f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith  struct B { A &&a; };
377f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith  constexpr B a = { A{0} }; // ok
378f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith  constexpr B b = { A(A{0}) }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
379f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith}
380f69dd33cfc0b1f2a6c88d10951dbd29c4dc806c2Richard Smith
381b6e3808bfe385da8d90bb431e41e30d721d5433fRichard Smithnamespace FakeInitList {
382b6e3808bfe385da8d90bb431e41e30d721d5433fRichard Smith  struct init_list_3_ints { const int (&x)[3]; };
383b6e3808bfe385da8d90bb431e41e30d721d5433fRichard Smith  struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; };
384b6e3808bfe385da8d90bb431e41e30d721d5433fRichard Smith  constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
385b6e3808bfe385da8d90bb431e41e30d721d5433fRichard Smith}
386b6e3808bfe385da8d90bb431e41e30d721d5433fRichard Smith
38759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
38859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
38959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int strcmp_ce(const char *p, const char *q) {
39059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
39159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
39259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
39359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace StringLiteral {
39459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
3951d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate<typename Char>
3961d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithconstexpr int MangleChars(const Char *p) {
39759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return *p + 3 * (*p ? MangleChars(p+1) : 0);
39859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
39959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
4009eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(MangleChars("constexpr!") == 1768383, "");
401ec789163a42a7be654ac34aadb750b508954d53cRichard Smithstatic_assert(MangleChars(u8"constexpr!") == 1768383, "");
402ec789163a42a7be654ac34aadb750b508954d53cRichard Smithstatic_assert(MangleChars(L"constexpr!") == 1768383, "");
4039eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(MangleChars(u"constexpr!") == 1768383, "");
4049eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(MangleChars(U"constexpr!") == 1768383, "");
40559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
40659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr char c0 = "nought index"[0];
40759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr char c1 = "nice index"[10];
4087098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithconstexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}} expected-note {{read of dereferenced one-past-the-end pointer}}
409b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithconstexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{cannot refer to element -1 of array of 15 elements}}
410bebf5b1bcfbf591dd3cd80c4aebd6486bb34f41cRichard Smithconstexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast that performs the conversions of a reinterpret_cast}}
41159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
41259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr const char *p = "test" + 2;
4139eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(*p == 's', "");
41459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
41559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr const char *max_iter(const char *a, const char *b) {
41659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return *a < *b ? b : a;
41759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
41859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr const char *max_element(const char *a, const char *b) {
41959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
42059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
42159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
42259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr char str[] = "the quick brown fox jumped over the lazy dog";
42359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr const char *max = max_element(begin(str), end(str));
4249eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(*max == 'z', "");
4259eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(max == str + 38, "");
42659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
4279eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(strcmp_ce("hello world", "hello world") == 0, "");
4289eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(strcmp_ce("hello world", "hello clang") > 0, "");
4299eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(strcmp_ce("constexpr", "test") < 0, "");
4309eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(strcmp_ce("", " ") < 0, "");
43159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
4327098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithstruct S {
4337098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  int n : "foo"[4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
4347098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith};
4357098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
436974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithstruct T {
437974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  char c[6];
438974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  constexpr T() : c{"foo"} {}
439974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith};
440974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithconstexpr T t;
441974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith
442974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithstatic_assert(t.c[0] == 'f', "");
443974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithstatic_assert(t.c[1] == 'o', "");
444974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithstatic_assert(t.c[2] == 'o', "");
445974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithstatic_assert(t.c[3] == 0, "");
446974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithstatic_assert(t.c[4] == 0, "");
447974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithstatic_assert(t.c[5] == 0, "");
448974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smithstatic_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
449974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith
450ec789163a42a7be654ac34aadb750b508954d53cRichard Smithstruct U {
451ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  wchar_t chars[6];
452ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  int n;
453ec789163a42a7be654ac34aadb750b508954d53cRichard Smith} constexpr u = { { L"test" }, 0 };
454ec789163a42a7be654ac34aadb750b508954d53cRichard Smithstatic_assert(u.chars[2] == L's', "");
455ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
456f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smithstruct V {
457f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  char c[4];
458f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  constexpr V() : c("hi!") {}
459f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith};
460f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smithstatic_assert(V().c[1] == "i"[0], "");
461f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith
46230ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smithnamespace Parens {
46330ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")},
46430ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith                          d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")};
46530ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  static_assert(a[0] == 'f', "");
46630ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  static_assert(b[1] == 'o', "");
46730ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  static_assert(c[2] == 'o', "");
46830ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  static_assert(d[0] == 'f', "");
46930ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  static_assert(e[1] == 'o', "");
47030ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  static_assert(f[2] == 'o', "");
47130ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  static_assert(f[5] == 0, "");
47230ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith  static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
47330ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith}
47430ae1ed71dc99a396b7ddba9cabe9846a0c4449dRichard Smith
47559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
47659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
47759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace Array {
47859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
4791d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithtemplate<typename Iter>
4801d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithconstexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
48159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return begin == end ? 0 : *begin + Sum(begin+1, end);
48259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
48359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
48459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int xs[] = { 1, 2, 3, 4, 5 };
48559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int ys[] = { 5, 4, 3, 2, 1 };
48659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int sum_xs = Sum(begin(xs), end(xs));
4879eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(sum_xs == 15, "");
48859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
48959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
49059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                       const int *xs, const int *ys, int c) {
491daaefc5381f9aafbb1cb6f88fb5ac6aaf34d65bfRichard Smith  return n ? F(
4927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith               *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
49308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith               *ys,
49408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith               ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
49508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
49608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
49708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith           : c;
49859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
49959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int MulAdd(int x, int y, int c) { return x * y + c; }
50059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
5019eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(InnerProduct == 35, "");
50259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
50359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
50459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
5059eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(DiffProd == 8, "");
50608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithstatic_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
50708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      expected-error {{constant expression}} \
50808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
50959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
51059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr const int *p = xs + 3;
51159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int xs4 = p[1]; // ok
5127098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithconstexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
513b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithconstexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
51459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int xs0 = p[-3]; // ok
515b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithconstexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
51659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
51759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
5189eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(zs[0][0][0][0] == 1, "");
5199eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(zs[1][1][1][1] == 16, "");
5207098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithstatic_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
5219eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert((&zs[0][0][0][2])[-1] == 2, "");
5229eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(**(**(zs + 1) + 1) == 11, "");
523b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of array of 2 elements in a constant expression}}
524b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
525b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithconstexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // expected-error {{constant expression}} expected-note {{cannot access array element of pointer past the end}}
52659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
52708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithconstexpr int fail(const int &p) {
528b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
52908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
530b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
53108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithexpected-error {{static_assert expression is not an integral constant expression}} \
53208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithexpected-note {{in call to 'fail(zs[1][0][1][0])'}}
53308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
534d7c56e1114bfe7d461786903bb720d2c6efc05a1Richard Smithconstexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
53559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int SumNonzero(const int *p) {
53659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return *p + (*p ? SumNonzero(p+1) : 0);
53759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
53859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int CountZero(const int *p, const int *q) {
53959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
54059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
5419eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(SumNonzero(arr) == 6, "");
5429eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(CountZero(arr, arr + 40) == 36, "");
54359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
544e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstruct ArrayElem {
545e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  constexpr ArrayElem() : n(0) {}
546e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  int n;
547840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith  constexpr int f() const { return n; }
548e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
549e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstruct ArrayRVal {
550e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  constexpr ArrayRVal() {}
551e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  ArrayElem elems[10];
552e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
5539eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(ArrayRVal().elems[3].f() == 0, "");
554e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
555de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithconstexpr int selfref[2][2][2] = {
556de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  selfref[1][1][1] + 1, selfref[0][0][0] + 1,
557de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  selfref[1][0][1] + 1, selfref[0][1][0] + 1,
558de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  selfref[1][0][0] + 1, selfref[0][1][1] + 1 };
559de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithstatic_assert(selfref[0][0][0] == 1, "");
560de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithstatic_assert(selfref[0][0][1] == 2, "");
561de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithstatic_assert(selfref[0][1][0] == 1, "");
562de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithstatic_assert(selfref[0][1][1] == 2, "");
563de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithstatic_assert(selfref[1][0][0] == 1, "");
564de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithstatic_assert(selfref[1][0][1] == 3, "");
565de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithstatic_assert(selfref[1][1][0] == 0, "");
566de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smithstatic_assert(selfref[1][1][1] == 0, "");
567de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
568a4334dffde250c22c339a974a7131914fe723180Richard Smithstruct TrivialDefCtor { int n; };
569a4334dffde250c22c339a974a7131914fe723180Richard Smithtypedef TrivialDefCtor TDCArray[2][2];
570a4334dffde250c22c339a974a7131914fe723180Richard Smithstatic_assert(TDCArray{}[1][1].n == 0, "");
571a4334dffde250c22c339a974a7131914fe723180Richard Smith
572a4334dffde250c22c339a974a7131914fe723180Richard Smithstruct NonAggregateTDC : TrivialDefCtor {};
573a4334dffde250c22c339a974a7131914fe723180Richard Smithtypedef NonAggregateTDC NATDCArray[2][2];
574a4334dffde250c22c339a974a7131914fe723180Richard Smithstatic_assert(NATDCArray{}[1][1].n == 0, "");
575a4334dffde250c22c339a974a7131914fe723180Richard Smith
57659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
57759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
57859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace DependentValues {
57959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
58059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct I { int n; typedef I V[10]; };
58159efe266b804330f4c1f3a1b0ff783e67dd90378Richard SmithI::V x, y;
5829568f0c13a84695c5cc064e7c32412f84604cb1eRichard Smithint g();
5831432a43e3076eec84967305bd85bace3a6e9102bRichard Smithtemplate<bool B, typename T> struct S : T {
58459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  int k;
58559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  void f() {
58659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    I::V &cells = B ? x : y;
58759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    I &i = cells[k];
58859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    switch (i.n) {}
5891432a43e3076eec84967305bd85bace3a6e9102bRichard Smith
5909568f0c13a84695c5cc064e7c32412f84604cb1eRichard Smith    // FIXME: We should be able to diagnose this.
5919568f0c13a84695c5cc064e7c32412f84604cb1eRichard Smith    constexpr int n = g();
5921432a43e3076eec84967305bd85bace3a6e9102bRichard Smith
5931432a43e3076eec84967305bd85bace3a6e9102bRichard Smith    constexpr int m = this->g(); // ok, could be constexpr
59459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
59559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith};
59659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
59759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
59859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
59959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithnamespace Class {
60059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
60159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct A { constexpr A(int a, int b) : k(a + b) {} int k; };
60259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int fn(const A &a) { return a.k; }
6039eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(fn(A(4,5)) == 9, "");
60459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
60559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
60659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct C {
60759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr C(C *this_) : m(42), n(this_->m) {} // ok
60859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  int m, n;
60959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith};
61059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct D {
61159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  C c;
61259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr D() : c(&c) {}
61359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith};
6149eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(D().c.n == 42, "");
61559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
6166180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstruct E {
61783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  constexpr E() : p(&p) {}
61859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  void *p;
61959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith};
620211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smithconstexpr const E &e1 = E();
62159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith// This is a constant expression if we elide the copy constructor call, and
62259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith// is not a constant expression if we don't! But we do, so it is.
6236180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithconstexpr E e2 = E();
6246180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstatic_assert(e2.p == &e2.p, "");
625099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr E e3;
6269eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(e3.p == &e3.p, "");
62759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
62859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithextern const class F f;
62959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct F {
63059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr F() : p(&f.p) {}
63159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  const void *p;
63259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith};
633099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithconstexpr F f;
63459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
63559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct G {
63659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  struct T {
63759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    constexpr T(T *p) : u1(), u2(p) {}
63859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    union U1 {
63959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      constexpr U1() {}
64059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      int a, b = 42;
64159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } u1;
64259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    union U2 {
64359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      constexpr U2(T *p) : c(p->u1.b) {}
64459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      int c, d;
64559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } u2;
64659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  } t;
64759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr G() : t(&t) {}
64859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} constexpr g;
64959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
6507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithstatic_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
6519eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(g.t.u1.b == 42, "");
6529eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(g.t.u2.c == 42, "");
6537098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithstatic_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
65459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
65559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct S {
65659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  int a, b;
65759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  const S *p;
65859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  double d;
65959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  const char *q;
66059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
66159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
66259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith};
66359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
66459efe266b804330f4c1f3a1b0ff783e67dd90378Richard SmithS global(43, &global);
66559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
6669eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(S(15, &global).b == 15, "");
66759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
66859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr bool CheckS(const S &s) {
66959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
67059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
6719eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(CheckS(S(27, &global)), "");
67259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
67359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct Arr {
67459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  char arr[3];
67559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  constexpr Arr() : arr{'x', 'y', 'z'} {}
67659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith};
67759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int hash(Arr &&a) {
67859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
67959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
68059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithconstexpr int k = hash(Arr());
6819eed49c2bb0f37bbfefefd0998b6303a686a66c0Richard Smithstatic_assert(k == 0x007a7978, "");
68259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
68359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
68459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstruct AggregateInit {
68559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  const char &c;
686