1*f4a2713aSLionel Sambuc // 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 2*f4a2713aSLionel Sambuc 3*f4a2713aSLionel Sambuc namespace StaticAssertFoldTest { 4*f4a2713aSLionel Sambuc 5*f4a2713aSLionel Sambuc int x; 6*f4a2713aSLionel Sambuc static_assert(++x, "test"); // expected-error {{not an integral constant expression}} 7*f4a2713aSLionel Sambuc static_assert(false, "test"); // expected-error {{test}} 8*f4a2713aSLionel Sambuc 9*f4a2713aSLionel Sambuc } 10*f4a2713aSLionel Sambuc 11*f4a2713aSLionel Sambuc typedef decltype(sizeof(char)) size_t; 12*f4a2713aSLionel Sambuc 13*f4a2713aSLionel Sambuc template<typename T> constexpr T id(const T &t) { return t; } 14*f4a2713aSLionel Sambuc template<typename T> constexpr T min(const T &a, const T &b) { 15*f4a2713aSLionel Sambuc return a < b ? a : b; 16*f4a2713aSLionel Sambuc } 17*f4a2713aSLionel Sambuc template<typename T> constexpr T max(const T &a, const T &b) { 18*f4a2713aSLionel Sambuc return a < b ? b : a; 19*f4a2713aSLionel Sambuc } 20*f4a2713aSLionel Sambuc template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; } 21*f4a2713aSLionel Sambuc template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; } 22*f4a2713aSLionel Sambuc 23*f4a2713aSLionel Sambuc struct MemberZero { 24*f4a2713aSLionel Sambuc constexpr int zero() const { return 0; } 25*f4a2713aSLionel Sambuc }; 26*f4a2713aSLionel Sambuc 27*f4a2713aSLionel Sambuc namespace DerivedToVBaseCast { 28*f4a2713aSLionel Sambuc 29*f4a2713aSLionel Sambuc struct U { int n; }; 30*f4a2713aSLionel Sambuc struct V : U { int n; }; 31*f4a2713aSLionel Sambuc struct A : virtual V { int n; }; 32*f4a2713aSLionel Sambuc struct Aa { int n; }; 33*f4a2713aSLionel Sambuc struct B : virtual A, Aa {}; 34*f4a2713aSLionel Sambuc struct C : virtual A, Aa {}; 35*f4a2713aSLionel Sambuc struct D : B, C {}; 36*f4a2713aSLionel Sambuc 37*f4a2713aSLionel Sambuc D d; 38*f4a2713aSLionel Sambuc constexpr B *p = &d; 39*f4a2713aSLionel Sambuc constexpr C *q = &d; 40*f4a2713aSLionel Sambuc 41*f4a2713aSLionel Sambuc static_assert((void*)p != (void*)q, ""); 42*f4a2713aSLionel Sambuc static_assert((A*)p == (A*)q, ""); 43*f4a2713aSLionel Sambuc static_assert((Aa*)p != (Aa*)q, ""); 44*f4a2713aSLionel Sambuc 45*f4a2713aSLionel Sambuc constexpr B &pp = d; 46*f4a2713aSLionel Sambuc constexpr C &qq = d; 47*f4a2713aSLionel Sambuc static_assert((void*)&pp != (void*)&qq, ""); 48*f4a2713aSLionel Sambuc static_assert(&(A&)pp == &(A&)qq, ""); 49*f4a2713aSLionel Sambuc static_assert(&(Aa&)pp != &(Aa&)qq, ""); 50*f4a2713aSLionel Sambuc 51*f4a2713aSLionel Sambuc constexpr V *v = p; 52*f4a2713aSLionel Sambuc constexpr V *w = q; 53*f4a2713aSLionel Sambuc constexpr V *x = (A*)p; 54*f4a2713aSLionel Sambuc static_assert(v == w, ""); 55*f4a2713aSLionel Sambuc static_assert(v == x, ""); 56*f4a2713aSLionel Sambuc 57*f4a2713aSLionel Sambuc static_assert((U*)&d == p, ""); 58*f4a2713aSLionel Sambuc static_assert((U*)&d == q, ""); 59*f4a2713aSLionel Sambuc static_assert((U*)&d == v, ""); 60*f4a2713aSLionel Sambuc static_assert((U*)&d == w, ""); 61*f4a2713aSLionel Sambuc static_assert((U*)&d == x, ""); 62*f4a2713aSLionel Sambuc 63*f4a2713aSLionel Sambuc struct X {}; 64*f4a2713aSLionel Sambuc struct Y1 : virtual X {}; 65*f4a2713aSLionel Sambuc struct Y2 : X {}; 66*f4a2713aSLionel Sambuc struct Z : Y1, Y2 {}; 67*f4a2713aSLionel Sambuc Z z; 68*f4a2713aSLionel Sambuc static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, ""); 69*f4a2713aSLionel Sambuc } 70*f4a2713aSLionel Sambuc 71*f4a2713aSLionel Sambuc namespace ConstCast { 72*f4a2713aSLionel Sambuc 73*f4a2713aSLionel Sambuc constexpr int n1 = 0; 74*f4a2713aSLionel Sambuc constexpr int n2 = const_cast<int&>(n1); 75*f4a2713aSLionel Sambuc constexpr int *n3 = const_cast<int*>(&n1); 76*f4a2713aSLionel Sambuc constexpr int n4 = *const_cast<int*>(&n1); 77*f4a2713aSLionel Sambuc constexpr const int * const *n5 = const_cast<const int* const*>(&n3); 78*f4a2713aSLionel Sambuc constexpr int **n6 = const_cast<int**>(&n3); 79*f4a2713aSLionel Sambuc constexpr int n7 = **n5; 80*f4a2713aSLionel Sambuc constexpr int n8 = **n6; 81*f4a2713aSLionel Sambuc 82*f4a2713aSLionel Sambuc // const_cast from prvalue to xvalue. 83*f4a2713aSLionel Sambuc struct A { int n; }; 84*f4a2713aSLionel Sambuc constexpr int n9 = (const_cast<A&&>(A{123})).n; 85*f4a2713aSLionel Sambuc static_assert(n9 == 123, ""); 86*f4a2713aSLionel Sambuc 87*f4a2713aSLionel Sambuc } 88*f4a2713aSLionel Sambuc 89*f4a2713aSLionel Sambuc namespace TemplateArgumentConversion { 90*f4a2713aSLionel Sambuc template<int n> struct IntParam {}; 91*f4a2713aSLionel Sambuc 92*f4a2713aSLionel Sambuc using IntParam0 = IntParam<0>; 93*f4a2713aSLionel Sambuc using IntParam0 = IntParam<id(0)>; 94*f4a2713aSLionel Sambuc using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} 95*f4a2713aSLionel Sambuc } 96*f4a2713aSLionel Sambuc 97*f4a2713aSLionel Sambuc namespace CaseStatements { 98*f4a2713aSLionel Sambuc void f(int n) { 99*f4a2713aSLionel Sambuc switch (n) { 100*f4a2713aSLionel Sambuc case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}} 101*f4a2713aSLionel Sambuc case id(0): // expected-error {{duplicate case value '0'}} 102*f4a2713aSLionel Sambuc return; 103*f4a2713aSLionel Sambuc } 104*f4a2713aSLionel Sambuc } 105*f4a2713aSLionel Sambuc } 106*f4a2713aSLionel Sambuc 107*f4a2713aSLionel Sambuc extern int &Recurse1; 108*f4a2713aSLionel Sambuc int &Recurse2 = Recurse1; // expected-note {{declared here}} 109*f4a2713aSLionel Sambuc int &Recurse1 = Recurse2; 110*f4a2713aSLionel Sambuc constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}} 111*f4a2713aSLionel Sambuc 112*f4a2713aSLionel Sambuc extern const int RecurseA; 113*f4a2713aSLionel Sambuc const int RecurseB = RecurseA; // expected-note {{declared here}} 114*f4a2713aSLionel Sambuc const int RecurseA = 10; 115*f4a2713aSLionel Sambuc constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}} 116*f4a2713aSLionel Sambuc 117*f4a2713aSLionel Sambuc namespace MemberEnum { 118*f4a2713aSLionel Sambuc struct WithMemberEnum { 119*f4a2713aSLionel Sambuc enum E { A = 42 }; 120*f4a2713aSLionel Sambuc } wme; 121*f4a2713aSLionel Sambuc 122*f4a2713aSLionel Sambuc static_assert(wme.A == 42, ""); 123*f4a2713aSLionel Sambuc } 124*f4a2713aSLionel Sambuc 125*f4a2713aSLionel Sambuc namespace DefaultArguments { 126*f4a2713aSLionel Sambuc 127*f4a2713aSLionel Sambuc const int z = int(); 128*f4a2713aSLionel Sambuc constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) { 129*f4a2713aSLionel Sambuc return a + b + *c + d; 130*f4a2713aSLionel Sambuc } 131*f4a2713aSLionel Sambuc const int four = 4; 132*f4a2713aSLionel Sambuc constexpr int eight = 8; 133*f4a2713aSLionel Sambuc constexpr const int twentyseven = 27; 134*f4a2713aSLionel Sambuc static_assert(Sum() == 0, ""); 135*f4a2713aSLionel Sambuc static_assert(Sum(1) == 1, ""); 136*f4a2713aSLionel Sambuc static_assert(Sum(1, four) == 5, ""); 137*f4a2713aSLionel Sambuc static_assert(Sum(1, eight, &twentyseven) == 36, ""); 138*f4a2713aSLionel Sambuc static_assert(Sum(1, 2, &four, eight) == 15, ""); 139*f4a2713aSLionel Sambuc 140*f4a2713aSLionel Sambuc } 141*f4a2713aSLionel Sambuc 142*f4a2713aSLionel Sambuc namespace Ellipsis { 143*f4a2713aSLionel Sambuc 144*f4a2713aSLionel Sambuc // Note, values passed through an ellipsis can't actually be used. 145*f4a2713aSLionel Sambuc constexpr int F(int a, ...) { return a; } 146*f4a2713aSLionel Sambuc static_assert(F(0) == 0, ""); 147*f4a2713aSLionel Sambuc static_assert(F(1, 0) == 1, ""); 148*f4a2713aSLionel Sambuc static_assert(F(2, "test") == 2, ""); 149*f4a2713aSLionel Sambuc static_assert(F(3, &F) == 3, ""); 150*f4a2713aSLionel Sambuc int k = 0; // expected-note {{here}} 151*f4a2713aSLionel Sambuc static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}} 152*f4a2713aSLionel Sambuc 153*f4a2713aSLionel Sambuc } 154*f4a2713aSLionel Sambuc 155*f4a2713aSLionel Sambuc namespace Recursion { 156*f4a2713aSLionel Sambuc constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; } 157*f4a2713aSLionel Sambuc static_assert(fib(11) == 89, ""); 158*f4a2713aSLionel Sambuc 159*f4a2713aSLionel Sambuc constexpr int gcd_inner(int a, int b) { 160*f4a2713aSLionel Sambuc return b == 0 ? a : gcd_inner(b, a % b); 161*f4a2713aSLionel Sambuc } 162*f4a2713aSLionel Sambuc constexpr int gcd(int a, int b) { 163*f4a2713aSLionel Sambuc return gcd_inner(max(a, b), min(a, b)); 164*f4a2713aSLionel Sambuc } 165*f4a2713aSLionel Sambuc 166*f4a2713aSLionel Sambuc static_assert(gcd(1749237, 5628959) == 7, ""); 167*f4a2713aSLionel Sambuc } 168*f4a2713aSLionel Sambuc 169*f4a2713aSLionel Sambuc namespace FunctionCast { 170*f4a2713aSLionel Sambuc // When folding, we allow functions to be cast to different types. Such 171*f4a2713aSLionel Sambuc // cast functions cannot be called, even if they're constexpr. 172*f4a2713aSLionel Sambuc constexpr int f() { return 1; } 173*f4a2713aSLionel Sambuc typedef double (*DoubleFn)(); 174*f4a2713aSLionel Sambuc typedef int (*IntFn)(); 175*f4a2713aSLionel Sambuc int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}} 176*f4a2713aSLionel Sambuc int b[(int)IntFn(f)()]; // ok 177*f4a2713aSLionel Sambuc } 178*f4a2713aSLionel Sambuc 179*f4a2713aSLionel Sambuc namespace StaticMemberFunction { 180*f4a2713aSLionel Sambuc struct S { 181*f4a2713aSLionel Sambuc static constexpr int k = 42; 182*f4a2713aSLionel Sambuc static constexpr int f(int n) { return n * k + 2; } 183*f4a2713aSLionel Sambuc } s; 184*f4a2713aSLionel Sambuc 185*f4a2713aSLionel Sambuc constexpr int n = s.f(19); 186*f4a2713aSLionel Sambuc static_assert(S::f(19) == 800, ""); 187*f4a2713aSLionel Sambuc static_assert(s.f(19) == 800, ""); 188*f4a2713aSLionel Sambuc static_assert(n == 800, ""); 189*f4a2713aSLionel Sambuc 190*f4a2713aSLionel Sambuc constexpr int (*sf1)(int) = &S::f; 191*f4a2713aSLionel Sambuc constexpr int (*sf2)(int) = &s.f; 192*f4a2713aSLionel Sambuc constexpr const int *sk = &s.k; 193*f4a2713aSLionel Sambuc } 194*f4a2713aSLionel Sambuc 195*f4a2713aSLionel Sambuc namespace ParameterScopes { 196*f4a2713aSLionel Sambuc 197*f4a2713aSLionel Sambuc const int k = 42; 198*f4a2713aSLionel Sambuc constexpr const int &ObscureTheTruth(const int &a) { return a; } 199*f4a2713aSLionel Sambuc constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}} 200*f4a2713aSLionel Sambuc return ObscureTheTruth(b ? a : k); 201*f4a2713aSLionel Sambuc } 202*f4a2713aSLionel Sambuc static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok 203*f4a2713aSLionel Sambuc constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}} 204*f4a2713aSLionel Sambuc 205*f4a2713aSLionel Sambuc constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { 206*f4a2713aSLionel Sambuc return ObscureTheTruth(b ? a : k); 207*f4a2713aSLionel Sambuc } 208*f4a2713aSLionel Sambuc static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok 209*f4a2713aSLionel Sambuc constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok 210*f4a2713aSLionel Sambuc 211*f4a2713aSLionel Sambuc constexpr int InternalReturnJunk(int n) { 212*f4a2713aSLionel Sambuc return MaybeReturnJunk(true, n); // expected-note {{read of variable whose lifetime has ended}} 213*f4a2713aSLionel Sambuc } 214*f4a2713aSLionel Sambuc constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}} 215*f4a2713aSLionel Sambuc 216*f4a2713aSLionel Sambuc constexpr int LToR(int &n) { return n; } 217*f4a2713aSLionel Sambuc constexpr int GrabCallersArgument(bool which, int a, int b) { 218*f4a2713aSLionel Sambuc return LToR(which ? b : a); 219*f4a2713aSLionel Sambuc } 220*f4a2713aSLionel Sambuc static_assert(GrabCallersArgument(false, 1, 2) == 1, ""); 221*f4a2713aSLionel Sambuc static_assert(GrabCallersArgument(true, 4, 8) == 8, ""); 222*f4a2713aSLionel Sambuc 223*f4a2713aSLionel Sambuc } 224*f4a2713aSLionel Sambuc 225*f4a2713aSLionel Sambuc namespace Pointers { 226*f4a2713aSLionel Sambuc 227*f4a2713aSLionel Sambuc constexpr int f(int n, const int *a, const int *b, const int *c) { 228*f4a2713aSLionel Sambuc return n == 0 ? 0 : *a + f(n-1, b, c, a); 229*f4a2713aSLionel Sambuc } 230*f4a2713aSLionel Sambuc 231*f4a2713aSLionel Sambuc const int x = 1, y = 10, z = 100; 232*f4a2713aSLionel Sambuc static_assert(f(23, &x, &y, &z) == 788, ""); 233*f4a2713aSLionel Sambuc 234*f4a2713aSLionel Sambuc constexpr int g(int n, int a, int b, int c) { 235*f4a2713aSLionel Sambuc return f(n, &a, &b, &c); 236*f4a2713aSLionel Sambuc } 237*f4a2713aSLionel Sambuc static_assert(g(23, x, y, z) == 788, ""); 238*f4a2713aSLionel Sambuc 239*f4a2713aSLionel Sambuc } 240*f4a2713aSLionel Sambuc 241*f4a2713aSLionel Sambuc namespace FunctionPointers { 242*f4a2713aSLionel Sambuc 243*f4a2713aSLionel Sambuc constexpr int Double(int n) { return 2 * n; } 244*f4a2713aSLionel Sambuc constexpr int Triple(int n) { return 3 * n; } 245*f4a2713aSLionel Sambuc constexpr int Twice(int (*F)(int), int n) { return F(F(n)); } 246*f4a2713aSLionel Sambuc constexpr int Quadruple(int n) { return Twice(Double, n); } 247*f4a2713aSLionel Sambuc constexpr auto Select(int n) -> int (*)(int) { 248*f4a2713aSLionel Sambuc return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0; 249*f4a2713aSLionel Sambuc } 250*f4a2713aSLionel Sambuc constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}} 251*f4a2713aSLionel Sambuc 252*f4a2713aSLionel Sambuc static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); 253*f4a2713aSLionel Sambuc 254*f4a2713aSLionel Sambuc 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*f4a2713aSLionel Sambuc 256*f4a2713aSLionel Sambuc } 257*f4a2713aSLionel Sambuc 258*f4a2713aSLionel Sambuc namespace PointerComparison { 259*f4a2713aSLionel Sambuc 260*f4a2713aSLionel Sambuc int x, y; 261*f4a2713aSLionel Sambuc static_assert(&x == &y, "false"); // expected-error {{false}} 262*f4a2713aSLionel Sambuc static_assert(&x != &y, ""); 263*f4a2713aSLionel Sambuc constexpr bool g1 = &x == &y; 264*f4a2713aSLionel Sambuc constexpr bool g2 = &x != &y; 265*f4a2713aSLionel Sambuc constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} 266*f4a2713aSLionel Sambuc constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} 267*f4a2713aSLionel Sambuc constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} 268*f4a2713aSLionel Sambuc constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} 269*f4a2713aSLionel Sambuc 270*f4a2713aSLionel Sambuc struct S { int x, y; } s; 271*f4a2713aSLionel Sambuc static_assert(&s.x == &s.y, "false"); // expected-error {{false}} 272*f4a2713aSLionel Sambuc static_assert(&s.x != &s.y, ""); 273*f4a2713aSLionel Sambuc static_assert(&s.x <= &s.y, ""); 274*f4a2713aSLionel Sambuc static_assert(&s.x >= &s.y, "false"); // expected-error {{false}} 275*f4a2713aSLionel Sambuc static_assert(&s.x < &s.y, ""); 276*f4a2713aSLionel Sambuc static_assert(&s.x > &s.y, "false"); // expected-error {{false}} 277*f4a2713aSLionel Sambuc 278*f4a2713aSLionel Sambuc static_assert(0 == &y, "false"); // expected-error {{false}} 279*f4a2713aSLionel Sambuc static_assert(0 != &y, ""); 280*f4a2713aSLionel Sambuc constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}} 281*f4a2713aSLionel Sambuc constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}} 282*f4a2713aSLionel Sambuc constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}} 283*f4a2713aSLionel Sambuc constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}} 284*f4a2713aSLionel Sambuc 285*f4a2713aSLionel Sambuc static_assert(&x == 0, "false"); // expected-error {{false}} 286*f4a2713aSLionel Sambuc static_assert(&x != 0, ""); 287*f4a2713aSLionel Sambuc constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}} 288*f4a2713aSLionel Sambuc constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}} 289*f4a2713aSLionel Sambuc constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}} 290*f4a2713aSLionel Sambuc constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}} 291*f4a2713aSLionel Sambuc 292*f4a2713aSLionel Sambuc static_assert(&x == &x, ""); 293*f4a2713aSLionel Sambuc static_assert(&x != &x, "false"); // expected-error {{false}} 294*f4a2713aSLionel Sambuc static_assert(&x <= &x, ""); 295*f4a2713aSLionel Sambuc static_assert(&x >= &x, ""); 296*f4a2713aSLionel Sambuc static_assert(&x < &x, "false"); // expected-error {{false}} 297*f4a2713aSLionel Sambuc static_assert(&x > &x, "false"); // expected-error {{false}} 298*f4a2713aSLionel Sambuc 299*f4a2713aSLionel Sambuc constexpr S* sptr = &s; 300*f4a2713aSLionel Sambuc constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}} 301*f4a2713aSLionel Sambuc 302*f4a2713aSLionel Sambuc struct U {}; 303*f4a2713aSLionel Sambuc struct Str { 304*f4a2713aSLionel Sambuc int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \ 305*f4a2713aSLionel Sambuc expected-warning {{not an integral constant expression}} \ 306*f4a2713aSLionel Sambuc expected-note {{dynamic_cast is not allowed in a constant expression}} 307*f4a2713aSLionel Sambuc int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \ 308*f4a2713aSLionel Sambuc expected-warning {{not an integral constant expression}} \ 309*f4a2713aSLionel Sambuc expected-note {{reinterpret_cast is not allowed in a constant expression}} 310*f4a2713aSLionel Sambuc int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \ 311*f4a2713aSLionel Sambuc expected-warning {{not an integral constant expression}} \ 312*f4a2713aSLionel Sambuc expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 313*f4a2713aSLionel Sambuc int d : (S*)(42) == (S*)(42); // \ 314*f4a2713aSLionel Sambuc expected-warning {{not an integral constant expression}} \ 315*f4a2713aSLionel Sambuc expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 316*f4a2713aSLionel Sambuc int e : (Str*)(sptr) == (Str*)(sptr); // \ 317*f4a2713aSLionel Sambuc expected-warning {{not an integral constant expression}} \ 318*f4a2713aSLionel Sambuc expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 319*f4a2713aSLionel Sambuc int f : &(U&)(*sptr) == &(U&)(*sptr); // \ 320*f4a2713aSLionel Sambuc expected-warning {{not an integral constant expression}} \ 321*f4a2713aSLionel Sambuc expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 322*f4a2713aSLionel Sambuc int g : (S*)(void*)(sptr) == sptr; // \ 323*f4a2713aSLionel Sambuc expected-warning {{not an integral constant expression}} \ 324*f4a2713aSLionel Sambuc expected-note {{cast from 'void *' is not allowed in a constant expression}} 325*f4a2713aSLionel Sambuc }; 326*f4a2713aSLionel Sambuc 327*f4a2713aSLionel Sambuc extern char externalvar[]; 328*f4a2713aSLionel Sambuc constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} 329*f4a2713aSLionel Sambuc constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}} 330*f4a2713aSLionel Sambuc static_assert(0 != "foo", ""); 331*f4a2713aSLionel Sambuc 332*f4a2713aSLionel Sambuc } 333*f4a2713aSLionel Sambuc 334*f4a2713aSLionel Sambuc namespace MaterializeTemporary { 335*f4a2713aSLionel Sambuc 336*f4a2713aSLionel Sambuc constexpr int f(const int &r) { return r; } 337*f4a2713aSLionel Sambuc constexpr int n = f(1); 338*f4a2713aSLionel Sambuc 339*f4a2713aSLionel Sambuc constexpr bool same(const int &a, const int &b) { return &a == &b; } 340*f4a2713aSLionel Sambuc constexpr bool sameTemporary(const int &n) { return same(n, n); } 341*f4a2713aSLionel Sambuc 342*f4a2713aSLionel Sambuc static_assert(n, ""); 343*f4a2713aSLionel Sambuc static_assert(!same(4, 4), ""); 344*f4a2713aSLionel Sambuc static_assert(same(n, n), ""); 345*f4a2713aSLionel Sambuc static_assert(sameTemporary(9), ""); 346*f4a2713aSLionel Sambuc 347*f4a2713aSLionel Sambuc struct A { int &&r; }; 348*f4a2713aSLionel Sambuc struct B { A &&a1; A &&a2; }; 349*f4a2713aSLionel Sambuc 350*f4a2713aSLionel Sambuc constexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}} 351*f4a2713aSLionel Sambuc static_assert(&b1.a1 != &b1.a2, ""); 352*f4a2713aSLionel Sambuc static_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} 353*f4a2713aSLionel Sambuc 354*f4a2713aSLionel Sambuc constexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}} 355*f4a2713aSLionel Sambuc static_assert(&b1 != &b2, ""); 356*f4a2713aSLionel Sambuc static_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} 357*f4a2713aSLionel Sambuc 358*f4a2713aSLionel Sambuc constexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 359*f4a2713aSLionel Sambuc void foo() { 360*f4a2713aSLionel Sambuc constexpr static B b1 { { 1 }, { 2 } }; // ok 361*f4a2713aSLionel Sambuc constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 362*f4a2713aSLionel Sambuc constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 363*f4a2713aSLionel Sambuc } 364*f4a2713aSLionel Sambuc 365*f4a2713aSLionel Sambuc constexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} }); // expected-warning 4{{unused}} 366*f4a2713aSLionel Sambuc static_assert(&b4 != &b2, ""); 367*f4a2713aSLionel Sambuc 368*f4a2713aSLionel Sambuc // Proposed DR: copy-elision doesn't trigger lifetime extension. 369*f4a2713aSLionel Sambuc constexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 370*f4a2713aSLionel Sambuc 371*f4a2713aSLionel Sambuc namespace NestedNonStatic { 372*f4a2713aSLionel Sambuc // Proposed DR: for a reference constant expression to refer to a static 373*f4a2713aSLionel Sambuc // storage duration temporary, that temporary must itself be initialized 374*f4a2713aSLionel Sambuc // by a constant expression (a core constant expression is not enough). 375*f4a2713aSLionel Sambuc struct A { int &&r; }; 376*f4a2713aSLionel Sambuc struct B { A &&a; }; 377*f4a2713aSLionel Sambuc constexpr B a = { A{0} }; // ok 378*f4a2713aSLionel Sambuc constexpr B b = { A(A{0}) }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 379*f4a2713aSLionel Sambuc } 380*f4a2713aSLionel Sambuc 381*f4a2713aSLionel Sambuc namespace FakeInitList { 382*f4a2713aSLionel Sambuc struct init_list_3_ints { const int (&x)[3]; }; 383*f4a2713aSLionel Sambuc struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; }; 384*f4a2713aSLionel Sambuc constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } }; 385*f4a2713aSLionel Sambuc } 386*f4a2713aSLionel Sambuc 387*f4a2713aSLionel Sambuc } 388*f4a2713aSLionel Sambuc 389*f4a2713aSLionel Sambuc constexpr int strcmp_ce(const char *p, const char *q) { 390*f4a2713aSLionel Sambuc return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1); 391*f4a2713aSLionel Sambuc } 392*f4a2713aSLionel Sambuc 393*f4a2713aSLionel Sambuc namespace StringLiteral { 394*f4a2713aSLionel Sambuc 395*f4a2713aSLionel Sambuc template<typename Char> 396*f4a2713aSLionel Sambuc constexpr int MangleChars(const Char *p) { 397*f4a2713aSLionel Sambuc return *p + 3 * (*p ? MangleChars(p+1) : 0); 398*f4a2713aSLionel Sambuc } 399*f4a2713aSLionel Sambuc 400*f4a2713aSLionel Sambuc static_assert(MangleChars("constexpr!") == 1768383, ""); 401*f4a2713aSLionel Sambuc static_assert(MangleChars(u8"constexpr!") == 1768383, ""); 402*f4a2713aSLionel Sambuc static_assert(MangleChars(L"constexpr!") == 1768383, ""); 403*f4a2713aSLionel Sambuc static_assert(MangleChars(u"constexpr!") == 1768383, ""); 404*f4a2713aSLionel Sambuc static_assert(MangleChars(U"constexpr!") == 1768383, ""); 405*f4a2713aSLionel Sambuc 406*f4a2713aSLionel Sambuc constexpr char c0 = "nought index"[0]; 407*f4a2713aSLionel Sambuc constexpr char c1 = "nice index"[10]; 408*f4a2713aSLionel Sambuc constexpr 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}} 409*f4a2713aSLionel Sambuc constexpr 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}} 410*f4a2713aSLionel Sambuc constexpr 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}} 411*f4a2713aSLionel Sambuc 412*f4a2713aSLionel Sambuc constexpr const char *p = "test" + 2; 413*f4a2713aSLionel Sambuc static_assert(*p == 's', ""); 414*f4a2713aSLionel Sambuc 415*f4a2713aSLionel Sambuc constexpr const char *max_iter(const char *a, const char *b) { 416*f4a2713aSLionel Sambuc return *a < *b ? b : a; 417*f4a2713aSLionel Sambuc } 418*f4a2713aSLionel Sambuc constexpr const char *max_element(const char *a, const char *b) { 419*f4a2713aSLionel Sambuc return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b)); 420*f4a2713aSLionel Sambuc } 421*f4a2713aSLionel Sambuc 422*f4a2713aSLionel Sambuc constexpr char str[] = "the quick brown fox jumped over the lazy dog"; 423*f4a2713aSLionel Sambuc constexpr const char *max = max_element(begin(str), end(str)); 424*f4a2713aSLionel Sambuc static_assert(*max == 'z', ""); 425*f4a2713aSLionel Sambuc static_assert(max == str + 38, ""); 426*f4a2713aSLionel Sambuc 427*f4a2713aSLionel Sambuc static_assert(strcmp_ce("hello world", "hello world") == 0, ""); 428*f4a2713aSLionel Sambuc static_assert(strcmp_ce("hello world", "hello clang") > 0, ""); 429*f4a2713aSLionel Sambuc static_assert(strcmp_ce("constexpr", "test") < 0, ""); 430*f4a2713aSLionel Sambuc static_assert(strcmp_ce("", " ") < 0, ""); 431*f4a2713aSLionel Sambuc 432*f4a2713aSLionel Sambuc struct S { 433*f4a2713aSLionel Sambuc 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}} 434*f4a2713aSLionel Sambuc }; 435*f4a2713aSLionel Sambuc 436*f4a2713aSLionel Sambuc struct T { 437*f4a2713aSLionel Sambuc char c[6]; 438*f4a2713aSLionel Sambuc constexpr T() : c{"foo"} {} 439*f4a2713aSLionel Sambuc }; 440*f4a2713aSLionel Sambuc constexpr T t; 441*f4a2713aSLionel Sambuc 442*f4a2713aSLionel Sambuc static_assert(t.c[0] == 'f', ""); 443*f4a2713aSLionel Sambuc static_assert(t.c[1] == 'o', ""); 444*f4a2713aSLionel Sambuc static_assert(t.c[2] == 'o', ""); 445*f4a2713aSLionel Sambuc static_assert(t.c[3] == 0, ""); 446*f4a2713aSLionel Sambuc static_assert(t.c[4] == 0, ""); 447*f4a2713aSLionel Sambuc static_assert(t.c[5] == 0, ""); 448*f4a2713aSLionel Sambuc static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 449*f4a2713aSLionel Sambuc 450*f4a2713aSLionel Sambuc struct U { 451*f4a2713aSLionel Sambuc wchar_t chars[6]; 452*f4a2713aSLionel Sambuc int n; 453*f4a2713aSLionel Sambuc } constexpr u = { { L"test" }, 0 }; 454*f4a2713aSLionel Sambuc static_assert(u.chars[2] == L's', ""); 455*f4a2713aSLionel Sambuc 456*f4a2713aSLionel Sambuc struct V { 457*f4a2713aSLionel Sambuc char c[4]; 458*f4a2713aSLionel Sambuc constexpr V() : c("hi!") {} 459*f4a2713aSLionel Sambuc }; 460*f4a2713aSLionel Sambuc static_assert(V().c[1] == "i"[0], ""); 461*f4a2713aSLionel Sambuc 462*f4a2713aSLionel Sambuc namespace Parens { 463*f4a2713aSLionel Sambuc constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")}, 464*f4a2713aSLionel Sambuc d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")}; 465*f4a2713aSLionel Sambuc static_assert(a[0] == 'f', ""); 466*f4a2713aSLionel Sambuc static_assert(b[1] == 'o', ""); 467*f4a2713aSLionel Sambuc static_assert(c[2] == 'o', ""); 468*f4a2713aSLionel Sambuc static_assert(d[0] == 'f', ""); 469*f4a2713aSLionel Sambuc static_assert(e[1] == 'o', ""); 470*f4a2713aSLionel Sambuc static_assert(f[2] == 'o', ""); 471*f4a2713aSLionel Sambuc static_assert(f[5] == 0, ""); 472*f4a2713aSLionel Sambuc static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 473*f4a2713aSLionel Sambuc } 474*f4a2713aSLionel Sambuc 475*f4a2713aSLionel Sambuc } 476*f4a2713aSLionel Sambuc 477*f4a2713aSLionel Sambuc namespace Array { 478*f4a2713aSLionel Sambuc 479*f4a2713aSLionel Sambuc template<typename Iter> 480*f4a2713aSLionel Sambuc constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) { 481*f4a2713aSLionel Sambuc return begin == end ? 0 : *begin + Sum(begin+1, end); 482*f4a2713aSLionel Sambuc } 483*f4a2713aSLionel Sambuc 484*f4a2713aSLionel Sambuc constexpr int xs[] = { 1, 2, 3, 4, 5 }; 485*f4a2713aSLionel Sambuc constexpr int ys[] = { 5, 4, 3, 2, 1 }; 486*f4a2713aSLionel Sambuc constexpr int sum_xs = Sum(begin(xs), end(xs)); 487*f4a2713aSLionel Sambuc static_assert(sum_xs == 15, ""); 488*f4a2713aSLionel Sambuc 489*f4a2713aSLionel Sambuc constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, 490*f4a2713aSLionel Sambuc const int *xs, const int *ys, int c) { 491*f4a2713aSLionel Sambuc return n ? F( 492*f4a2713aSLionel Sambuc *xs, // expected-note {{read of dereferenced one-past-the-end pointer}} 493*f4a2713aSLionel Sambuc *ys, 494*f4a2713aSLionel Sambuc ZipFoldR(F, n-1, xs+1, ys+1, c)) // \ 495*f4a2713aSLionel Sambuc expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \ 496*f4a2713aSLionel Sambuc expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}} 497*f4a2713aSLionel Sambuc : c; 498*f4a2713aSLionel Sambuc } 499*f4a2713aSLionel Sambuc constexpr int MulAdd(int x, int y, int c) { return x * y + c; } 500*f4a2713aSLionel Sambuc constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); 501*f4a2713aSLionel Sambuc static_assert(InnerProduct == 35, ""); 502*f4a2713aSLionel Sambuc 503*f4a2713aSLionel Sambuc constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } 504*f4a2713aSLionel Sambuc constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); 505*f4a2713aSLionel Sambuc static_assert(DiffProd == 8, ""); 506*f4a2713aSLionel Sambuc static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \ 507*f4a2713aSLionel Sambuc expected-error {{constant expression}} \ 508*f4a2713aSLionel Sambuc expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}} 509*f4a2713aSLionel Sambuc 510*f4a2713aSLionel Sambuc constexpr const int *p = xs + 3; 511*f4a2713aSLionel Sambuc constexpr int xs4 = p[1]; // ok 512*f4a2713aSLionel Sambuc constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 513*f4a2713aSLionel Sambuc constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}} 514*f4a2713aSLionel Sambuc constexpr int xs0 = p[-3]; // ok 515*f4a2713aSLionel Sambuc constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 516*f4a2713aSLionel Sambuc 517*f4a2713aSLionel Sambuc constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 518*f4a2713aSLionel Sambuc static_assert(zs[0][0][0][0] == 1, ""); 519*f4a2713aSLionel Sambuc static_assert(zs[1][1][1][1] == 16, ""); 520*f4a2713aSLionel Sambuc static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 521*f4a2713aSLionel Sambuc static_assert((&zs[0][0][0][2])[-1] == 2, ""); 522*f4a2713aSLionel Sambuc static_assert(**(**(zs + 1) + 1) == 11, ""); 523*f4a2713aSLionel Sambuc static_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}} 524*f4a2713aSLionel Sambuc static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, ""); 525*f4a2713aSLionel Sambuc constexpr 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}} 526*f4a2713aSLionel Sambuc 527*f4a2713aSLionel Sambuc constexpr int fail(const int &p) { 528*f4a2713aSLionel Sambuc return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}} 529*f4a2713aSLionel Sambuc } 530*f4a2713aSLionel Sambuc static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \ 531*f4a2713aSLionel Sambuc expected-error {{static_assert expression is not an integral constant expression}} \ 532*f4a2713aSLionel Sambuc expected-note {{in call to 'fail(zs[1][0][1][0])'}} 533*f4a2713aSLionel Sambuc 534*f4a2713aSLionel Sambuc constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}} 535*f4a2713aSLionel Sambuc constexpr int SumNonzero(const int *p) { 536*f4a2713aSLionel Sambuc return *p + (*p ? SumNonzero(p+1) : 0); 537*f4a2713aSLionel Sambuc } 538*f4a2713aSLionel Sambuc constexpr int CountZero(const int *p, const int *q) { 539*f4a2713aSLionel Sambuc return p == q ? 0 : (*p == 0) + CountZero(p+1, q); 540*f4a2713aSLionel Sambuc } 541*f4a2713aSLionel Sambuc static_assert(SumNonzero(arr) == 6, ""); 542*f4a2713aSLionel Sambuc static_assert(CountZero(arr, arr + 40) == 36, ""); 543*f4a2713aSLionel Sambuc 544*f4a2713aSLionel Sambuc struct ArrayElem { 545*f4a2713aSLionel Sambuc constexpr ArrayElem() : n(0) {} 546*f4a2713aSLionel Sambuc int n; 547*f4a2713aSLionel Sambuc constexpr int f() const { return n; } 548*f4a2713aSLionel Sambuc }; 549*f4a2713aSLionel Sambuc struct ArrayRVal { 550*f4a2713aSLionel Sambuc constexpr ArrayRVal() {} 551*f4a2713aSLionel Sambuc ArrayElem elems[10]; 552*f4a2713aSLionel Sambuc }; 553*f4a2713aSLionel Sambuc static_assert(ArrayRVal().elems[3].f() == 0, ""); 554*f4a2713aSLionel Sambuc 555*f4a2713aSLionel Sambuc constexpr int selfref[2][2][2] = { 556*f4a2713aSLionel Sambuc selfref[1][1][1] + 1, selfref[0][0][0] + 1, 557*f4a2713aSLionel Sambuc selfref[1][0][1] + 1, selfref[0][1][0] + 1, 558*f4a2713aSLionel Sambuc selfref[1][0][0] + 1, selfref[0][1][1] + 1 }; 559*f4a2713aSLionel Sambuc static_assert(selfref[0][0][0] == 1, ""); 560*f4a2713aSLionel Sambuc static_assert(selfref[0][0][1] == 2, ""); 561*f4a2713aSLionel Sambuc static_assert(selfref[0][1][0] == 1, ""); 562*f4a2713aSLionel Sambuc static_assert(selfref[0][1][1] == 2, ""); 563*f4a2713aSLionel Sambuc static_assert(selfref[1][0][0] == 1, ""); 564*f4a2713aSLionel Sambuc static_assert(selfref[1][0][1] == 3, ""); 565*f4a2713aSLionel Sambuc static_assert(selfref[1][1][0] == 0, ""); 566*f4a2713aSLionel Sambuc static_assert(selfref[1][1][1] == 0, ""); 567*f4a2713aSLionel Sambuc 568*f4a2713aSLionel Sambuc struct TrivialDefCtor { int n; }; 569*f4a2713aSLionel Sambuc typedef TrivialDefCtor TDCArray[2][2]; 570*f4a2713aSLionel Sambuc static_assert(TDCArray{}[1][1].n == 0, ""); 571*f4a2713aSLionel Sambuc 572*f4a2713aSLionel Sambuc struct NonAggregateTDC : TrivialDefCtor {}; 573*f4a2713aSLionel Sambuc typedef NonAggregateTDC NATDCArray[2][2]; 574*f4a2713aSLionel Sambuc static_assert(NATDCArray{}[1][1].n == 0, ""); 575*f4a2713aSLionel Sambuc 576*f4a2713aSLionel Sambuc } 577*f4a2713aSLionel Sambuc 578*f4a2713aSLionel Sambuc namespace DependentValues { 579*f4a2713aSLionel Sambuc 580*f4a2713aSLionel Sambuc struct I { int n; typedef I V[10]; }; 581*f4a2713aSLionel Sambuc I::V x, y; 582*f4a2713aSLionel Sambuc int g(); 583*f4a2713aSLionel Sambuc template<bool B, typename T> struct S : T { 584*f4a2713aSLionel Sambuc int k; 585*f4a2713aSLionel Sambuc void f() { 586*f4a2713aSLionel Sambuc I::V &cells = B ? x : y; 587*f4a2713aSLionel Sambuc I &i = cells[k]; 588*f4a2713aSLionel Sambuc switch (i.n) {} 589*f4a2713aSLionel Sambuc 590*f4a2713aSLionel Sambuc // FIXME: We should be able to diagnose this. 591*f4a2713aSLionel Sambuc constexpr int n = g(); 592*f4a2713aSLionel Sambuc 593*f4a2713aSLionel Sambuc constexpr int m = this->g(); // ok, could be constexpr 594*f4a2713aSLionel Sambuc } 595*f4a2713aSLionel Sambuc }; 596*f4a2713aSLionel Sambuc 597*f4a2713aSLionel Sambuc } 598*f4a2713aSLionel Sambuc 599*f4a2713aSLionel Sambuc namespace Class { 600*f4a2713aSLionel Sambuc 601*f4a2713aSLionel Sambuc struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; 602*f4a2713aSLionel Sambuc constexpr int fn(const A &a) { return a.k; } 603*f4a2713aSLionel Sambuc static_assert(fn(A(4,5)) == 9, ""); 604*f4a2713aSLionel Sambuc 605*f4a2713aSLionel Sambuc struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}} 606*f4a2713aSLionel Sambuc struct C { 607*f4a2713aSLionel Sambuc constexpr C(C *this_) : m(42), n(this_->m) {} // ok 608*f4a2713aSLionel Sambuc int m, n; 609*f4a2713aSLionel Sambuc }; 610*f4a2713aSLionel Sambuc struct D { 611*f4a2713aSLionel Sambuc C c; 612*f4a2713aSLionel Sambuc constexpr D() : c(&c) {} 613*f4a2713aSLionel Sambuc }; 614*f4a2713aSLionel Sambuc static_assert(D().c.n == 42, ""); 615*f4a2713aSLionel Sambuc 616*f4a2713aSLionel Sambuc struct E { 617*f4a2713aSLionel Sambuc constexpr E() : p(&p) {} 618*f4a2713aSLionel Sambuc void *p; 619*f4a2713aSLionel Sambuc }; 620*f4a2713aSLionel Sambuc constexpr const E &e1 = E(); 621*f4a2713aSLionel Sambuc // This is a constant expression if we elide the copy constructor call, and 622*f4a2713aSLionel Sambuc // is not a constant expression if we don't! But we do, so it is. 623*f4a2713aSLionel Sambuc constexpr E e2 = E(); 624*f4a2713aSLionel Sambuc static_assert(e2.p == &e2.p, ""); 625*f4a2713aSLionel Sambuc constexpr E e3; 626*f4a2713aSLionel Sambuc static_assert(e3.p == &e3.p, ""); 627*f4a2713aSLionel Sambuc 628*f4a2713aSLionel Sambuc extern const class F f; 629*f4a2713aSLionel Sambuc struct F { 630*f4a2713aSLionel Sambuc constexpr F() : p(&f.p) {} 631*f4a2713aSLionel Sambuc const void *p; 632*f4a2713aSLionel Sambuc }; 633*f4a2713aSLionel Sambuc constexpr F f; 634*f4a2713aSLionel Sambuc 635*f4a2713aSLionel Sambuc struct G { 636*f4a2713aSLionel Sambuc struct T { 637*f4a2713aSLionel Sambuc constexpr T(T *p) : u1(), u2(p) {} 638*f4a2713aSLionel Sambuc union U1 { 639*f4a2713aSLionel Sambuc constexpr U1() {} 640*f4a2713aSLionel Sambuc int a, b = 42; 641*f4a2713aSLionel Sambuc } u1; 642*f4a2713aSLionel Sambuc union U2 { 643*f4a2713aSLionel Sambuc constexpr U2(T *p) : c(p->u1.b) {} 644*f4a2713aSLionel Sambuc int c, d; 645*f4a2713aSLionel Sambuc } u2; 646*f4a2713aSLionel Sambuc } t; 647*f4a2713aSLionel Sambuc constexpr G() : t(&t) {} 648*f4a2713aSLionel Sambuc } constexpr g; 649*f4a2713aSLionel Sambuc 650*f4a2713aSLionel Sambuc static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}} 651*f4a2713aSLionel Sambuc static_assert(g.t.u1.b == 42, ""); 652*f4a2713aSLionel Sambuc static_assert(g.t.u2.c == 42, ""); 653*f4a2713aSLionel Sambuc static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}} 654*f4a2713aSLionel Sambuc 655*f4a2713aSLionel Sambuc struct S { 656*f4a2713aSLionel Sambuc int a, b; 657*f4a2713aSLionel Sambuc const S *p; 658*f4a2713aSLionel Sambuc double d; 659*f4a2713aSLionel Sambuc const char *q; 660*f4a2713aSLionel Sambuc 661*f4a2713aSLionel Sambuc constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} 662*f4a2713aSLionel Sambuc }; 663*f4a2713aSLionel Sambuc 664*f4a2713aSLionel Sambuc S global(43, &global); 665*f4a2713aSLionel Sambuc 666*f4a2713aSLionel Sambuc static_assert(S(15, &global).b == 15, ""); 667*f4a2713aSLionel Sambuc 668*f4a2713aSLionel Sambuc constexpr bool CheckS(const S &s) { 669*f4a2713aSLionel Sambuc return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; 670*f4a2713aSLionel Sambuc } 671*f4a2713aSLionel Sambuc static_assert(CheckS(S(27, &global)), ""); 672*f4a2713aSLionel Sambuc 673*f4a2713aSLionel Sambuc struct Arr { 674*f4a2713aSLionel Sambuc char arr[3]; 675*f4a2713aSLionel Sambuc constexpr Arr() : arr{'x', 'y', 'z'} {} 676*f4a2713aSLionel Sambuc }; 677*f4a2713aSLionel Sambuc constexpr int hash(Arr &&a) { 678*f4a2713aSLionel Sambuc return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; 679*f4a2713aSLionel Sambuc } 680*f4a2713aSLionel Sambuc constexpr int k = hash(Arr()); 681*f4a2713aSLionel Sambuc static_assert(k == 0x007a7978, ""); 682*f4a2713aSLionel Sambuc 683*f4a2713aSLionel Sambuc 684*f4a2713aSLionel Sambuc struct AggregateInit { 685*f4a2713aSLionel Sambuc const char &c; 686*f4a2713aSLionel Sambuc int n; 687*f4a2713aSLionel Sambuc double d; 688*f4a2713aSLionel Sambuc int arr[5]; 689*f4a2713aSLionel Sambuc void *p; 690*f4a2713aSLionel Sambuc }; 691*f4a2713aSLionel Sambuc 692*f4a2713aSLionel Sambuc constexpr AggregateInit agg1 = { "hello"[0] }; 693*f4a2713aSLionel Sambuc 694*f4a2713aSLionel Sambuc static_assert(strcmp_ce(&agg1.c, "hello") == 0, ""); 695*f4a2713aSLionel Sambuc static_assert(agg1.n == 0, ""); 696*f4a2713aSLionel Sambuc static_assert(agg1.d == 0.0, ""); 697*f4a2713aSLionel Sambuc static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 698*f4a2713aSLionel Sambuc static_assert(agg1.arr[0] == 0, ""); 699*f4a2713aSLionel Sambuc static_assert(agg1.arr[4] == 0, ""); 700*f4a2713aSLionel Sambuc static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}} 701*f4a2713aSLionel Sambuc static_assert(agg1.p == nullptr, ""); 702*f4a2713aSLionel Sambuc 703*f4a2713aSLionel Sambuc static constexpr const unsigned char uc[] = { "foo" }; 704*f4a2713aSLionel Sambuc static_assert(uc[0] == 'f', ""); 705*f4a2713aSLionel Sambuc static_assert(uc[3] == 0, ""); 706*f4a2713aSLionel Sambuc 707*f4a2713aSLionel Sambuc namespace SimpleDerivedClass { 708*f4a2713aSLionel Sambuc 709*f4a2713aSLionel Sambuc struct B { 710*f4a2713aSLionel Sambuc constexpr B(int n) : a(n) {} 711*f4a2713aSLionel Sambuc int a; 712*f4a2713aSLionel Sambuc }; 713*f4a2713aSLionel Sambuc struct D : B { 714*f4a2713aSLionel Sambuc constexpr D(int n) : B(n) {} 715*f4a2713aSLionel Sambuc }; 716*f4a2713aSLionel Sambuc constexpr D d(3); 717*f4a2713aSLionel Sambuc static_assert(d.a == 3, ""); 718*f4a2713aSLionel Sambuc 719*f4a2713aSLionel Sambuc } 720*f4a2713aSLionel Sambuc 721*f4a2713aSLionel Sambuc struct Bottom { constexpr Bottom() {} }; 722*f4a2713aSLionel Sambuc struct Base : Bottom { 723*f4a2713aSLionel Sambuc constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} 724*f4a2713aSLionel Sambuc int a; 725*f4a2713aSLionel Sambuc const char *b; 726*f4a2713aSLionel Sambuc }; 727*f4a2713aSLionel Sambuc struct Base2 : Bottom { 728*f4a2713aSLionel Sambuc constexpr Base2(const int &r) : r(r) {} 729*f4a2713aSLionel Sambuc int q = 123; 730*f4a2713aSLionel Sambuc const int &r; 731*f4a2713aSLionel Sambuc }; 732*f4a2713aSLionel Sambuc struct Derived : Base, Base2 { 733*f4a2713aSLionel Sambuc constexpr Derived() : Base(76), Base2(a) {} 734*f4a2713aSLionel Sambuc int c = r + b[1]; 735*f4a2713aSLionel Sambuc }; 736*f4a2713aSLionel Sambuc 737*f4a2713aSLionel Sambuc constexpr bool operator==(const Base &a, const Base &b) { 738*f4a2713aSLionel Sambuc return a.a == b.a && strcmp_ce(a.b, b.b) == 0; 739*f4a2713aSLionel Sambuc } 740*f4a2713aSLionel Sambuc 741*f4a2713aSLionel Sambuc constexpr Base base; 742*f4a2713aSLionel Sambuc constexpr Base base2(76); 743*f4a2713aSLionel Sambuc constexpr Derived derived; 744*f4a2713aSLionel Sambuc static_assert(derived.a == 76, ""); 745*f4a2713aSLionel Sambuc static_assert(derived.b[2] == 's', ""); 746*f4a2713aSLionel Sambuc static_assert(derived.c == 76 + 'e', ""); 747*f4a2713aSLionel Sambuc static_assert(derived.q == 123, ""); 748*f4a2713aSLionel Sambuc static_assert(derived.r == 76, ""); 749*f4a2713aSLionel Sambuc static_assert(&derived.r == &derived.a, ""); 750*f4a2713aSLionel Sambuc 751*f4a2713aSLionel Sambuc static_assert(!(derived == base), ""); 752*f4a2713aSLionel Sambuc static_assert(derived == base2, ""); 753*f4a2713aSLionel Sambuc 754*f4a2713aSLionel Sambuc constexpr Bottom &bot1 = (Base&)derived; 755*f4a2713aSLionel Sambuc constexpr Bottom &bot2 = (Base2&)derived; 756*f4a2713aSLionel Sambuc static_assert(&bot1 != &bot2, ""); 757*f4a2713aSLionel Sambuc 758*f4a2713aSLionel Sambuc constexpr Bottom *pb1 = (Base*)&derived; 759*f4a2713aSLionel Sambuc constexpr Bottom *pb2 = (Base2*)&derived; 760*f4a2713aSLionel Sambuc static_assert(&pb1 != &pb2, ""); 761*f4a2713aSLionel Sambuc static_assert(pb1 == &bot1, ""); 762*f4a2713aSLionel Sambuc static_assert(pb2 == &bot2, ""); 763*f4a2713aSLionel Sambuc 764*f4a2713aSLionel Sambuc constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}} 765*f4a2713aSLionel Sambuc constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}} 766*f4a2713aSLionel Sambuc constexpr Base2 &ok2 = (Base2&)bot2; 767*f4a2713aSLionel Sambuc static_assert(&ok2 == &derived, ""); 768*f4a2713aSLionel Sambuc 769*f4a2713aSLionel Sambuc constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}} 770*f4a2713aSLionel Sambuc constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}} 771*f4a2713aSLionel Sambuc constexpr Base2 *pok2 = (Base2*)pb2; 772*f4a2713aSLionel Sambuc static_assert(pok2 == &derived, ""); 773*f4a2713aSLionel Sambuc static_assert(&ok2 == pok2, ""); 774*f4a2713aSLionel Sambuc static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, ""); 775*f4a2713aSLionel Sambuc static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, ""); 776*f4a2713aSLionel Sambuc 777*f4a2713aSLionel Sambuc // Core issue 903: we do not perform constant evaluation when checking for a 778*f4a2713aSLionel Sambuc // null pointer in C++11. Just check for an integer literal with value 0. 779*f4a2713aSLionel Sambuc constexpr Base *nullB = 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Class::Base *const' with an rvalue of type 'int'}} 780*f4a2713aSLionel Sambuc constexpr Base *nullB1 = 0; 781*f4a2713aSLionel Sambuc static_assert((Bottom*)nullB == 0, ""); 782*f4a2713aSLionel Sambuc static_assert((Derived*)nullB == 0, ""); 783*f4a2713aSLionel Sambuc static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); 784*f4a2713aSLionel Sambuc Base *nullB2 = '\0'; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'char'}} 785*f4a2713aSLionel Sambuc Base *nullB3 = (0); 786*f4a2713aSLionel Sambuc Base *nullB4 = false; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'bool'}} 787*f4a2713aSLionel Sambuc Base *nullB5 = ((0ULL)); 788*f4a2713aSLionel Sambuc Base *nullB6 = 0.; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'double'}} 789*f4a2713aSLionel Sambuc enum Null { kNull }; 790*f4a2713aSLionel Sambuc Base *nullB7 = kNull; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'Class::Null'}} 791*f4a2713aSLionel Sambuc static_assert(nullB1 == (1 - 1), ""); // expected-error {{comparison between pointer and integer}} 792*f4a2713aSLionel Sambuc 793*f4a2713aSLionel Sambuc 794*f4a2713aSLionel Sambuc 795*f4a2713aSLionel Sambuc namespace ConversionOperators { 796*f4a2713aSLionel Sambuc 797*f4a2713aSLionel Sambuc struct T { 798*f4a2713aSLionel Sambuc constexpr T(int n) : k(5*n - 3) {} 799*f4a2713aSLionel Sambuc constexpr operator int() const { return k; } 800*f4a2713aSLionel Sambuc int k; 801*f4a2713aSLionel Sambuc }; 802*f4a2713aSLionel Sambuc 803*f4a2713aSLionel Sambuc struct S { 804*f4a2713aSLionel Sambuc constexpr S(int n) : k(2*n + 1) {} 805*f4a2713aSLionel Sambuc constexpr operator int() const { return k; } 806*f4a2713aSLionel Sambuc constexpr operator T() const { return T(k); } 807*f4a2713aSLionel Sambuc int k; 808*f4a2713aSLionel Sambuc }; 809*f4a2713aSLionel Sambuc 810*f4a2713aSLionel Sambuc constexpr bool check(T a, T b) { return a == b.k; } 811*f4a2713aSLionel Sambuc 812*f4a2713aSLionel Sambuc static_assert(S(5) == 11, ""); 813*f4a2713aSLionel Sambuc static_assert(check(S(5), 11), ""); 814*f4a2713aSLionel Sambuc 815*f4a2713aSLionel Sambuc namespace PR14171 { 816*f4a2713aSLionel Sambuc 817*f4a2713aSLionel Sambuc struct X { 818*f4a2713aSLionel Sambuc constexpr (operator int)() const { return 0; } 819*f4a2713aSLionel Sambuc }; 820*f4a2713aSLionel Sambuc static_assert(X() == 0, ""); 821*f4a2713aSLionel Sambuc 822*f4a2713aSLionel Sambuc } 823*f4a2713aSLionel Sambuc 824*f4a2713aSLionel Sambuc } 825*f4a2713aSLionel Sambuc 826*f4a2713aSLionel Sambuc } 827*f4a2713aSLionel Sambuc 828*f4a2713aSLionel Sambuc namespace Temporaries { 829*f4a2713aSLionel Sambuc 830*f4a2713aSLionel Sambuc struct S { 831*f4a2713aSLionel Sambuc constexpr S() {} 832*f4a2713aSLionel Sambuc constexpr int f() const; 833*f4a2713aSLionel Sambuc constexpr int g() const; 834*f4a2713aSLionel Sambuc }; 835*f4a2713aSLionel Sambuc struct T : S { 836*f4a2713aSLionel Sambuc constexpr T(int n) : S(), n(n) {} 837*f4a2713aSLionel Sambuc int n; 838*f4a2713aSLionel Sambuc }; 839*f4a2713aSLionel Sambuc constexpr int S::f() const { 840*f4a2713aSLionel Sambuc return static_cast<const T*>(this)->n; // expected-note {{cannot cast}} 841*f4a2713aSLionel Sambuc } 842*f4a2713aSLionel Sambuc constexpr int S::g() const { 843*f4a2713aSLionel Sambuc // FIXME: Better diagnostic for this. 844*f4a2713aSLionel Sambuc return this->*(int(S::*))&T::n; // expected-note {{subexpression}} 845*f4a2713aSLionel Sambuc } 846*f4a2713aSLionel Sambuc // The T temporary is implicitly cast to an S subobject, but we can recover the 847*f4a2713aSLionel Sambuc // T full-object via a base-to-derived cast, or a derived-to-base-casted member 848*f4a2713aSLionel Sambuc // pointer. 849*f4a2713aSLionel Sambuc static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->f()'}} 850*f4a2713aSLionel Sambuc static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->g()'}} 851*f4a2713aSLionel Sambuc static_assert(T(3).f() == 3, ""); 852*f4a2713aSLionel Sambuc static_assert(T(4).g() == 4, ""); 853*f4a2713aSLionel Sambuc 854*f4a2713aSLionel Sambuc constexpr int f(const S &s) { 855*f4a2713aSLionel Sambuc return static_cast<const T&>(s).n; 856*f4a2713aSLionel Sambuc } 857*f4a2713aSLionel Sambuc constexpr int n = f(T(5)); 858*f4a2713aSLionel Sambuc static_assert(f(T(5)) == 5, ""); 859*f4a2713aSLionel Sambuc 860*f4a2713aSLionel Sambuc constexpr bool b(int n) { return &n; } 861*f4a2713aSLionel Sambuc static_assert(b(0), ""); 862*f4a2713aSLionel Sambuc 863*f4a2713aSLionel Sambuc } 864*f4a2713aSLionel Sambuc 865*f4a2713aSLionel Sambuc namespace Union { 866*f4a2713aSLionel Sambuc 867*f4a2713aSLionel Sambuc union U { 868*f4a2713aSLionel Sambuc int a; 869*f4a2713aSLionel Sambuc int b; 870*f4a2713aSLionel Sambuc }; 871*f4a2713aSLionel Sambuc 872*f4a2713aSLionel Sambuc constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}} 873*f4a2713aSLionel Sambuc static_assert(u[0].a == 0, ""); 874*f4a2713aSLionel Sambuc static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}} 875*f4a2713aSLionel Sambuc static_assert(u[1].b == 1, ""); 876*f4a2713aSLionel Sambuc static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 877*f4a2713aSLionel Sambuc static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}} 878*f4a2713aSLionel Sambuc static_assert((&(u[1]) + 1 + 1)->b == 3, ""); 879*f4a2713aSLionel Sambuc 880*f4a2713aSLionel Sambuc constexpr U v = {}; 881*f4a2713aSLionel Sambuc static_assert(v.a == 0, ""); 882*f4a2713aSLionel Sambuc 883*f4a2713aSLionel Sambuc union Empty {}; 884*f4a2713aSLionel Sambuc constexpr Empty e = {}; 885*f4a2713aSLionel Sambuc 886*f4a2713aSLionel Sambuc // Make sure we handle trivial copy constructors for unions. 887*f4a2713aSLionel Sambuc constexpr U x = {42}; 888*f4a2713aSLionel Sambuc constexpr U y = x; 889*f4a2713aSLionel Sambuc static_assert(y.a == 42, ""); 890*f4a2713aSLionel Sambuc static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}} 891*f4a2713aSLionel Sambuc 892*f4a2713aSLionel Sambuc } 893*f4a2713aSLionel Sambuc 894*f4a2713aSLionel Sambuc namespace MemberPointer { 895*f4a2713aSLionel Sambuc struct A { 896*f4a2713aSLionel Sambuc constexpr A(int n) : n(n) {} 897*f4a2713aSLionel Sambuc int n; 898*f4a2713aSLionel Sambuc constexpr int f() const { return n + 3; } 899*f4a2713aSLionel Sambuc }; 900*f4a2713aSLionel Sambuc constexpr A a(7); 901*f4a2713aSLionel Sambuc static_assert(A(5).*&A::n == 5, ""); 902*f4a2713aSLionel Sambuc static_assert((&a)->*&A::n == 7, ""); 903*f4a2713aSLionel Sambuc static_assert((A(8).*&A::f)() == 11, ""); 904*f4a2713aSLionel Sambuc static_assert(((&a)->*&A::f)() == 10, ""); 905*f4a2713aSLionel Sambuc 906*f4a2713aSLionel Sambuc struct B : A { 907*f4a2713aSLionel Sambuc constexpr B(int n, int m) : A(n), m(m) {} 908*f4a2713aSLionel Sambuc int m; 909*f4a2713aSLionel Sambuc constexpr int g() const { return n + m + 1; } 910*f4a2713aSLionel Sambuc }; 911*f4a2713aSLionel Sambuc constexpr B b(9, 13); 912*f4a2713aSLionel Sambuc static_assert(B(4, 11).*&A::n == 4, ""); 913*f4a2713aSLionel Sambuc static_assert(B(4, 11).*&B::m == 11, ""); 914*f4a2713aSLionel Sambuc static_assert(B(4, 11).*(int(A::*))&B::m == 11, ""); 915*f4a2713aSLionel Sambuc static_assert((&b)->*&A::n == 9, ""); 916*f4a2713aSLionel Sambuc static_assert((&b)->*&B::m == 13, ""); 917*f4a2713aSLionel Sambuc static_assert((&b)->*(int(A::*))&B::m == 13, ""); 918*f4a2713aSLionel Sambuc static_assert((B(4, 11).*&A::f)() == 7, ""); 919*f4a2713aSLionel Sambuc static_assert((B(4, 11).*&B::g)() == 16, ""); 920*f4a2713aSLionel Sambuc static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); 921*f4a2713aSLionel Sambuc static_assert(((&b)->*&A::f)() == 12, ""); 922*f4a2713aSLionel Sambuc static_assert(((&b)->*&B::g)() == 23, ""); 923*f4a2713aSLionel Sambuc static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); 924*f4a2713aSLionel Sambuc 925*f4a2713aSLionel Sambuc struct S { 926*f4a2713aSLionel Sambuc constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : 927*f4a2713aSLionel Sambuc m(m), n(n), pf(pf), pn(pn) {} 928*f4a2713aSLionel Sambuc constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} 929*f4a2713aSLionel Sambuc 930*f4a2713aSLionel Sambuc constexpr int f() const { return this->*pn; } 931*f4a2713aSLionel Sambuc virtual int g() const; 932*f4a2713aSLionel Sambuc 933*f4a2713aSLionel Sambuc int m, n; 934*f4a2713aSLionel Sambuc int (S::*pf)() const; 935*f4a2713aSLionel Sambuc int S::*pn; 936*f4a2713aSLionel Sambuc }; 937*f4a2713aSLionel Sambuc 938*f4a2713aSLionel Sambuc constexpr int S::*pm = &S::m; 939*f4a2713aSLionel Sambuc constexpr int S::*pn = &S::n; 940*f4a2713aSLionel Sambuc constexpr int (S::*pf)() const = &S::f; 941*f4a2713aSLionel Sambuc constexpr int (S::*pg)() const = &S::g; 942*f4a2713aSLionel Sambuc 943*f4a2713aSLionel Sambuc constexpr S s(2, 5, &S::f, &S::m); 944*f4a2713aSLionel Sambuc 945*f4a2713aSLionel Sambuc static_assert((s.*&S::f)() == 2, ""); 946*f4a2713aSLionel Sambuc static_assert((s.*s.pf)() == 2, ""); 947*f4a2713aSLionel Sambuc 948*f4a2713aSLionel Sambuc static_assert(pf == &S::f, ""); 949*f4a2713aSLionel Sambuc static_assert(pf == s.*&S::pf, ""); 950*f4a2713aSLionel Sambuc static_assert(pm == &S::m, ""); 951*f4a2713aSLionel Sambuc static_assert(pm != pn, ""); 952*f4a2713aSLionel Sambuc static_assert(s.pn != pn, ""); 953*f4a2713aSLionel Sambuc static_assert(s.pn == pm, ""); 954*f4a2713aSLionel Sambuc static_assert(pg != nullptr, ""); 955*f4a2713aSLionel Sambuc static_assert(pf != nullptr, ""); 956*f4a2713aSLionel Sambuc static_assert((int S::*)nullptr == nullptr, ""); 957*f4a2713aSLionel Sambuc static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} 958*f4a2713aSLionel Sambuc static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} 959*f4a2713aSLionel Sambuc 960*f4a2713aSLionel Sambuc template<int n> struct T : T<n-1> {}; 961*f4a2713aSLionel Sambuc template<> struct T<0> { int n; }; 962*f4a2713aSLionel Sambuc template<> struct T<30> : T<29> { int m; }; 963*f4a2713aSLionel Sambuc 964*f4a2713aSLionel Sambuc T<17> t17; 965*f4a2713aSLionel Sambuc T<30> t30; 966*f4a2713aSLionel Sambuc 967*f4a2713aSLionel Sambuc constexpr int (T<10>::*deepn) = &T<0>::n; 968*f4a2713aSLionel Sambuc static_assert(&(t17.*deepn) == &t17.n, ""); 969*f4a2713aSLionel Sambuc static_assert(deepn == &T<2>::n, ""); 970*f4a2713aSLionel Sambuc 971*f4a2713aSLionel Sambuc constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; 972*f4a2713aSLionel Sambuc constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} 973*f4a2713aSLionel Sambuc static_assert(&(t30.*deepm) == &t30.m, ""); 974*f4a2713aSLionel Sambuc static_assert(deepm == &T<50>::m, ""); 975*f4a2713aSLionel Sambuc static_assert(deepm != deepn, ""); 976*f4a2713aSLionel Sambuc 977*f4a2713aSLionel Sambuc constexpr T<5> *p17_5 = &t17; 978*f4a2713aSLionel Sambuc constexpr T<13> *p17_13 = (T<13>*)p17_5; 979*f4a2713aSLionel Sambuc constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'T<17>' to type 'T<23>'}} 980*f4a2713aSLionel Sambuc static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); 981*f4a2713aSLionel Sambuc static_assert(&(p17_13->*deepn) == &t17.n, ""); 982*f4a2713aSLionel Sambuc constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} 983*f4a2713aSLionel Sambuc 984*f4a2713aSLionel Sambuc constexpr T<5> *p30_5 = &t30; 985*f4a2713aSLionel Sambuc constexpr T<23> *p30_23 = (T<23>*)p30_5; 986*f4a2713aSLionel Sambuc constexpr T<13> *p30_13 = p30_23; 987*f4a2713aSLionel Sambuc static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); 988*f4a2713aSLionel Sambuc static_assert(&(p30_13->*deepn) == &t30.n, ""); 989*f4a2713aSLionel Sambuc static_assert(&(p30_23->*deepn) == &t30.n, ""); 990*f4a2713aSLionel Sambuc static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); 991*f4a2713aSLionel Sambuc static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); 992*f4a2713aSLionel Sambuc static_assert(&(p30_23->*deepm) == &t30.m, ""); 993*f4a2713aSLionel Sambuc 994*f4a2713aSLionel Sambuc struct Base { int n; }; 995*f4a2713aSLionel Sambuc template<int N> struct Mid : Base {}; 996*f4a2713aSLionel Sambuc struct Derived : Mid<0>, Mid<1> {}; 997*f4a2713aSLionel Sambuc static_assert(&Mid<0>::n == &Mid<1>::n, ""); 998*f4a2713aSLionel Sambuc static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n != 999*f4a2713aSLionel Sambuc (int Derived::*)(int Mid<1>::*)&Mid<1>::n, ""); 1000*f4a2713aSLionel Sambuc static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, ""); 1001*f4a2713aSLionel Sambuc } 1002*f4a2713aSLionel Sambuc 1003*f4a2713aSLionel Sambuc namespace ArrayBaseDerived { 1004*f4a2713aSLionel Sambuc 1005*f4a2713aSLionel Sambuc struct Base { 1006*f4a2713aSLionel Sambuc constexpr Base() {} 1007*f4a2713aSLionel Sambuc int n = 0; 1008*f4a2713aSLionel Sambuc }; 1009*f4a2713aSLionel Sambuc struct Derived : Base { 1010*f4a2713aSLionel Sambuc constexpr Derived() {} 1011*f4a2713aSLionel Sambuc constexpr const int *f() const { return &n; } 1012*f4a2713aSLionel Sambuc }; 1013*f4a2713aSLionel Sambuc 1014*f4a2713aSLionel Sambuc constexpr Derived a[10]; 1015*f4a2713aSLionel Sambuc constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); 1016*f4a2713aSLionel Sambuc constexpr Base *pb3 = const_cast<Derived*>(&a[3]); 1017*f4a2713aSLionel Sambuc static_assert(pb3 == pd3, ""); 1018*f4a2713aSLionel Sambuc 1019*f4a2713aSLionel Sambuc // pb3 does not point to an array element. 1020*f4a2713aSLionel Sambuc constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. 1021*f4a2713aSLionel Sambuc constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}} 1022*f4a2713aSLionel Sambuc constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}} 1023*f4a2713aSLionel Sambuc constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}} 1024*f4a2713aSLionel Sambuc constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}} 1025*f4a2713aSLionel Sambuc constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}} 1026*f4a2713aSLionel Sambuc constexpr Base *pb3a = pb4 - 1; 1027*f4a2713aSLionel Sambuc 1028*f4a2713aSLionel Sambuc // pb4 does not point to a Derived. 1029*f4a2713aSLionel Sambuc constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}} 1030*f4a2713aSLionel Sambuc constexpr Derived *pd3a = (Derived*)pb3a; 1031*f4a2713aSLionel Sambuc constexpr int pd3n = pd3a->n; 1032*f4a2713aSLionel Sambuc 1033*f4a2713aSLionel Sambuc // pd3a still points to the Derived array. 1034*f4a2713aSLionel Sambuc constexpr Derived *pd6 = pd3a + 3; 1035*f4a2713aSLionel Sambuc static_assert(pd6 == &a[6], ""); 1036*f4a2713aSLionel Sambuc constexpr Derived *pd9 = pd6 + 3; 1037*f4a2713aSLionel Sambuc constexpr Derived *pd10 = pd6 + 4; 1038*f4a2713aSLionel Sambuc constexpr int pd9n = pd9->n; // ok 1039*f4a2713aSLionel Sambuc constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}} 1040*f4a2713aSLionel Sambuc constexpr int pd0n = pd10[-10].n; 1041*f4a2713aSLionel Sambuc constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}} 1042*f4a2713aSLionel Sambuc 1043*f4a2713aSLionel Sambuc constexpr Base *pb9 = pd9; 1044*f4a2713aSLionel Sambuc constexpr const int *(Base::*pfb)() const = 1045*f4a2713aSLionel Sambuc static_cast<const int *(Base::*)() const>(&Derived::f); 1046*f4a2713aSLionel Sambuc static_assert((pb9->*pfb)() == &a[9].n, ""); 1047*f4a2713aSLionel Sambuc } 1048*f4a2713aSLionel Sambuc 1049*f4a2713aSLionel Sambuc namespace Complex { 1050*f4a2713aSLionel Sambuc 1051*f4a2713aSLionel Sambuc class complex { 1052*f4a2713aSLionel Sambuc int re, im; 1053*f4a2713aSLionel Sambuc public: 1054*f4a2713aSLionel Sambuc constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} 1055*f4a2713aSLionel Sambuc constexpr complex(const complex &o) : re(o.re), im(o.im) {} 1056*f4a2713aSLionel Sambuc constexpr complex operator-() const { return complex(-re, -im); } 1057*f4a2713aSLionel Sambuc friend constexpr complex operator+(const complex &l, const complex &r) { 1058*f4a2713aSLionel Sambuc return complex(l.re + r.re, l.im + r.im); 1059*f4a2713aSLionel Sambuc } 1060*f4a2713aSLionel Sambuc friend constexpr complex operator-(const complex &l, const complex &r) { 1061*f4a2713aSLionel Sambuc return l + -r; 1062*f4a2713aSLionel Sambuc } 1063*f4a2713aSLionel Sambuc friend constexpr complex operator*(const complex &l, const complex &r) { 1064*f4a2713aSLionel Sambuc return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); 1065*f4a2713aSLionel Sambuc } 1066*f4a2713aSLionel Sambuc friend constexpr bool operator==(const complex &l, const complex &r) { 1067*f4a2713aSLionel Sambuc return l.re == r.re && l.im == r.im; 1068*f4a2713aSLionel Sambuc } 1069*f4a2713aSLionel Sambuc constexpr bool operator!=(const complex &r) const { 1070*f4a2713aSLionel Sambuc return re != r.re || im != r.im; 1071*f4a2713aSLionel Sambuc } 1072*f4a2713aSLionel Sambuc constexpr int real() const { return re; } 1073*f4a2713aSLionel Sambuc constexpr int imag() const { return im; } 1074*f4a2713aSLionel Sambuc }; 1075*f4a2713aSLionel Sambuc 1076*f4a2713aSLionel Sambuc constexpr complex i = complex(0, 1); 1077*f4a2713aSLionel Sambuc constexpr complex k = (3 + 4*i) * (6 - 4*i); 1078*f4a2713aSLionel Sambuc static_assert(complex(1,0).real() == 1, ""); 1079*f4a2713aSLionel Sambuc static_assert(complex(1,0).imag() == 0, ""); 1080*f4a2713aSLionel Sambuc static_assert(((complex)1).imag() == 0, ""); 1081*f4a2713aSLionel Sambuc static_assert(k.real() == 34, ""); 1082*f4a2713aSLionel Sambuc static_assert(k.imag() == 12, ""); 1083*f4a2713aSLionel Sambuc static_assert(k - 34 == 12*i, ""); 1084*f4a2713aSLionel Sambuc static_assert((complex)1 == complex(1), ""); 1085*f4a2713aSLionel Sambuc static_assert((complex)1 != complex(0, 1), ""); 1086*f4a2713aSLionel Sambuc static_assert(complex(1) == complex(1), ""); 1087*f4a2713aSLionel Sambuc static_assert(complex(1) != complex(0, 1), ""); 1088*f4a2713aSLionel Sambuc constexpr complex makeComplex(int re, int im) { return complex(re, im); } 1089*f4a2713aSLionel Sambuc static_assert(makeComplex(1,0) == complex(1), ""); 1090*f4a2713aSLionel Sambuc static_assert(makeComplex(1,0) != complex(0, 1), ""); 1091*f4a2713aSLionel Sambuc 1092*f4a2713aSLionel Sambuc class complex_wrap : public complex { 1093*f4a2713aSLionel Sambuc public: 1094*f4a2713aSLionel Sambuc constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} 1095*f4a2713aSLionel Sambuc constexpr complex_wrap(const complex_wrap &o) : complex(o) {} 1096*f4a2713aSLionel Sambuc }; 1097*f4a2713aSLionel Sambuc 1098*f4a2713aSLionel Sambuc static_assert((complex_wrap)1 == complex(1), ""); 1099*f4a2713aSLionel Sambuc static_assert((complex)1 != complex_wrap(0, 1), ""); 1100*f4a2713aSLionel Sambuc static_assert(complex(1) == complex_wrap(1), ""); 1101*f4a2713aSLionel Sambuc static_assert(complex_wrap(1) != complex(0, 1), ""); 1102*f4a2713aSLionel Sambuc constexpr complex_wrap makeComplexWrap(int re, int im) { 1103*f4a2713aSLionel Sambuc return complex_wrap(re, im); 1104*f4a2713aSLionel Sambuc } 1105*f4a2713aSLionel Sambuc static_assert(makeComplexWrap(1,0) == complex(1), ""); 1106*f4a2713aSLionel Sambuc static_assert(makeComplexWrap(1,0) != complex(0, 1), ""); 1107*f4a2713aSLionel Sambuc 1108*f4a2713aSLionel Sambuc } 1109*f4a2713aSLionel Sambuc 1110*f4a2713aSLionel Sambuc namespace PR11595 { 1111*f4a2713aSLionel Sambuc struct A { constexpr bool operator==(int x) const { return true; } }; 1112*f4a2713aSLionel Sambuc struct B { B(); A& x; }; 1113*f4a2713aSLionel Sambuc static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} 1114*f4a2713aSLionel Sambuc 1115*f4a2713aSLionel Sambuc constexpr bool f(int k) { // expected-error {{constexpr function never produces a constant expression}} 1116*f4a2713aSLionel Sambuc return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} 1117*f4a2713aSLionel Sambuc } 1118*f4a2713aSLionel Sambuc } 1119*f4a2713aSLionel Sambuc 1120*f4a2713aSLionel Sambuc namespace ExprWithCleanups { 1121*f4a2713aSLionel Sambuc struct A { A(); ~A(); int get(); }; 1122*f4a2713aSLionel Sambuc constexpr int get(bool FromA) { return FromA ? A().get() : 1; } 1123*f4a2713aSLionel Sambuc constexpr int n = get(false); 1124*f4a2713aSLionel Sambuc } 1125*f4a2713aSLionel Sambuc 1126*f4a2713aSLionel Sambuc namespace Volatile { 1127*f4a2713aSLionel Sambuc 1128*f4a2713aSLionel Sambuc volatile constexpr int n1 = 0; // expected-note {{here}} 1129*f4a2713aSLionel Sambuc volatile const int n2 = 0; // expected-note {{here}} 1130*f4a2713aSLionel Sambuc int n3 = 37; // expected-note {{declared here}} 1131*f4a2713aSLionel Sambuc 1132*f4a2713aSLionel Sambuc constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} 1133*f4a2713aSLionel Sambuc constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} 1134*f4a2713aSLionel Sambuc constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}} 1135*f4a2713aSLionel Sambuc constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}} 1136*f4a2713aSLionel Sambuc 1137*f4a2713aSLionel Sambuc struct T { int n; }; 1138*f4a2713aSLionel Sambuc const T t = { 42 }; // expected-note {{declared here}} 1139*f4a2713aSLionel Sambuc 1140*f4a2713aSLionel Sambuc constexpr int f(volatile int &&r) { 1141*f4a2713aSLionel Sambuc return r; // expected-note {{read of volatile-qualified type 'volatile int'}} 1142*f4a2713aSLionel Sambuc } 1143*f4a2713aSLionel Sambuc constexpr int g(volatile int &&r) { 1144*f4a2713aSLionel Sambuc return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}} 1145*f4a2713aSLionel Sambuc } 1146*f4a2713aSLionel Sambuc struct S { 1147*f4a2713aSLionel Sambuc int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}} 1148*f4a2713aSLionel Sambuc int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}} 1149*f4a2713aSLionel Sambuc int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}} 1150*f4a2713aSLionel Sambuc int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}} 1151*f4a2713aSLionel Sambuc }; 1152*f4a2713aSLionel Sambuc 1153*f4a2713aSLionel Sambuc } 1154*f4a2713aSLionel Sambuc 1155*f4a2713aSLionel Sambuc namespace ExternConstexpr { 1156*f4a2713aSLionel Sambuc extern constexpr int n = 0; 1157*f4a2713aSLionel Sambuc extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}} 1158*f4a2713aSLionel Sambuc void f() { 1159*f4a2713aSLionel Sambuc extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}} 1160*f4a2713aSLionel Sambuc constexpr int j = 0; 1161*f4a2713aSLionel Sambuc constexpr int k; // expected-error {{default initialization of an object of const type}} 1162*f4a2713aSLionel Sambuc } 1163*f4a2713aSLionel Sambuc } 1164*f4a2713aSLionel Sambuc 1165*f4a2713aSLionel Sambuc namespace ComplexConstexpr { 1166*f4a2713aSLionel Sambuc constexpr _Complex float test1 = {}; 1167*f4a2713aSLionel Sambuc constexpr _Complex float test2 = {1}; 1168*f4a2713aSLionel Sambuc constexpr _Complex double test3 = {1,2}; 1169*f4a2713aSLionel Sambuc constexpr _Complex int test4 = {4}; 1170*f4a2713aSLionel Sambuc constexpr _Complex int test5 = 4; 1171*f4a2713aSLionel Sambuc constexpr _Complex int test6 = {5,6}; 1172*f4a2713aSLionel Sambuc typedef _Complex float fcomplex; 1173*f4a2713aSLionel Sambuc constexpr fcomplex test7 = fcomplex(); 1174*f4a2713aSLionel Sambuc 1175*f4a2713aSLionel Sambuc constexpr const double &t2r = __real test3; 1176*f4a2713aSLionel Sambuc constexpr const double &t2i = __imag test3; 1177*f4a2713aSLionel Sambuc static_assert(&t2r + 1 == &t2i, ""); 1178*f4a2713aSLionel Sambuc static_assert(t2r == 1.0, ""); 1179*f4a2713aSLionel Sambuc static_assert(t2i == 2.0, ""); 1180*f4a2713aSLionel Sambuc constexpr const double *t2p = &t2r; 1181*f4a2713aSLionel Sambuc static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}} 1182*f4a2713aSLionel Sambuc static_assert(t2p[0] == 1.0, ""); 1183*f4a2713aSLionel Sambuc static_assert(t2p[1] == 2.0, ""); 1184*f4a2713aSLionel Sambuc static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}} 1185*f4a2713aSLionel Sambuc static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}} 1186*f4a2713aSLionel Sambuc constexpr _Complex float *p = 0; 1187*f4a2713aSLionel Sambuc constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{cannot access real component of null}} 1188*f4a2713aSLionel Sambuc constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}} 1189*f4a2713aSLionel Sambuc constexpr const _Complex double *q = &test3 + 1; 1190*f4a2713aSLionel Sambuc constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}} 1191*f4a2713aSLionel Sambuc constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}} 1192*f4a2713aSLionel Sambuc 1193*f4a2713aSLionel Sambuc static_assert(__real test6 == 5, ""); 1194*f4a2713aSLionel Sambuc static_assert(__imag test6 == 6, ""); 1195*f4a2713aSLionel Sambuc static_assert(&__imag test6 == &__real test6 + 1, ""); 1196*f4a2713aSLionel Sambuc } 1197*f4a2713aSLionel Sambuc 1198*f4a2713aSLionel Sambuc // _Atomic(T) is exactly like T for the purposes of constant expression 1199*f4a2713aSLionel Sambuc // evaluation.. 1200*f4a2713aSLionel Sambuc namespace Atomic { 1201*f4a2713aSLionel Sambuc constexpr _Atomic int n = 3; 1202*f4a2713aSLionel Sambuc 1203*f4a2713aSLionel Sambuc struct S { _Atomic(double) d; }; 1204*f4a2713aSLionel Sambuc constexpr S s = { 0.5 }; 1205*f4a2713aSLionel Sambuc constexpr double d1 = s.d; 1206*f4a2713aSLionel Sambuc constexpr double d2 = n; 1207*f4a2713aSLionel Sambuc constexpr _Atomic double d3 = n; 1208*f4a2713aSLionel Sambuc 1209*f4a2713aSLionel Sambuc constexpr _Atomic(int) n2 = d3; 1210*f4a2713aSLionel Sambuc static_assert(d1 == 0.5, ""); 1211*f4a2713aSLionel Sambuc static_assert(d3 == 3.0, ""); 1212*f4a2713aSLionel Sambuc 1213*f4a2713aSLionel Sambuc namespace PR16056 { 1214*f4a2713aSLionel Sambuc struct TestVar { 1215*f4a2713aSLionel Sambuc _Atomic(int) value; 1216*f4a2713aSLionel Sambuc constexpr TestVar(int value) : value(value) {} 1217*f4a2713aSLionel Sambuc }; 1218*f4a2713aSLionel Sambuc constexpr TestVar testVar{-1}; 1219*f4a2713aSLionel Sambuc static_assert(testVar.value == -1, ""); 1220*f4a2713aSLionel Sambuc } 1221*f4a2713aSLionel Sambuc } 1222*f4a2713aSLionel Sambuc 1223*f4a2713aSLionel Sambuc namespace InstantiateCaseStmt { 1224*f4a2713aSLionel Sambuc template<int x> constexpr int f() { return x; } 1225*f4a2713aSLionel Sambuc template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; } 1226*f4a2713aSLionel Sambuc int gg(int c) { return g<4>(c); } 1227*f4a2713aSLionel Sambuc } 1228*f4a2713aSLionel Sambuc 1229*f4a2713aSLionel Sambuc namespace ConvertedConstantExpr { 1230*f4a2713aSLionel Sambuc extern int &m; 1231*f4a2713aSLionel Sambuc extern int &n; 1232*f4a2713aSLionel Sambuc 1233*f4a2713aSLionel Sambuc constexpr int k = 4; 1234*f4a2713aSLionel Sambuc int &m = const_cast<int&>(k); 1235*f4a2713aSLionel Sambuc 1236*f4a2713aSLionel Sambuc // If we have nothing more interesting to say, ensure we don't produce a 1237*f4a2713aSLionel Sambuc // useless note and instead just point to the non-constant subexpression. 1238*f4a2713aSLionel Sambuc enum class E { 1239*f4a2713aSLionel Sambuc em = m, 1240*f4a2713aSLionel Sambuc en = n, // expected-error {{not a constant expression}} 1241*f4a2713aSLionel Sambuc eo = (m + 1242*f4a2713aSLionel Sambuc n // expected-error {{not a constant expression}} 1243*f4a2713aSLionel Sambuc ), 1244*f4a2713aSLionel Sambuc eq = reinterpret_cast<int>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}} 1245*f4a2713aSLionel Sambuc }; 1246*f4a2713aSLionel Sambuc } 1247*f4a2713aSLionel Sambuc 1248*f4a2713aSLionel Sambuc namespace IndirectField { 1249*f4a2713aSLionel Sambuc struct S { 1250*f4a2713aSLionel Sambuc struct { // expected-warning {{GNU extension}} 1251*f4a2713aSLionel Sambuc union { // expected-warning {{declared in an anonymous struct}} 1252*f4a2713aSLionel Sambuc struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}} 1253*f4a2713aSLionel Sambuc int a; 1254*f4a2713aSLionel Sambuc int b; 1255*f4a2713aSLionel Sambuc }; 1256*f4a2713aSLionel Sambuc int c; 1257*f4a2713aSLionel Sambuc }; 1258*f4a2713aSLionel Sambuc int d; 1259*f4a2713aSLionel Sambuc }; 1260*f4a2713aSLionel Sambuc union { 1261*f4a2713aSLionel Sambuc int e; 1262*f4a2713aSLionel Sambuc int f; 1263*f4a2713aSLionel Sambuc }; 1264*f4a2713aSLionel Sambuc constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {} 1265*f4a2713aSLionel Sambuc constexpr S(int c, int d, int f) : c(c), d(d), f(f) {} 1266*f4a2713aSLionel Sambuc }; 1267*f4a2713aSLionel Sambuc 1268*f4a2713aSLionel Sambuc constexpr S s1(1, 2, 3, 4); 1269*f4a2713aSLionel Sambuc constexpr S s2(5, 6, 7); 1270*f4a2713aSLionel Sambuc 1271*f4a2713aSLionel Sambuc // FIXME: The diagnostics here do a very poor job of explaining which unnamed 1272*f4a2713aSLionel Sambuc // member is active and which is requested. 1273*f4a2713aSLionel Sambuc static_assert(s1.a == 1, ""); 1274*f4a2713aSLionel Sambuc static_assert(s1.b == 2, ""); 1275*f4a2713aSLionel Sambuc static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1276*f4a2713aSLionel Sambuc static_assert(s1.d == 3, ""); 1277*f4a2713aSLionel Sambuc static_assert(s1.e == 4, ""); 1278*f4a2713aSLionel Sambuc static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1279*f4a2713aSLionel Sambuc 1280*f4a2713aSLionel Sambuc static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1281*f4a2713aSLionel Sambuc static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1282*f4a2713aSLionel Sambuc static_assert(s2.c == 5, ""); 1283*f4a2713aSLionel Sambuc static_assert(s2.d == 6, ""); 1284*f4a2713aSLionel Sambuc static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1285*f4a2713aSLionel Sambuc static_assert(s2.f == 7, ""); 1286*f4a2713aSLionel Sambuc } 1287*f4a2713aSLionel Sambuc 1288*f4a2713aSLionel Sambuc // DR1405: don't allow reading mutable members in constant expressions. 1289*f4a2713aSLionel Sambuc namespace MutableMembers { 1290*f4a2713aSLionel Sambuc struct MM { 1291*f4a2713aSLionel Sambuc mutable int n; // expected-note 3{{declared here}} 1292*f4a2713aSLionel Sambuc } constexpr mm = { 4 }; 1293*f4a2713aSLionel Sambuc constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} 1294*f4a2713aSLionel Sambuc int x = (mm.n = 1, 3); 1295*f4a2713aSLionel Sambuc constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} 1296*f4a2713aSLionel Sambuc 1297*f4a2713aSLionel Sambuc // Here's one reason why allowing this would be a disaster... 1298*f4a2713aSLionel Sambuc template<int n> struct Id { int k = n; }; 1299*f4a2713aSLionel Sambuc int f() { 1300*f4a2713aSLionel Sambuc constexpr MM m = { 0 }; 1301*f4a2713aSLionel Sambuc ++m.n; 1302*f4a2713aSLionel Sambuc return Id<m.n>().k; // expected-error {{not a constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} 1303*f4a2713aSLionel Sambuc } 1304*f4a2713aSLionel Sambuc 1305*f4a2713aSLionel Sambuc struct A { int n; }; 1306*f4a2713aSLionel Sambuc struct B { mutable A a; }; // expected-note {{here}} 1307*f4a2713aSLionel Sambuc struct C { B b; }; 1308*f4a2713aSLionel Sambuc constexpr C c[3] = {}; 1309*f4a2713aSLionel Sambuc constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}} 1310*f4a2713aSLionel Sambuc } 1311*f4a2713aSLionel Sambuc 1312*f4a2713aSLionel Sambuc namespace Fold { 1313*f4a2713aSLionel Sambuc 1314*f4a2713aSLionel Sambuc // This macro forces its argument to be constant-folded, even if it's not 1315*f4a2713aSLionel Sambuc // otherwise a constant expression. 1316*f4a2713aSLionel Sambuc #define fold(x) (__builtin_constant_p(x) ? (x) : (x)) 1317*f4a2713aSLionel Sambuc 1318*f4a2713aSLionel Sambuc constexpr int n = (int)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}} 1319*f4a2713aSLionel Sambuc constexpr int m = fold((int)(char*)123); // ok 1320*f4a2713aSLionel Sambuc static_assert(m == 123, ""); 1321*f4a2713aSLionel Sambuc 1322*f4a2713aSLionel Sambuc #undef fold 1323*f4a2713aSLionel Sambuc 1324*f4a2713aSLionel Sambuc } 1325*f4a2713aSLionel Sambuc 1326*f4a2713aSLionel Sambuc namespace DR1454 { 1327*f4a2713aSLionel Sambuc 1328*f4a2713aSLionel Sambuc constexpr const int &f(const int &n) { return n; } 1329*f4a2713aSLionel Sambuc constexpr int k1 = f(0); // ok 1330*f4a2713aSLionel Sambuc 1331*f4a2713aSLionel Sambuc struct Wrap { 1332*f4a2713aSLionel Sambuc const int &value; 1333*f4a2713aSLionel Sambuc }; 1334*f4a2713aSLionel Sambuc constexpr const Wrap &g(const Wrap &w) { return w; } 1335*f4a2713aSLionel Sambuc constexpr int k2 = g({0}).value; // ok 1336*f4a2713aSLionel Sambuc 1337*f4a2713aSLionel Sambuc // The temporary here has static storage duration, so we can bind a constexpr 1338*f4a2713aSLionel Sambuc // reference to it. 1339*f4a2713aSLionel Sambuc constexpr const int &i = 1; 1340*f4a2713aSLionel Sambuc constexpr const int j = i; 1341*f4a2713aSLionel Sambuc static_assert(j == 1, ""); 1342*f4a2713aSLionel Sambuc 1343*f4a2713aSLionel Sambuc // The temporary here is not const, so it can't be read outside the expression 1344*f4a2713aSLionel Sambuc // in which it was created (per the C++14 rules, which we use to avoid a C++11 1345*f4a2713aSLionel Sambuc // defect). 1346*f4a2713aSLionel Sambuc constexpr int &&k = 1; // expected-note {{temporary created here}} 1347*f4a2713aSLionel Sambuc constexpr const int l = k; // expected-error {{constant expression}} expected-note {{read of temporary}} 1348*f4a2713aSLionel Sambuc 1349*f4a2713aSLionel Sambuc void f() { 1350*f4a2713aSLionel Sambuc // The temporary here has automatic storage duration, so we can't bind a 1351*f4a2713aSLionel Sambuc // constexpr reference to it. 1352*f4a2713aSLionel Sambuc constexpr const int &i = 1; // expected-error {{constant expression}} expected-note 2{{temporary}} 1353*f4a2713aSLionel Sambuc } 1354*f4a2713aSLionel Sambuc 1355*f4a2713aSLionel Sambuc } 1356*f4a2713aSLionel Sambuc 1357*f4a2713aSLionel Sambuc namespace RecursiveOpaqueExpr { 1358*f4a2713aSLionel Sambuc template<typename Iter> 1359*f4a2713aSLionel Sambuc constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) { 1360*f4a2713aSLionel Sambuc return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}} 1361*f4a2713aSLionel Sambuc } 1362*f4a2713aSLionel Sambuc 1363*f4a2713aSLionel Sambuc constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 }; 1364*f4a2713aSLionel Sambuc static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, ""); 1365*f4a2713aSLionel Sambuc 1366*f4a2713aSLionel Sambuc constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 }; 1367*f4a2713aSLionel Sambuc static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, ""); 1368*f4a2713aSLionel Sambuc 1369*f4a2713aSLionel Sambuc constexpr int arr3[] = { 1370*f4a2713aSLionel Sambuc 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1371*f4a2713aSLionel Sambuc 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1372*f4a2713aSLionel Sambuc 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1373*f4a2713aSLionel Sambuc 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1374*f4a2713aSLionel Sambuc 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1375*f4a2713aSLionel Sambuc 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1376*f4a2713aSLionel Sambuc 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1377*f4a2713aSLionel Sambuc 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 1378*f4a2713aSLionel Sambuc static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, ""); 1379*f4a2713aSLionel Sambuc } 1380*f4a2713aSLionel Sambuc 1381*f4a2713aSLionel Sambuc namespace VLASizeof { 1382*f4a2713aSLionel Sambuc 1383*f4a2713aSLionel Sambuc void f(int k) { 1384*f4a2713aSLionel Sambuc int arr[k]; // expected-warning {{C99}} 1385*f4a2713aSLionel Sambuc constexpr int n = 1 + 1386*f4a2713aSLionel Sambuc sizeof(arr) // expected-error {{constant expression}} 1387*f4a2713aSLionel Sambuc * 3; 1388*f4a2713aSLionel Sambuc } 1389*f4a2713aSLionel Sambuc } 1390*f4a2713aSLionel Sambuc 1391*f4a2713aSLionel Sambuc namespace CompoundLiteral { 1392*f4a2713aSLionel Sambuc // FIXME: 1393*f4a2713aSLionel Sambuc // We don't model the semantics of this correctly: the compound literal is 1394*f4a2713aSLionel Sambuc // represented as a prvalue in the AST, but actually behaves like an lvalue. 1395*f4a2713aSLionel Sambuc // We treat the compound literal as a temporary and refuse to produce a 1396*f4a2713aSLionel Sambuc // pointer to it. This is OK: we're not required to treat this as a constant 1397*f4a2713aSLionel Sambuc // in C++, and in C we model compound literals as lvalues. 1398*f4a2713aSLionel Sambuc constexpr int *p = (int*)(int[1]){0}; // expected-warning {{C99}} expected-error {{constant expression}} expected-note 2{{temporary}} 1399*f4a2713aSLionel Sambuc } 1400*f4a2713aSLionel Sambuc 1401*f4a2713aSLionel Sambuc namespace Vector { 1402*f4a2713aSLionel Sambuc typedef int __attribute__((vector_size(16))) VI4; 1403*f4a2713aSLionel Sambuc constexpr VI4 f(int n) { 1404*f4a2713aSLionel Sambuc return VI4 { n * 3, n + 4, n - 5, n / 6 }; 1405*f4a2713aSLionel Sambuc } 1406*f4a2713aSLionel Sambuc constexpr auto v1 = f(10); 1407*f4a2713aSLionel Sambuc 1408*f4a2713aSLionel Sambuc typedef double __attribute__((vector_size(32))) VD4; 1409*f4a2713aSLionel Sambuc constexpr VD4 g(int n) { 1410*f4a2713aSLionel Sambuc return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}} 1411*f4a2713aSLionel Sambuc } 1412*f4a2713aSLionel Sambuc constexpr auto v2 = g(4); 1413*f4a2713aSLionel Sambuc } 1414*f4a2713aSLionel Sambuc 1415*f4a2713aSLionel Sambuc // PR12626, redux 1416*f4a2713aSLionel Sambuc namespace InvalidClasses { 1417*f4a2713aSLionel Sambuc void test0() { 1418*f4a2713aSLionel Sambuc struct X; // expected-note {{forward declaration}} 1419*f4a2713aSLionel Sambuc struct Y { bool b; X x; }; // expected-error {{field has incomplete type}} 1420*f4a2713aSLionel Sambuc Y y; 1421*f4a2713aSLionel Sambuc auto& b = y.b; 1422*f4a2713aSLionel Sambuc } 1423*f4a2713aSLionel Sambuc } 1424*f4a2713aSLionel Sambuc 1425*f4a2713aSLionel Sambuc namespace NamespaceAlias { 1426*f4a2713aSLionel Sambuc constexpr int f() { 1427*f4a2713aSLionel Sambuc namespace NS = NamespaceAlias; // expected-warning {{use of this statement in a constexpr function is a C++1y extension}} 1428*f4a2713aSLionel Sambuc return &NS::f != nullptr; 1429*f4a2713aSLionel Sambuc } 1430*f4a2713aSLionel Sambuc } 1431*f4a2713aSLionel Sambuc 1432*f4a2713aSLionel Sambuc // Constructors can be implicitly constexpr, even for a non-literal type. 1433*f4a2713aSLionel Sambuc namespace ImplicitConstexpr { 1434*f4a2713aSLionel Sambuc struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}} 1435*f4a2713aSLionel Sambuc struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; }; 1436*f4a2713aSLionel Sambuc struct S { R r; }; // expected-note 3{{here}} 1437*f4a2713aSLionel Sambuc struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; }; 1438*f4a2713aSLionel Sambuc struct U { T t; }; // expected-note 3{{here}} 1439*f4a2713aSLionel Sambuc static_assert(!__is_literal_type(Q), ""); 1440*f4a2713aSLionel Sambuc static_assert(!__is_literal_type(R), ""); 1441*f4a2713aSLionel Sambuc static_assert(!__is_literal_type(S), ""); 1442*f4a2713aSLionel Sambuc static_assert(!__is_literal_type(T), ""); 1443*f4a2713aSLionel Sambuc static_assert(!__is_literal_type(U), ""); 1444*f4a2713aSLionel Sambuc struct Test { 1445*f4a2713aSLionel Sambuc friend Q::Q() noexcept; // expected-error {{follows constexpr}} 1446*f4a2713aSLionel Sambuc friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}} 1447*f4a2713aSLionel Sambuc friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}} 1448*f4a2713aSLionel Sambuc friend S::S() noexcept; // expected-error {{follows constexpr}} 1449*f4a2713aSLionel Sambuc friend S::S(S&&) noexcept; // expected-error {{follows constexpr}} 1450*f4a2713aSLionel Sambuc friend S::S(const S&) noexcept; // expected-error {{follows constexpr}} 1451*f4a2713aSLionel Sambuc friend constexpr U::U() noexcept; // expected-error {{follows non-constexpr}} 1452*f4a2713aSLionel Sambuc friend constexpr U::U(U&&) noexcept; // expected-error {{follows non-constexpr}} 1453*f4a2713aSLionel Sambuc friend constexpr U::U(const U&) noexcept; // expected-error {{follows non-constexpr}} 1454*f4a2713aSLionel Sambuc }; 1455*f4a2713aSLionel Sambuc } 1456*f4a2713aSLionel Sambuc 1457*f4a2713aSLionel Sambuc // Indirectly test that an implicit lvalue to xvalue conversion performed for 1458*f4a2713aSLionel Sambuc // an NRVO move operation isn't implemented as CK_LValueToRValue. 1459*f4a2713aSLionel Sambuc namespace PR12826 { 1460*f4a2713aSLionel Sambuc struct Foo {}; 1461*f4a2713aSLionel Sambuc constexpr Foo id(Foo x) { return x; } 1462*f4a2713aSLionel Sambuc constexpr Foo res(id(Foo())); 1463*f4a2713aSLionel Sambuc } 1464*f4a2713aSLionel Sambuc 1465*f4a2713aSLionel Sambuc namespace PR13273 { 1466*f4a2713aSLionel Sambuc struct U { 1467*f4a2713aSLionel Sambuc int t; 1468*f4a2713aSLionel Sambuc U() = default; 1469*f4a2713aSLionel Sambuc }; 1470*f4a2713aSLionel Sambuc 1471*f4a2713aSLionel Sambuc struct S : U { 1472*f4a2713aSLionel Sambuc S() = default; 1473*f4a2713aSLionel Sambuc }; 1474*f4a2713aSLionel Sambuc 1475*f4a2713aSLionel Sambuc // S's default constructor isn't constexpr, because U's default constructor 1476*f4a2713aSLionel Sambuc // doesn't initialize 't', but it's trivial, so value-initialization doesn't 1477*f4a2713aSLionel Sambuc // actually call it. 1478*f4a2713aSLionel Sambuc static_assert(S{}.t == 0, ""); 1479*f4a2713aSLionel Sambuc } 1480*f4a2713aSLionel Sambuc 1481*f4a2713aSLionel Sambuc namespace PR12670 { 1482*f4a2713aSLionel Sambuc struct S { 1483*f4a2713aSLionel Sambuc constexpr S(int a0) : m(a0) {} 1484*f4a2713aSLionel Sambuc constexpr S() : m(6) {} 1485*f4a2713aSLionel Sambuc int m; 1486*f4a2713aSLionel Sambuc }; 1487*f4a2713aSLionel Sambuc constexpr S x[3] = { {4}, 5 }; 1488*f4a2713aSLionel Sambuc static_assert(x[0].m == 4, ""); 1489*f4a2713aSLionel Sambuc static_assert(x[1].m == 5, ""); 1490*f4a2713aSLionel Sambuc static_assert(x[2].m == 6, ""); 1491*f4a2713aSLionel Sambuc } 1492*f4a2713aSLionel Sambuc 1493*f4a2713aSLionel Sambuc // Indirectly test that an implicit lvalue-to-rvalue conversion is performed 1494*f4a2713aSLionel Sambuc // when a conditional operator has one argument of type void and where the other 1495*f4a2713aSLionel Sambuc // is a glvalue of class type. 1496*f4a2713aSLionel Sambuc namespace ConditionalLValToRVal { 1497*f4a2713aSLionel Sambuc struct A { 1498*f4a2713aSLionel Sambuc constexpr A(int a) : v(a) {} 1499*f4a2713aSLionel Sambuc int v; 1500*f4a2713aSLionel Sambuc }; 1501*f4a2713aSLionel Sambuc 1502*f4a2713aSLionel Sambuc constexpr A f(const A &a) { 1503*f4a2713aSLionel Sambuc return a.v == 0 ? throw a : a; 1504*f4a2713aSLionel Sambuc } 1505*f4a2713aSLionel Sambuc 1506*f4a2713aSLionel Sambuc constexpr A a(4); 1507*f4a2713aSLionel Sambuc static_assert(f(a).v == 4, ""); 1508*f4a2713aSLionel Sambuc } 1509*f4a2713aSLionel Sambuc 1510*f4a2713aSLionel Sambuc namespace TLS { 1511*f4a2713aSLionel Sambuc __thread int n; 1512*f4a2713aSLionel Sambuc int m; 1513*f4a2713aSLionel Sambuc 1514*f4a2713aSLionel Sambuc constexpr bool b = &n == &n; 1515*f4a2713aSLionel Sambuc 1516*f4a2713aSLionel Sambuc constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}} 1517*f4a2713aSLionel Sambuc 1518*f4a2713aSLionel Sambuc constexpr int *f() { return &n; } 1519*f4a2713aSLionel Sambuc constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}} 1520*f4a2713aSLionel Sambuc constexpr bool c = f() == f(); 1521*f4a2713aSLionel Sambuc 1522*f4a2713aSLionel Sambuc constexpr int *g() { return &m; } 1523*f4a2713aSLionel Sambuc constexpr int *r = g(); 1524*f4a2713aSLionel Sambuc } 1525*f4a2713aSLionel Sambuc 1526*f4a2713aSLionel Sambuc namespace Void { 1527*f4a2713aSLionel Sambuc constexpr void f() { return; } // expected-error{{constexpr function's return type 'void' is not a literal type}} 1528*f4a2713aSLionel Sambuc 1529*f4a2713aSLionel Sambuc void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}} 1530*f4a2713aSLionel Sambuc #define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__)) 1531*f4a2713aSLionel Sambuc template<typename T, size_t S> 1532*f4a2713aSLionel Sambuc constexpr T get(T (&a)[S], size_t k) { 1533*f4a2713aSLionel Sambuc return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}} 1534*f4a2713aSLionel Sambuc } 1535*f4a2713aSLionel Sambuc #undef ASSERT 1536*f4a2713aSLionel Sambuc template int get(int (&a)[4], size_t); 1537*f4a2713aSLionel Sambuc constexpr int arr[] = { 4, 1, 2, 3, 4 }; 1538*f4a2713aSLionel Sambuc static_assert(get(arr, 1) == 1, ""); 1539*f4a2713aSLionel Sambuc static_assert(get(arr, 4) == 4, ""); 1540*f4a2713aSLionel Sambuc static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \ 1541*f4a2713aSLionel Sambuc // expected-note{{in call to 'get(arr, 0)'}} 1542*f4a2713aSLionel Sambuc } 1543*f4a2713aSLionel Sambuc 1544*f4a2713aSLionel Sambuc namespace std { struct type_info; } 1545*f4a2713aSLionel Sambuc 1546*f4a2713aSLionel Sambuc namespace TypeId { 1547*f4a2713aSLionel Sambuc struct A { virtual ~A(); }; 1548*f4a2713aSLionel Sambuc A f(); 1549*f4a2713aSLionel Sambuc A &g(); 1550*f4a2713aSLionel Sambuc constexpr auto &x = typeid(f()); 1551*f4a2713aSLionel Sambuc constexpr auto &y = typeid(g()); // expected-error{{constant expression}} \ 1552*f4a2713aSLionel Sambuc // expected-note{{typeid applied to expression of polymorphic type 'TypeId::A' is not allowed in a constant expression}} 1553*f4a2713aSLionel Sambuc } 1554*f4a2713aSLionel Sambuc 1555*f4a2713aSLionel Sambuc namespace PR14203 { 1556*f4a2713aSLionel Sambuc struct duration { 1557*f4a2713aSLionel Sambuc constexpr duration() {} 1558*f4a2713aSLionel Sambuc constexpr operator int() const { return 0; } 1559*f4a2713aSLionel Sambuc }; 1560*f4a2713aSLionel Sambuc template<typename T> void f() { 1561*f4a2713aSLionel Sambuc // If we want to evaluate this at the point of the template definition, we 1562*f4a2713aSLionel Sambuc // need to trigger the implicit definition of the move constructor at that 1563*f4a2713aSLionel Sambuc // point. 1564*f4a2713aSLionel Sambuc // FIXME: C++ does not permit us to implicitly define it at the appropriate 1565*f4a2713aSLionel Sambuc // times, since it is only allowed to be implicitly defined when it is 1566*f4a2713aSLionel Sambuc // odr-used. 1567*f4a2713aSLionel Sambuc constexpr duration d = duration(); 1568*f4a2713aSLionel Sambuc } 1569*f4a2713aSLionel Sambuc // FIXME: It's unclear whether this is valid. On the one hand, we're not 1570*f4a2713aSLionel Sambuc // allowed to generate a move constructor. On the other hand, if we did, 1571*f4a2713aSLionel Sambuc // this would be a constant expression. For now, we generate a move 1572*f4a2713aSLionel Sambuc // constructor here. 1573*f4a2713aSLionel Sambuc int n = sizeof(short{duration(duration())}); 1574*f4a2713aSLionel Sambuc } 1575*f4a2713aSLionel Sambuc 1576*f4a2713aSLionel Sambuc namespace ArrayEltInit { 1577*f4a2713aSLionel Sambuc struct A { 1578*f4a2713aSLionel Sambuc constexpr A() : p(&p) {} 1579*f4a2713aSLionel Sambuc void *p; 1580*f4a2713aSLionel Sambuc }; 1581*f4a2713aSLionel Sambuc constexpr A a[10]; 1582*f4a2713aSLionel Sambuc static_assert(a[0].p == &a[0].p, ""); 1583*f4a2713aSLionel Sambuc static_assert(a[9].p == &a[9].p, ""); 1584*f4a2713aSLionel Sambuc static_assert(a[0].p != &a[9].p, ""); 1585*f4a2713aSLionel Sambuc static_assert(a[9].p != &a[0].p, ""); 1586*f4a2713aSLionel Sambuc 1587*f4a2713aSLionel Sambuc constexpr A b[10] = {}; 1588*f4a2713aSLionel Sambuc static_assert(b[0].p == &b[0].p, ""); 1589*f4a2713aSLionel Sambuc static_assert(b[9].p == &b[9].p, ""); 1590*f4a2713aSLionel Sambuc static_assert(b[0].p != &b[9].p, ""); 1591*f4a2713aSLionel Sambuc static_assert(b[9].p != &b[0].p, ""); 1592*f4a2713aSLionel Sambuc } 1593*f4a2713aSLionel Sambuc 1594*f4a2713aSLionel Sambuc namespace PR15884 { 1595*f4a2713aSLionel Sambuc struct S {}; 1596*f4a2713aSLionel Sambuc constexpr S f() { return {}; } 1597*f4a2713aSLionel Sambuc constexpr S *p = &f(); 1598*f4a2713aSLionel Sambuc // expected-error@-1 {{taking the address of a temporary}} 1599*f4a2713aSLionel Sambuc // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}} 1600*f4a2713aSLionel Sambuc // expected-note@-3 {{pointer to temporary is not a constant expression}} 1601*f4a2713aSLionel Sambuc // expected-note@-4 {{temporary created here}} 1602*f4a2713aSLionel Sambuc } 1603*f4a2713aSLionel Sambuc 1604*f4a2713aSLionel Sambuc namespace AfterError { 1605*f4a2713aSLionel Sambuc // FIXME: Suppress the 'no return statements' diagnostic if the body is invalid. 1606*f4a2713aSLionel Sambuc constexpr int error() { // expected-error {{no return statement}} 1607*f4a2713aSLionel Sambuc return foobar; // expected-error {{undeclared identifier}} 1608*f4a2713aSLionel Sambuc } 1609*f4a2713aSLionel Sambuc constexpr int k = error(); // expected-error {{must be initialized by a constant expression}} 1610*f4a2713aSLionel Sambuc } 1611*f4a2713aSLionel Sambuc 1612*f4a2713aSLionel Sambuc namespace std { 1613*f4a2713aSLionel Sambuc typedef decltype(sizeof(int)) size_t; 1614*f4a2713aSLionel Sambuc 1615*f4a2713aSLionel Sambuc template <class _E> 1616*f4a2713aSLionel Sambuc class initializer_list 1617*f4a2713aSLionel Sambuc { 1618*f4a2713aSLionel Sambuc const _E* __begin_; 1619*f4a2713aSLionel Sambuc size_t __size_; 1620*f4a2713aSLionel Sambuc 1621*f4a2713aSLionel Sambuc constexpr initializer_list(const _E* __b, size_t __s) 1622*f4a2713aSLionel Sambuc : __begin_(__b), 1623*f4a2713aSLionel Sambuc __size_(__s) 1624*f4a2713aSLionel Sambuc {} 1625*f4a2713aSLionel Sambuc 1626*f4a2713aSLionel Sambuc public: 1627*f4a2713aSLionel Sambuc typedef _E value_type; 1628*f4a2713aSLionel Sambuc typedef const _E& reference; 1629*f4a2713aSLionel Sambuc typedef const _E& const_reference; 1630*f4a2713aSLionel Sambuc typedef size_t size_type; 1631*f4a2713aSLionel Sambuc 1632*f4a2713aSLionel Sambuc typedef const _E* iterator; 1633*f4a2713aSLionel Sambuc typedef const _E* const_iterator; 1634*f4a2713aSLionel Sambuc 1635*f4a2713aSLionel Sambuc constexpr initializer_list() : __begin_(nullptr), __size_(0) {} 1636*f4a2713aSLionel Sambuc 1637*f4a2713aSLionel Sambuc constexpr size_t size() const {return __size_;} 1638*f4a2713aSLionel Sambuc constexpr const _E* begin() const {return __begin_;} 1639*f4a2713aSLionel Sambuc constexpr const _E* end() const {return __begin_ + __size_;} 1640*f4a2713aSLionel Sambuc }; 1641*f4a2713aSLionel Sambuc } 1642*f4a2713aSLionel Sambuc 1643*f4a2713aSLionel Sambuc namespace InitializerList { 1644*f4a2713aSLionel Sambuc constexpr int sum(const int *b, const int *e) { 1645*f4a2713aSLionel Sambuc return b != e ? *b + sum(b+1, e) : 0; 1646*f4a2713aSLionel Sambuc } 1647*f4a2713aSLionel Sambuc constexpr int sum(std::initializer_list<int> ints) { 1648*f4a2713aSLionel Sambuc return sum(ints.begin(), ints.end()); 1649*f4a2713aSLionel Sambuc } 1650*f4a2713aSLionel Sambuc static_assert(sum({1, 2, 3, 4, 5}) == 15, ""); 1651*f4a2713aSLionel Sambuc } 1652*f4a2713aSLionel Sambuc 1653*f4a2713aSLionel Sambuc namespace StmtExpr { 1654*f4a2713aSLionel Sambuc struct A { int k; }; 1655*f4a2713aSLionel Sambuc void f() { 1656*f4a2713aSLionel Sambuc static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}} 1657*f4a2713aSLionel Sambuc constexpr auto a = ({ A(); }); // expected-warning {{extension}} 1658*f4a2713aSLionel Sambuc } 1659*f4a2713aSLionel Sambuc constexpr int g(int k) { 1660*f4a2713aSLionel Sambuc return ({ // expected-warning {{extension}} 1661*f4a2713aSLionel Sambuc const int x = k; 1662*f4a2713aSLionel Sambuc x * x; 1663*f4a2713aSLionel Sambuc }); 1664*f4a2713aSLionel Sambuc } 1665*f4a2713aSLionel Sambuc static_assert(g(123) == 15129, ""); 1666*f4a2713aSLionel Sambuc constexpr int h() { // expected-error {{never produces a constant}} 1667*f4a2713aSLionel Sambuc return ({ // expected-warning {{extension}} 1668*f4a2713aSLionel Sambuc return 0; // expected-note {{not supported}} 1669*f4a2713aSLionel Sambuc 1; 1670*f4a2713aSLionel Sambuc }); 1671*f4a2713aSLionel Sambuc } 1672*f4a2713aSLionel Sambuc } 1673*f4a2713aSLionel Sambuc 1674*f4a2713aSLionel Sambuc namespace VirtualFromBase { 1675*f4a2713aSLionel Sambuc struct S1 { 1676*f4a2713aSLionel Sambuc virtual int f() const; 1677*f4a2713aSLionel Sambuc }; 1678*f4a2713aSLionel Sambuc struct S2 { 1679*f4a2713aSLionel Sambuc virtual int f(); 1680*f4a2713aSLionel Sambuc }; 1681*f4a2713aSLionel Sambuc template <typename T> struct X : T { 1682*f4a2713aSLionel Sambuc constexpr X() {} 1683*f4a2713aSLionel Sambuc double d = 0.0; 1684*f4a2713aSLionel Sambuc constexpr int f() { return sizeof(T); } // expected-warning {{will not be implicitly 'const' in C++1y}} 1685*f4a2713aSLionel Sambuc }; 1686*f4a2713aSLionel Sambuc 1687*f4a2713aSLionel Sambuc // Virtual f(), not OK. 1688*f4a2713aSLionel Sambuc constexpr X<X<S1>> xxs1; 1689*f4a2713aSLionel Sambuc constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1); 1690*f4a2713aSLionel Sambuc static_assert(p->f() == sizeof(X<S1>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}} 1691*f4a2713aSLionel Sambuc 1692*f4a2713aSLionel Sambuc // Non-virtual f(), OK. 1693*f4a2713aSLionel Sambuc constexpr X<X<S2>> xxs2; 1694*f4a2713aSLionel Sambuc constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2); 1695*f4a2713aSLionel Sambuc static_assert(q->f() == sizeof(S2), ""); 1696*f4a2713aSLionel Sambuc } 1697*f4a2713aSLionel Sambuc 1698*f4a2713aSLionel Sambuc namespace ConstexprConstructorRecovery { 1699*f4a2713aSLionel Sambuc class X { 1700*f4a2713aSLionel Sambuc public: 1701*f4a2713aSLionel Sambuc enum E : short { 1702*f4a2713aSLionel Sambuc headers = 0x1, 1703*f4a2713aSLionel Sambuc middlefile = 0x2, 1704*f4a2713aSLionel Sambuc choices = 0x4 1705*f4a2713aSLionel Sambuc }; 1706*f4a2713aSLionel Sambuc constexpr X() noexcept {}; 1707*f4a2713aSLionel Sambuc protected: 1708*f4a2713aSLionel Sambuc E val{0}; // expected-error {{cannot initialize a member subobject of type 'ConstexprConstructorRecovery::X::E' with an rvalue of type 'int'}} 1709*f4a2713aSLionel Sambuc }; 1710*f4a2713aSLionel Sambuc constexpr X x{}; 1711*f4a2713aSLionel Sambuc } 1712*f4a2713aSLionel Sambuc 1713*f4a2713aSLionel Sambuc namespace Lifetime { 1714*f4a2713aSLionel Sambuc void f() { 1715*f4a2713aSLionel Sambuc constexpr int &n = n; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}} 1716*f4a2713aSLionel Sambuc constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}} 1717*f4a2713aSLionel Sambuc } 1718*f4a2713aSLionel Sambuc 1719*f4a2713aSLionel Sambuc constexpr int &get(int &&n) { return n; } 1720*f4a2713aSLionel Sambuc struct S { 1721*f4a2713aSLionel Sambuc int &&r; // expected-note 2{{declared here}} 1722*f4a2713aSLionel Sambuc int &s; 1723*f4a2713aSLionel Sambuc int t; 1724*f4a2713aSLionel Sambuc constexpr S() : r(0), s(get(0)), t(r) {} // expected-warning {{temporary}} 1725*f4a2713aSLionel Sambuc constexpr S(int) : r(0), s(get(0)), t(s) {} // expected-warning {{temporary}} expected-note {{read of object outside its lifetime}} 1726*f4a2713aSLionel Sambuc }; 1727*f4a2713aSLionel Sambuc constexpr int k1 = S().t; // ok, int is lifetime-extended to end of constructor 1728*f4a2713aSLionel Sambuc constexpr int k2 = S(0).t; // expected-error {{constant expression}} expected-note {{in call}} 1729*f4a2713aSLionel Sambuc } 1730*f4a2713aSLionel Sambuc 1731*f4a2713aSLionel Sambuc namespace Bitfields { 1732*f4a2713aSLionel Sambuc struct A { 1733*f4a2713aSLionel Sambuc bool b : 1; 1734*f4a2713aSLionel Sambuc unsigned u : 5; 1735*f4a2713aSLionel Sambuc int n : 5; 1736*f4a2713aSLionel Sambuc bool b2 : 3; 1737*f4a2713aSLionel Sambuc unsigned u2 : 74; // expected-warning {{exceeds the size of its type}} 1738*f4a2713aSLionel Sambuc int n2 : 81; // expected-warning {{exceeds the size of its type}} 1739*f4a2713aSLionel Sambuc }; 1740*f4a2713aSLionel Sambuc 1741*f4a2713aSLionel Sambuc constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}} 1742*f4a2713aSLionel Sambuc static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 && 1743*f4a2713aSLionel Sambuc a.u2 + 1 == 0 && a.n2 == 0x7fffffff, 1744*f4a2713aSLionel Sambuc "bad truncation of bitfield values"); 1745*f4a2713aSLionel Sambuc 1746*f4a2713aSLionel Sambuc struct B { 1747*f4a2713aSLionel Sambuc int n : 3; 1748*f4a2713aSLionel Sambuc constexpr B(int k) : n(k) {} 1749*f4a2713aSLionel Sambuc }; 1750*f4a2713aSLionel Sambuc static_assert(B(3).n == 3, ""); 1751*f4a2713aSLionel Sambuc static_assert(B(4).n == -4, ""); 1752*f4a2713aSLionel Sambuc static_assert(B(7).n == -1, ""); 1753*f4a2713aSLionel Sambuc static_assert(B(8).n == 0, ""); 1754*f4a2713aSLionel Sambuc static_assert(B(-1).n == -1, ""); 1755*f4a2713aSLionel Sambuc static_assert(B(-8889).n == -1, ""); 1756*f4a2713aSLionel Sambuc 1757*f4a2713aSLionel Sambuc namespace PR16755 { 1758*f4a2713aSLionel Sambuc struct X { 1759*f4a2713aSLionel Sambuc int x : 1; 1760*f4a2713aSLionel Sambuc constexpr static int f(int x) { 1761*f4a2713aSLionel Sambuc return X{x}.x; 1762*f4a2713aSLionel Sambuc } 1763*f4a2713aSLionel Sambuc }; 1764*f4a2713aSLionel Sambuc static_assert(X::f(3) == -1, "3 should truncate to -1"); 1765*f4a2713aSLionel Sambuc } 1766*f4a2713aSLionel Sambuc } 1767*f4a2713aSLionel Sambuc 1768*f4a2713aSLionel Sambuc namespace ZeroSizeTypes { 1769*f4a2713aSLionel Sambuc constexpr int (*p1)[0] = 0, (*p2)[0] = 0; 1770*f4a2713aSLionel Sambuc constexpr int k = p2 - p1; 1771*f4a2713aSLionel Sambuc // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}} 1772*f4a2713aSLionel Sambuc // expected-note@-2 {{subtraction of pointers to type 'int [0]' of zero size}} 1773*f4a2713aSLionel Sambuc 1774*f4a2713aSLionel Sambuc int arr[5][0]; 1775*f4a2713aSLionel Sambuc constexpr int f() { // expected-error {{never produces a constant expression}} 1776*f4a2713aSLionel Sambuc return &arr[3] - &arr[0]; // expected-note {{subtraction of pointers to type 'int [0]' of zero size}} 1777*f4a2713aSLionel Sambuc } 1778*f4a2713aSLionel Sambuc } 1779*f4a2713aSLionel Sambuc 1780*f4a2713aSLionel Sambuc namespace BadDefaultInit { 1781*f4a2713aSLionel Sambuc template<int N> struct X { static const int n = N; }; 1782*f4a2713aSLionel Sambuc 1783*f4a2713aSLionel Sambuc struct A { // expected-note {{subexpression}} 1784*f4a2713aSLionel Sambuc int k = X<A().k>::n; // expected-error {{defaulted default constructor of 'A' cannot be used}} expected-error {{not a constant expression}} expected-note {{in call to 'A()'}} 1785*f4a2713aSLionel Sambuc }; 1786*f4a2713aSLionel Sambuc 1787*f4a2713aSLionel Sambuc // FIXME: The "constexpr constructor must initialize all members" diagnostic 1788*f4a2713aSLionel Sambuc // here is bogus (we discard the k(k) initializer because the parameter 'k' 1789*f4a2713aSLionel Sambuc // has been marked invalid). 1790*f4a2713aSLionel Sambuc struct B { // expected-note 2{{candidate}} 1791*f4a2713aSLionel Sambuc constexpr B( // expected-error {{must initialize all members}} expected-note {{candidate}} 1792*f4a2713aSLionel Sambuc int k = X<B().k>::n) : // expected-error {{no matching constructor}} 1793*f4a2713aSLionel Sambuc k(k) {} 1794*f4a2713aSLionel Sambuc int k; // expected-note {{not initialized}} 1795*f4a2713aSLionel Sambuc }; 1796*f4a2713aSLionel Sambuc } 1797*f4a2713aSLionel Sambuc 1798*f4a2713aSLionel Sambuc namespace NeverConstantTwoWays { 1799*f4a2713aSLionel Sambuc // If we see something non-constant but foldable followed by something 1800*f4a2713aSLionel Sambuc // non-constant and not foldable, we want the first diagnostic, not the 1801*f4a2713aSLionel Sambuc // second. 1802*f4a2713aSLionel Sambuc constexpr int f(int n) { // expected-error {{never produces a constant expression}} 1803*f4a2713aSLionel Sambuc return (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}} 1804*f4a2713aSLionel Sambuc 1 / 0 : // expected-warning {{division by zero}} 1805*f4a2713aSLionel Sambuc 0; 1806*f4a2713aSLionel Sambuc } 1807*f4a2713aSLionel Sambuc 1808*f4a2713aSLionel Sambuc // FIXME: We should diagnose the cast to long here, not the division by zero. 1809*f4a2713aSLionel Sambuc constexpr int n = // expected-error {{must be initialized by a constant expression}} 1810*f4a2713aSLionel Sambuc (int *)(long)&n == &n ? 1811*f4a2713aSLionel Sambuc 1 / 0 : // expected-warning {{division by zero}} expected-note {{division by zero}} 1812*f4a2713aSLionel Sambuc 0; 1813*f4a2713aSLionel Sambuc } 1814*f4a2713aSLionel Sambuc 1815*f4a2713aSLionel Sambuc namespace PR17800 { 1816*f4a2713aSLionel Sambuc struct A { 1817*f4a2713aSLionel Sambuc constexpr int operator()() const { return 0; } 1818*f4a2713aSLionel Sambuc }; 1819*f4a2713aSLionel Sambuc template <typename ...T> constexpr int sink(T ...) { 1820*f4a2713aSLionel Sambuc return 0; 1821*f4a2713aSLionel Sambuc } 1822*f4a2713aSLionel Sambuc template <int ...N> constexpr int run() { 1823*f4a2713aSLionel Sambuc return sink(A()() + N ...); 1824*f4a2713aSLionel Sambuc } 1825*f4a2713aSLionel Sambuc constexpr int k = run<1, 2, 3>(); 1826*f4a2713aSLionel Sambuc } 1827*f4a2713aSLionel Sambuc 1828*f4a2713aSLionel Sambuc namespace BuiltinStrlen { 1829*f4a2713aSLionel Sambuc constexpr const char *a = "foo\0quux"; 1830*f4a2713aSLionel Sambuc constexpr char b[] = "foo\0quux"; 1831*f4a2713aSLionel Sambuc constexpr int f() { return 'u'; } 1832*f4a2713aSLionel Sambuc constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 }; 1833*f4a2713aSLionel Sambuc 1834*f4a2713aSLionel Sambuc static_assert(__builtin_strlen("foo") == 3, ""); 1835*f4a2713aSLionel Sambuc static_assert(__builtin_strlen("foo\0quux") == 3, ""); 1836*f4a2713aSLionel Sambuc static_assert(__builtin_strlen("foo\0quux" + 4) == 4, ""); 1837*f4a2713aSLionel Sambuc 1838*f4a2713aSLionel Sambuc constexpr bool check(const char *p) { 1839*f4a2713aSLionel Sambuc return __builtin_strlen(p) == 3 && 1840*f4a2713aSLionel Sambuc __builtin_strlen(p + 1) == 2 && 1841*f4a2713aSLionel Sambuc __builtin_strlen(p + 2) == 1 && 1842*f4a2713aSLionel Sambuc __builtin_strlen(p + 3) == 0 && 1843*f4a2713aSLionel Sambuc __builtin_strlen(p + 4) == 4 && 1844*f4a2713aSLionel Sambuc __builtin_strlen(p + 5) == 3 && 1845*f4a2713aSLionel Sambuc __builtin_strlen(p + 6) == 2 && 1846*f4a2713aSLionel Sambuc __builtin_strlen(p + 7) == 1 && 1847*f4a2713aSLionel Sambuc __builtin_strlen(p + 8) == 0; 1848*f4a2713aSLionel Sambuc } 1849*f4a2713aSLionel Sambuc 1850*f4a2713aSLionel Sambuc static_assert(check(a), ""); 1851*f4a2713aSLionel Sambuc static_assert(check(b), ""); 1852*f4a2713aSLionel Sambuc static_assert(check(c), ""); 1853*f4a2713aSLionel Sambuc 1854*f4a2713aSLionel Sambuc constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 1855*f4a2713aSLionel Sambuc constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 1856*f4a2713aSLionel Sambuc constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 1857*f4a2713aSLionel Sambuc 1858*f4a2713aSLionel Sambuc constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 1859*f4a2713aSLionel Sambuc constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 1860*f4a2713aSLionel Sambuc constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 1861*f4a2713aSLionel Sambuc 1862*f4a2713aSLionel Sambuc // FIXME: The diagnostic here could be better. 1863*f4a2713aSLionel Sambuc constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator. 1864*f4a2713aSLionel Sambuc constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 1865*f4a2713aSLionel Sambuc } 1866