constant-expression-cxx11.cpp revision 099e7f647ccda915513f2b2ec53352dc756082d3
14fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 -pedantic %s -Wno-comment 24fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 34fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace StaticAssertFoldTest { 44fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 54fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comint x; 64fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(++x, "test"); // expected-error {{not an integral constant expression}} 74fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(false, "test"); // expected-error {{test}} 84fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 94fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 104fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 116177e6999d23a4268ffd98dedfb1da00e272a89brobertphillips@google.comtemplate<typename T> constexpr T id(const T &t) { return t; } // expected-note {{here}} 12086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com// FIXME: support templates here. 13f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.com//template<typename T> constexpr T min(const T &a, const T &b) { 144fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com// return a < b ? a : b; 15da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com//} 16da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com//template<typename T> constexpr T max(const T &a, const T &b) { 174fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com// return a < b ? b : a; 184fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com//} 194fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr int min(const int &a, const int &b) { return a < b ? a : b; } 204fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr int max(const int &a, const int &b) { return a < b ? b : a; } 214fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 224fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstruct MemberZero { 234fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int zero() { return 0; } 24086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com}; 25086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 26086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comnamespace DerivedToVBaseCast { 27086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 28086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com struct U { int n; }; 29086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com struct V : U { int n; }; 30086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com struct A : virtual V { int n; }; 31086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com struct Aa { int n; }; 3277cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.com struct B : virtual A, Aa {}; 33086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com struct C : virtual A, Aa {}; 3477cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.com struct D : B, C {}; 3577cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.com 3677cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.com D d; 37086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com constexpr B *p = &d; 38086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com constexpr C *q = &d; 39d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com static_assert((void*)p != (void*)q, ""); 40d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com static_assert((A*)p == (A*)q, ""); 41d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com static_assert((Aa*)p != (Aa*)q, ""); 42d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com 43d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com constexpr B &pp = d; 44d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com constexpr C &qq = d; 45d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com static_assert((void*)&pp != (void*)&qq, ""); 46d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com static_assert(&(A&)pp == &(A&)qq, ""); 47d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com static_assert(&(Aa&)pp != &(Aa&)qq, ""); 48d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com 49d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com constexpr V *v = p; 50086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com constexpr V *w = q; 515fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com constexpr V *x = (A*)p; 525fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com static_assert(v == w, ""); 535fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com static_assert(v == x, ""); 545fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com 555fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com static_assert((U*)&d == p, ""); 565fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com static_assert((U*)&d == q, ""); 575fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com static_assert((U*)&d == v, ""); 585fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com static_assert((U*)&d == w, ""); 594fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static_assert((U*)&d == x, ""); 604fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 614fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com struct X {}; 62168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com struct Y1 : virtual X {}; 63086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com struct Y2 : X {}; 644fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com struct Z : Y1, Y2 {}; 65d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com Z z; 665fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, ""); 67da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com 684fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 694fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 70344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.orgnamespace ConstCast { 71344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org 72f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.orgconstexpr int n1 = 0; 73a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.comconstexpr int n2 = const_cast<int&>(n1); 74a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.comconstexpr int *n3 = const_cast<int*>(&n1); 75a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.comconstexpr int n4 = *const_cast<int*>(&n1); 76344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.orgconstexpr const int * const *n5 = const_cast<const int* const*>(&n3); 775fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.comconstexpr int **n6 = const_cast<int**>(&n3); 78a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.comconstexpr int n7 = **n5; 79a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.comconstexpr int n8 = **n6; 80a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com 81a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com} 824fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 834fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace TemplateArgumentConversion { 84086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com template<int n> struct IntParam {}; 854fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 864fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com using IntParam0 = IntParam<0>; 87d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com // FIXME: This should be accepted once we implement the new ICE rules. 88b7f9a86b2d6218d5f4846e5975794b4c18619a61bsalomon@google.com using IntParam0 = IntParam<id(0)>; // expected-error {{not an integral constant expression}} 89a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}} 90f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org} 91a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com 924fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace CaseStatements { 934fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com void f(int n) { 944fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com switch (n) { 954fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com // FIXME: Produce the 'add ()' fixit for this. 964fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}} expected-note {{non-literal type '<bound member function type>'}} 974fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com // FIXME: This should be accepted once we implement the new ICE rules. 984fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com case id(1): // expected-error {{not an integer constant expression}} expected-note {{undefined function}} 994fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return; 1004fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 1014fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 1024fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 1034fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 104168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.comextern int &Recurse1; 105086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comint &Recurse2 = Recurse1; // expected-note 2{{declared here}} expected-note {{initializer of 'Recurse1' is not a constant expression}} 106f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.comint &Recurse1 = Recurse2; // expected-note {{declared here}} expected-note {{initializer of 'Recurse2' is not a constant expression}} 107d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.comconstexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}} 1085fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com 109da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comextern const int RecurseA; 110f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.orgconst int RecurseB = RecurseA; // expected-note {{declared here}} 1114fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconst int RecurseA = 10; 112086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comconstexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}} 1134fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1144fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace MemberEnum { 115d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com struct WithMemberEnum { 1165fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com enum E { A = 42 }; 117da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com } wme; 1184fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1194fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static_assert(wme.A == 42, ""); 1204fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 1214fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1224fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace DefaultArguments { 123168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com 124086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comconst int z = int(); 1254fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) { 126d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com return a + b + *c + d; 1275fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com} 128da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comconst int four = 4; 129f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.orgconstexpr int eight = 8; 1304fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr const int twentyseven = 27; 131086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comstatic_assert(Sum() == 0, ""); 1324fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(Sum(1) == 1, ""); 1334fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(Sum(1, four) == 5, ""); 134d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.comstatic_assert(Sum(1, eight, &twentyseven) == 36, ""); 1355fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.comstatic_assert(Sum(1, 2, &four, eight) == 15, ""); 136da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com 1374fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 1384fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1394fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace Ellipsis { 1404fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1414fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com// Note, values passed through an ellipsis can't actually be used. 142168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.comconstexpr int F(int a, ...) { return a; } 143086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comstatic_assert(F(0) == 0, ""); 144f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.comstatic_assert(F(1, 0) == 1, ""); 1454fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(F(2, "test") == 2, ""); 146d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.comstatic_assert(F(3, &F) == 3, ""); 1475fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.comint k = 0; 148da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comstatic_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 149f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org 1504fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 151086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 1524fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace Recursion { 1534fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; } 154d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com static_assert(fib(11) == 89, ""); 1555fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com 156da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com constexpr int gcd_inner(int a, int b) { 1574fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return b == 0 ? a : gcd_inner(b, a % b); 1584fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 1594fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int gcd(int a, int b) { 1604fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return gcd_inner(max(a, b), min(a, b)); 1614fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 162168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com 163086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com static_assert(gcd(1749237, 5628959) == 7, ""); 1644fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 1654fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 166d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.comnamespace FunctionCast { 1675fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com // When folding, we allow functions to be cast to different types. Such 168da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com // cast functions cannot be called, even if they're constexpr. 169f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org constexpr int f() { return 1; } 1704fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com typedef double (*DoubleFn)(); 171086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com typedef int (*IntFn)(); 1724fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{extension}} 1734fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com int b[(int)IntFn(f)()]; // ok 174d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com} 1755fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com 176da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comnamespace StaticMemberFunction { 1774fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com struct S { 1784fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static constexpr int k = 42; 1794fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static constexpr int f(int n) { return n * k + 2; } 1804fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } s; 1814fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1824fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int n = s.f(19); 1834fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static_assert(S::f(19) == 800, ""); 1844fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static_assert(s.f(19) == 800, ""); 1854fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static_assert(n == 800, ""); 1864fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1874fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int (*sf1)(int) = &S::f; 1884fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int (*sf2)(int) = &s.f; 1894fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr const int *sk = &s.k; 1904fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 1914fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1924fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace ParameterScopes { 1934fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 1944fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com const int k = 42; 1954fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr const int &ObscureTheTruth(const int &a) { return a; } // expected-note 3{{reference to 'a' cannot be returned from a constexpr function}} 1964fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}} 1974fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return ObscureTheTruth(b ? a : k); // expected-note 2{{in call to 'ObscureTheTruth(a)'}} 1984fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 1994fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok 2004fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnJunk(1, 0)'}} 2014fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 2024fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}} 2034fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com // If ObscureTheTruth returns a reference to 'a', the result is not a 2044fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com // constant expression even though 'a' is still in scope. 2054fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}} 2064fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 207f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.com static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok 2084fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnNonstaticRef(1, 0)'}} 2094fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 2104fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int InternalReturnJunk(int n) { 211f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.com // FIXME: We should reject this: it never produces a constant expression. 2124fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}} 213032b221dadb6eb8283ac2d1bc8913ee7bb5cfe7absalomon@google.com } 2144fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}} 2154fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 2164fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int LToR(int &n) { return n; } 217f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.com constexpr int GrabCallersArgument(bool which, int a, int b) { 2184fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return LToR(which ? b : a); 2194fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 220032b221dadb6eb8283ac2d1bc8913ee7bb5cfe7absalomon@google.com static_assert(GrabCallersArgument(false, 1, 2) == 1, ""); 221032b221dadb6eb8283ac2d1bc8913ee7bb5cfe7absalomon@google.com static_assert(GrabCallersArgument(true, 4, 8) == 8, ""); 222032b221dadb6eb8283ac2d1bc8913ee7bb5cfe7absalomon@google.com 223032b221dadb6eb8283ac2d1bc8913ee7bb5cfe7absalomon@google.com} 224032b221dadb6eb8283ac2d1bc8913ee7bb5cfe7absalomon@google.com 2254fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace Pointers { 2264fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 227168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com constexpr int f(int n, const int *a, const int *b, const int *c) { 2284fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return n == 0 ? 0 : *a + f(n-1, b, c, a); 2294fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 2304fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 231168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com const int x = 1, y = 10, z = 100; 2324fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com static_assert(f(23, &x, &y, &z) == 788, ""); 233086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 234086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com constexpr int g(int n, int a, int b, int c) { 235086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com return f(n, &a, &b, &c); 2364fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com } 237d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com static_assert(g(23, x, y, z) == 788, ""); 2384fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 239d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com} 240d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com 2414fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace FunctionPointers { 242d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com 2434fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int Double(int n) { return 2 * n; } 244d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com constexpr int Triple(int n) { return 3 * n; } 2454fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int Twice(int (*F)(int), int n) { return F(F(n)); } 2464fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com constexpr int Quadruple(int n) { return Twice(Double, n); } 2475fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com constexpr auto Select(int n) -> int (*)(int) { 2485fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0; 2495fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com } 2505fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}} 2515fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com 2525fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); 2535fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com 2545fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}} 2555fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com 2565fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com} 2574fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 2584fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comnamespace PointerComparison { 2596177e6999d23a4268ffd98dedfb1da00e272a89brobertphillips@google.com 2605fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.comint x, y; 2615fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.comstatic_assert(&x == &y, "false"); // expected-error {{false}} 2625fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.comstatic_assert(&x != &y, ""); 2635fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.comconstexpr bool g1 = &x == &y; 2645fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.comconstexpr bool g2 = &x != &y; 265086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comconstexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} 26696399948dc2f1afd2e9d9e5be7d646c01d17f82bbsalomon@google.comconstexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} 2676177e6999d23a4268ffd98dedfb1da00e272a89brobertphillips@google.comconstexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} 268086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comconstexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} 269086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 2709e90aed5de82732cc9921f01388d3063a41a053bcommit-bot@chromium.orgstruct S { int x, y; } s; 271168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.comstatic_assert(&s.x == &s.y, "false"); // expected-error {{false}} 2724fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(&s.x != &s.y, ""); 2734fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(&s.x <= &s.y, ""); 274fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.comstatic_assert(&s.x >= &s.y, "false"); // expected-error {{false}} 275018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.comstatic_assert(&s.x < &s.y, ""); 2764fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(&s.x > &s.y, "false"); // expected-error {{false}} 2774fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 278f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.orgstatic_assert(0 == &y, "false"); // expected-error {{false}} 279fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.comstatic_assert(0 != &y, ""); 280018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.comconstexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}} 2814fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}} 2824fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}} 2834fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}} 2844fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 2854fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comstatic_assert(&x == 0, "false"); // expected-error {{false}} 286018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.comstatic_assert(&x != 0, ""); 2874fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}} 2884fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}} 2894fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}} 2904fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}} 291f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.com 292da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comstatic_assert(&x == &x, ""); 293da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comstatic_assert(&x != &x, "false"); // expected-error {{false}} 294da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comstatic_assert(&x <= &x, ""); 295da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comstatic_assert(&x >= &x, ""); 296da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comstatic_assert(&x < &x, "false"); // expected-error {{false}} 297da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comstatic_assert(&x > &x, "false"); // expected-error {{false}} 298f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.com 299da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comconstexpr S* sptr = &s; 300da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comconstexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}} 301da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com 302da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comstruct Str { 303da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com // FIXME: In C++ mode, we should say 'integral' not 'integer' 304da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \ 3059e90aed5de82732cc9921f01388d3063a41a053bcommit-bot@chromium.org expected-warning {{not integer constant expression}} \ 306344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-note {{dynamic_cast is not allowed in a constant expression}} 3079e90aed5de82732cc9921f01388d3063a41a053bcommit-bot@chromium.org int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \ 308344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-warning {{not integer constant expression}} \ 309344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-note {{reinterpret_cast is not allowed in a constant expression}} 310344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \ 311344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-warning {{not integer constant expression}} \ 312344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 313344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org int d : (S*)(42) == (S*)(42); // \ 314344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-warning {{not integer constant expression}} \ 315344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 316344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org int e : (Str*)(sptr) == (Str*)(sptr); // \ 317344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-warning {{not integer constant expression}} \ 31888cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.org expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 319344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \ 320344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-warning {{not integer constant expression}} \ 321344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 322344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org int g : (S*)(void*)(sptr) == sptr; // \ 323344cf45a40c7de3c4664f8a048d4017af88adfeecommit-bot@chromium.org expected-warning {{not integer constant expression}} \ 3244fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com expected-note {{cast from 'void *' is not allowed in a constant expression}} 325d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.com}; 326086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 327086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comextern char externalvar[]; 328086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comconstexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} 329086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comconstexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}} 33077cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.comstatic_assert(0 != "foo", ""); 33177cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.com 33277cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.com} 33377cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.com 33477cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.comnamespace MaterializeTemporary { 335086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 336086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comconstexpr int f(const int &r) { return r; } 337086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comconstexpr int n = f(1); 338e55fd0f188a2b0b137ddfd9728da58481e2309efbsalomon@google.com 33977cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.comconstexpr bool same(const int &a, const int &b) { return &a == &b; } 34077cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.comconstexpr bool sameTemporary(const int &n) { return same(n, n); } 34177cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.com 34277cf460e5df7dd5272e71f9835880ded48fe8d12bsalomon@google.comstatic_assert(n, ""); 343086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comstatic_assert(!same(4, 4), ""); 34488cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.orgstatic_assert(same(n, n), ""); 345086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.comstatic_assert(sameTemporary(9), ""); 346086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 347086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com} 348086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com 349d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.comconstexpr int strcmp_ce(const char *p, const char *q) { 350086e5354fe7ae60e69c42bdfbc3d03bd8559b44ftomhudson@google.com return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1); 351f0a104e6f16dc095286d32f1e104894ae0b2b19fbsalomon@google.com} 3524fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com 353d7727ceb82e271f8b5580c51571c57b09c5e3cedbsalomon@google.comnamespace StringLiteral { 3545fa2107842e017ee5932955ecf89ceca179e4630bsalomon@google.com 355da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com// FIXME: Refactor this once we support constexpr templates. 356da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.comconstexpr int MangleChars(const char *p) { 357da668988e92a63cc504d6bfc79d94df393cb350ctomhudson@google.com return *p + 3 * (*p ? MangleChars(p+1) : 0); 3584fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com} 3594fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.comconstexpr int MangleChars(const char16_t *p) { 3604fa6694c587b3830932429766c99d08c8dd9b723bsalomon@google.com return *p + 3 * (*p ? MangleChars(p+1) : 0); 361} 362constexpr int MangleChars(const char32_t *p) { 363 return *p + 3 * (*p ? MangleChars(p+1) : 0); 364} 365 366static_assert(MangleChars("constexpr!") == 1768383, ""); 367static_assert(MangleChars(u"constexpr!") == 1768383, ""); 368static_assert(MangleChars(U"constexpr!") == 1768383, ""); 369 370constexpr char c0 = "nought index"[0]; 371constexpr char c1 = "nice index"[10]; 372constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}} 373constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} 374constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} 375 376constexpr const char *p = "test" + 2; 377static_assert(*p == 's', ""); 378 379constexpr const char *max_iter(const char *a, const char *b) { 380 return *a < *b ? b : a; 381} 382constexpr const char *max_element(const char *a, const char *b) { 383 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b)); 384} 385 386constexpr const char *begin(const char (&arr)[45]) { return arr; } 387constexpr const char *end(const char (&arr)[45]) { return arr + 45; } 388 389constexpr char str[] = "the quick brown fox jumped over the lazy dog"; 390constexpr const char *max = max_element(begin(str), end(str)); 391static_assert(*max == 'z', ""); 392static_assert(max == str + 38, ""); 393 394static_assert(strcmp_ce("hello world", "hello world") == 0, ""); 395static_assert(strcmp_ce("hello world", "hello clang") > 0, ""); 396static_assert(strcmp_ce("constexpr", "test") < 0, ""); 397static_assert(strcmp_ce("", " ") < 0, ""); 398 399} 400 401namespace Array { 402 403// FIXME: Use templates for these once we support constexpr templates. 404constexpr int Sum(const int *begin, const int *end) { 405 return begin == end ? 0 : *begin + Sum(begin+1, end); 406} 407constexpr const int *begin(const int (&xs)[5]) { return xs; } 408constexpr const int *end(const int (&xs)[5]) { return xs + 5; } 409 410constexpr int xs[] = { 1, 2, 3, 4, 5 }; 411constexpr int ys[] = { 5, 4, 3, 2, 1 }; 412constexpr int sum_xs = Sum(begin(xs), end(xs)); 413static_assert(sum_xs == 15, ""); 414 415constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, 416 const int *xs, const int *ys, int c) { 417 return n ? F( 418 *xs, // expected-note {{subexpression not valid}} 419 *ys, 420 ZipFoldR(F, n-1, xs+1, ys+1, c)) // \ 421 expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \ 422 expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}} 423 : c; 424} 425constexpr int MulAdd(int x, int y, int c) { return x * y + c; } 426constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); 427static_assert(InnerProduct == 35, ""); 428 429constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } 430constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); 431static_assert(DiffProd == 8, ""); 432static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \ 433 expected-error {{constant expression}} \ 434 expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}} 435 436constexpr const int *p = xs + 3; 437constexpr int xs4 = p[1]; // ok 438constexpr int xs5 = p[2]; // expected-error {{constant expression}} 439constexpr int xs0 = p[-3]; // ok 440constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} 441 442constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 443static_assert(zs[0][0][0][0] == 1, ""); 444static_assert(zs[1][1][1][1] == 16, ""); 445static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 446static_assert((&zs[0][0][0][2])[-1] == 2, ""); 447static_assert(**(**(zs + 1) + 1) == 11, ""); 448static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); 449 450constexpr int fail(const int &p) { 451 return (&p)[64]; // expected-note {{subexpression}} 452} 453static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1)) == 11, ""); // \ 454expected-error {{static_assert expression is not an integral constant expression}} \ 455expected-note {{in call to 'fail(zs[1][0][1][0])'}} 456 457constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{extension}} 458constexpr int SumNonzero(const int *p) { 459 return *p + (*p ? SumNonzero(p+1) : 0); 460} 461constexpr int CountZero(const int *p, const int *q) { 462 return p == q ? 0 : (*p == 0) + CountZero(p+1, q); 463} 464static_assert(SumNonzero(arr) == 6, ""); 465static_assert(CountZero(arr, arr + 40) == 36, ""); 466 467struct ArrayElem { 468 constexpr ArrayElem() : n(0) {} 469 int n; 470 constexpr int f() { return n; } 471}; 472struct ArrayRVal { 473 constexpr ArrayRVal() {} 474 ArrayElem elems[10]; 475}; 476static_assert(ArrayRVal().elems[3].f() == 0, ""); 477 478} 479 480namespace DependentValues { 481 482struct I { int n; typedef I V[10]; }; 483I::V x, y; 484template<bool B> struct S { 485 int k; 486 void f() { 487 I::V &cells = B ? x : y; 488 I &i = cells[k]; 489 switch (i.n) {} 490 } 491}; 492 493} 494 495namespace Class { 496 497struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; 498constexpr int fn(const A &a) { return a.k; } 499static_assert(fn(A(4,5)) == 9, ""); 500 501struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}} 502struct C { 503 constexpr C(C *this_) : m(42), n(this_->m) {} // ok 504 int m, n; 505}; 506struct D { 507 C c; 508 constexpr D() : c(&c) {} 509}; 510static_assert(D().c.n == 42, ""); 511 512struct E { // expected-note {{here}} 513 constexpr E() : p(&p) {} // expected-note {{pointer to temporary cannot be used to initialize a member in a constant expression}} 514 void *p; 515}; 516constexpr const E &e1 = E(); // expected-error {{constant expression}} expected-note {{in call to 'E()'}} expected-note {{temporary created here}} 517// This is a constant expression if we elide the copy constructor call, and 518// is not a constant expression if we don't! But we do, so it is. 519// FIXME: The move constructor is not currently implicitly defined as constexpr. 520constexpr E e2 = E(); // unexpected-error {{constant expression}} unexpected-note {{here}} unexpected-note {{non-constexpr constructor 'E' cannot be used in a constant expression}} 521static_assert(e2.p == &e2.p, ""); // unexpected-error {{constant expression}} unexpected-note {{initializer of 'e2' is not a constant expression}} 522constexpr E e3; 523static_assert(e3.p == &e3.p, ""); 524 525extern const class F f; 526struct F { 527 constexpr F() : p(&f.p) {} 528 const void *p; 529}; 530constexpr F f; 531 532struct G { 533 struct T { 534 constexpr T(T *p) : u1(), u2(p) {} 535 union U1 { 536 constexpr U1() {} 537 int a, b = 42; 538 } u1; 539 union U2 { 540 constexpr U2(T *p) : c(p->u1.b) {} 541 int c, d; 542 } u2; 543 } t; 544 constexpr G() : t(&t) {} 545} constexpr g; 546 547static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 548static_assert(g.t.u1.b == 42, ""); 549static_assert(g.t.u2.c == 42, ""); 550static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 551 552struct S { 553 int a, b; 554 const S *p; 555 double d; 556 const char *q; 557 558 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} 559}; 560 561S global(43, &global); 562 563static_assert(S(15, &global).b == 15, ""); 564 565constexpr bool CheckS(const S &s) { 566 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; 567} 568static_assert(CheckS(S(27, &global)), ""); 569 570struct Arr { 571 char arr[3]; 572 constexpr Arr() : arr{'x', 'y', 'z'} {} 573}; 574constexpr int hash(Arr &&a) { 575 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; 576} 577constexpr int k = hash(Arr()); 578static_assert(k == 0x007a7978, ""); 579 580 581struct AggregateInit { 582 const char &c; 583 int n; 584 double d; 585 int arr[5]; 586 void *p; 587}; 588 589constexpr AggregateInit agg1 = { "hello"[0] }; 590 591static_assert(strcmp_ce(&agg1.c, "hello") == 0, ""); 592static_assert(agg1.n == 0, ""); 593static_assert(agg1.d == 0.0, ""); 594static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 595static_assert(agg1.arr[0] == 0, ""); 596static_assert(agg1.arr[4] == 0, ""); 597static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 598static_assert(agg1.p == nullptr, ""); 599 600namespace SimpleDerivedClass { 601 602struct B { 603 constexpr B(int n) : a(n) {} 604 int a; 605}; 606struct D : B { 607 constexpr D(int n) : B(n) {} 608}; 609constexpr D d(3); 610static_assert(d.a == 3, ""); 611 612} 613 614struct Bottom { constexpr Bottom() {} }; 615struct Base : Bottom { 616 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} 617 int a; 618 const char *b; 619}; 620struct Base2 : Bottom { 621 constexpr Base2(const int &r) : r(r) {} 622 int q = 123; 623 const int &r; 624}; 625struct Derived : Base, Base2 { 626 constexpr Derived() : Base(76), Base2(a) {} 627 int c = r + b[1]; 628}; 629 630constexpr bool operator==(const Base &a, const Base &b) { 631 return a.a == b.a && strcmp_ce(a.b, b.b) == 0; 632} 633 634constexpr Base base; 635constexpr Base base2(76); 636constexpr Derived derived; 637static_assert(derived.a == 76, ""); 638static_assert(derived.b[2] == 's', ""); 639static_assert(derived.c == 76 + 'e', ""); 640static_assert(derived.q == 123, ""); 641static_assert(derived.r == 76, ""); 642static_assert(&derived.r == &derived.a, ""); 643 644static_assert(!(derived == base), ""); 645static_assert(derived == base2, ""); 646 647constexpr Bottom &bot1 = (Base&)derived; 648constexpr Bottom &bot2 = (Base2&)derived; 649static_assert(&bot1 != &bot2, ""); 650 651constexpr Bottom *pb1 = (Base*)&derived; 652constexpr Bottom *pb2 = (Base2*)&derived; 653static_assert(pb1 != pb2, ""); 654static_assert(pb1 == &bot1, ""); 655static_assert(pb2 == &bot2, ""); 656 657constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} 658constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} 659constexpr Base2 &ok2 = (Base2&)bot2; 660static_assert(&ok2 == &derived, ""); 661 662constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} 663constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} 664constexpr Base2 *pok2 = (Base2*)pb2; 665static_assert(pok2 == &derived, ""); 666static_assert(&ok2 == pok2, ""); 667static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, ""); 668static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, ""); 669 670constexpr Base *nullB = 42 - 6 * 7; 671static_assert((Bottom*)nullB == 0, ""); 672static_assert((Derived*)nullB == 0, ""); 673static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); 674 675} 676 677namespace Temporaries { 678 679struct S { 680 constexpr S() {} 681 constexpr int f(); 682}; 683struct T : S { 684 constexpr T(int n) : S(), n(n) {} 685 int n; 686}; 687constexpr int S::f() { 688 // 'this' must be the postfix-expression in a class member access expression, 689 // so we can't just use 690 // return static_cast<T*>(this)->n; 691 return this->*(int(S::*))&T::n; 692} 693// The T temporary is implicitly cast to an S subobject, but we can recover the 694// T full-object via a base-to-derived cast, or a derived-to-base-casted member 695// pointer. 696static_assert(T(3).f() == 3, ""); 697 698constexpr int f(const S &s) { 699 return static_cast<const T&>(s).n; 700} 701constexpr int n = f(T(5)); 702static_assert(f(T(5)) == 5, ""); 703 704} 705 706namespace Union { 707 708union U { 709 int a; 710 int b; 711}; 712 713constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{extension}} 714static_assert(u[0].a == 0, ""); 715static_assert(u[0].b, ""); // expected-error {{constant expression}} 716static_assert(u[1].b == 1, ""); 717static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 718static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 719static_assert((&(u[1]) + 1 + 1)->b == 3, ""); 720 721} 722 723namespace MemberPointer { 724 struct A { 725 constexpr A(int n) : n(n) {} 726 int n; 727 constexpr int f() { return n + 3; } 728 }; 729 constexpr A a(7); 730 static_assert(A(5).*&A::n == 5, ""); 731 static_assert((&a)->*&A::n == 7, ""); 732 static_assert((A(8).*&A::f)() == 11, ""); 733 static_assert(((&a)->*&A::f)() == 10, ""); 734 735 struct B : A { 736 constexpr B(int n, int m) : A(n), m(m) {} 737 int m; 738 constexpr int g() { return n + m + 1; } 739 }; 740 constexpr B b(9, 13); 741 static_assert(B(4, 11).*&A::n == 4, ""); 742 static_assert(B(4, 11).*&B::m == 11, ""); 743 static_assert(B(4, 11).*(int(A::*))&B::m == 11, ""); 744 static_assert((&b)->*&A::n == 9, ""); 745 static_assert((&b)->*&B::m == 13, ""); 746 static_assert((&b)->*(int(A::*))&B::m == 13, ""); 747 static_assert((B(4, 11).*&A::f)() == 7, ""); 748 static_assert((B(4, 11).*&B::g)() == 16, ""); 749 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); 750 static_assert(((&b)->*&A::f)() == 12, ""); 751 static_assert(((&b)->*&B::g)() == 23, ""); 752 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); 753 754 struct S { 755 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : 756 m(m), n(n), pf(pf), pn(pn) {} 757 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} 758 759 constexpr int f() { return this->*pn; } 760 virtual int g() const; 761 762 int m, n; 763 int (S::*pf)() const; 764 int S::*pn; 765 }; 766 767 constexpr int S::*pm = &S::m; 768 constexpr int S::*pn = &S::n; 769 constexpr int (S::*pf)() const = &S::f; 770 constexpr int (S::*pg)() const = &S::g; 771 772 constexpr S s(2, 5, &S::f, &S::m); 773 774 static_assert((s.*&S::f)() == 2, ""); 775 static_assert((s.*s.pf)() == 2, ""); 776 777 template<int n> struct T : T<n-1> {}; 778 template<> struct T<0> { int n; }; 779 template<> struct T<30> : T<29> { int m; }; 780 781 T<17> t17; 782 T<30> t30; 783 784 constexpr int (T<10>::*deepn) = &T<0>::n; 785 static_assert(&(t17.*deepn) == &t17.n, ""); 786 787 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; 788 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} 789 static_assert(&(t30.*deepm) == &t30.m, ""); 790 791 constexpr T<5> *p17_5 = &t17; 792 constexpr T<13> *p17_13 = (T<13>*)p17_5; 793 constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} 794 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); 795 static_assert(&(p17_13->*deepn) == &t17.n, ""); 796 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} 797 798 constexpr T<5> *p30_5 = &t30; 799 constexpr T<23> *p30_23 = (T<23>*)p30_5; 800 constexpr T<13> *p30_13 = p30_23; 801 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); 802 static_assert(&(p30_13->*deepn) == &t30.n, ""); 803 static_assert(&(p30_23->*deepn) == &t30.n, ""); 804 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); 805 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); 806 static_assert(&(p30_23->*deepm) == &t30.m, ""); 807} 808 809namespace ArrayBaseDerived { 810 811 struct Base { 812 constexpr Base() {} 813 int n = 0; 814 }; 815 struct Derived : Base { 816 constexpr Derived() {} 817 constexpr const int *f() { return &n; } 818 }; 819 820 constexpr Derived a[10]; 821 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); 822 constexpr Base *pb3 = const_cast<Derived*>(&a[3]); 823 static_assert(pb3 == pd3, ""); 824 825 // pb3 does not point to an array element. 826 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. 827 constexpr int pb4n = pb4->n; // expected-error {{constant expression}} 828 constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this. 829 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} 830 constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this. 831 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} 832 constexpr Base *pb3a = pb4 - 1; 833 834 // pb4 does not point to a Derived. 835 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} 836 constexpr Derived *pd3a = (Derived*)pb3a; 837 constexpr int pd3n = pd3a->n; 838 839 // pd3a still points to the Derived array. 840 constexpr Derived *pd6 = pd3a + 3; 841 static_assert(pd6 == &a[6], ""); 842 constexpr Derived *pd9 = pd6 + 3; 843 constexpr Derived *pd10 = pd6 + 4; 844 constexpr int pd9n = pd9->n; // ok 845 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} 846 constexpr int pd0n = pd10[-10].n; 847 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} 848 849 constexpr Base *pb9 = pd9; 850 constexpr const int *(Base::*pfb)() const = 851 static_cast<const int *(Base::*)() const>(&Derived::f); 852 static_assert((pb9->*pfb)() == &a[9].n, ""); 853} 854 855namespace Complex { 856 857class complex { 858 int re, im; 859public: 860 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} 861 constexpr complex(const complex &o) : re(o.re), im(o.im) {} 862 constexpr complex operator-() const { return complex(-re, -im); } 863 friend constexpr complex operator+(const complex &l, const complex &r) { 864 return complex(l.re + r.re, l.im + r.im); 865 } 866 friend constexpr complex operator-(const complex &l, const complex &r) { 867 return l + -r; 868 } 869 friend constexpr complex operator*(const complex &l, const complex &r) { 870 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); 871 } 872 friend constexpr bool operator==(const complex &l, const complex &r) { 873 return l.re == r.re && l.im == r.im; 874 } 875 constexpr bool operator!=(const complex &r) const { 876 return re != r.re || im != r.im; 877 } 878 constexpr int real() const { return re; } 879 constexpr int imag() const { return im; } 880}; 881 882constexpr complex i = complex(0, 1); 883constexpr complex k = (3 + 4*i) * (6 - 4*i); 884static_assert(complex(1,0).real() == 1, ""); 885static_assert(complex(1,0).imag() == 0, ""); 886static_assert(((complex)1).imag() == 0, ""); 887static_assert(k.real() == 34, ""); 888static_assert(k.imag() == 12, ""); 889static_assert(k - 34 == 12*i, ""); 890static_assert((complex)1 == complex(1), ""); 891static_assert((complex)1 != complex(0, 1), ""); 892static_assert(complex(1) == complex(1), ""); 893static_assert(complex(1) != complex(0, 1), ""); 894constexpr complex makeComplex(int re, int im) { return complex(re, im); } 895static_assert(makeComplex(1,0) == complex(1), ""); 896static_assert(makeComplex(1,0) != complex(0, 1), ""); 897 898class complex_wrap : public complex { 899public: 900 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} 901 constexpr complex_wrap(const complex_wrap &o) : complex(o) {} 902}; 903 904static_assert((complex_wrap)1 == complex(1), ""); 905static_assert((complex)1 != complex_wrap(0, 1), ""); 906static_assert(complex(1) == complex_wrap(1), ""); 907static_assert(complex_wrap(1) != complex(0, 1), ""); 908constexpr complex_wrap makeComplexWrap(int re, int im) { 909 return complex_wrap(re, im); 910} 911static_assert(makeComplexWrap(1,0) == complex(1), ""); 912static_assert(makeComplexWrap(1,0) != complex(0, 1), ""); 913 914} 915 916namespace PR11595 { 917 struct A { constexpr bool operator==(int x) { return true; } }; 918 struct B { B(); ~B(); A& x; }; 919 static_assert(B().x == 3, ""); // expected-error {{constant expression}} 920} 921