Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i686-linux -Wno-string-plus-int -fsyntax-only -verify -std=c++11 -pedantic %s -Wno-comment |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3 | namespace StaticAssertFoldTest { |
| 4 | |
| 5 | int x; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 6 | static_assert(++x, "test"); // expected-error {{not an integral constant expression}} |
| 7 | static_assert(false, "test"); // expected-error {{test}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 8 | |
| 9 | } |
| 10 | |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 11 | typedef decltype(sizeof(char)) size_t; |
| 12 | |
| 13 | template<typename T> constexpr T id(const T &t) { return t; } |
| 14 | template<typename T> constexpr T min(const T &a, const T &b) { |
| 15 | return a < b ? a : b; |
| 16 | } |
| 17 | template<typename T> constexpr T max(const T &a, const T &b) { |
| 18 | return a < b ? b : a; |
| 19 | } |
| 20 | template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; } |
| 21 | template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; } |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 22 | |
| 23 | struct MemberZero { |
| 24 | constexpr int zero() { return 0; } |
| 25 | }; |
| 26 | |
| 27 | namespace DerivedToVBaseCast { |
| 28 | |
| 29 | struct U { int n; }; |
| 30 | struct V : U { int n; }; |
| 31 | struct A : virtual V { int n; }; |
| 32 | struct Aa { int n; }; |
| 33 | struct B : virtual A, Aa {}; |
| 34 | struct C : virtual A, Aa {}; |
| 35 | struct D : B, C {}; |
| 36 | |
| 37 | D d; |
| 38 | constexpr B *p = &d; |
| 39 | constexpr C *q = &d; |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 40 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 41 | static_assert((void*)p != (void*)q, ""); |
| 42 | static_assert((A*)p == (A*)q, ""); |
| 43 | static_assert((Aa*)p != (Aa*)q, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 44 | |
| 45 | constexpr B &pp = d; |
| 46 | constexpr C &qq = d; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 47 | static_assert((void*)&pp != (void*)&qq, ""); |
| 48 | static_assert(&(A&)pp == &(A&)qq, ""); |
| 49 | static_assert(&(Aa&)pp != &(Aa&)qq, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 50 | |
| 51 | constexpr V *v = p; |
| 52 | constexpr V *w = q; |
| 53 | constexpr V *x = (A*)p; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 54 | static_assert(v == w, ""); |
| 55 | static_assert(v == x, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 56 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 57 | static_assert((U*)&d == p, ""); |
| 58 | static_assert((U*)&d == q, ""); |
| 59 | static_assert((U*)&d == v, ""); |
| 60 | static_assert((U*)&d == w, ""); |
| 61 | static_assert((U*)&d == x, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 62 | |
| 63 | struct X {}; |
| 64 | struct Y1 : virtual X {}; |
| 65 | struct Y2 : X {}; |
| 66 | struct Z : Y1, Y2 {}; |
| 67 | Z z; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 68 | static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 71 | namespace ConstCast { |
| 72 | |
| 73 | constexpr int n1 = 0; |
| 74 | constexpr int n2 = const_cast<int&>(n1); |
| 75 | constexpr int *n3 = const_cast<int*>(&n1); |
| 76 | constexpr int n4 = *const_cast<int*>(&n1); |
| 77 | constexpr const int * const *n5 = const_cast<const int* const*>(&n3); |
| 78 | constexpr int **n6 = const_cast<int**>(&n3); |
| 79 | constexpr int n7 = **n5; |
| 80 | constexpr int n8 = **n6; |
| 81 | |
| 82 | } |
| 83 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 84 | namespace TemplateArgumentConversion { |
| 85 | template<int n> struct IntParam {}; |
| 86 | |
| 87 | using IntParam0 = IntParam<0>; |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 88 | using IntParam0 = IntParam<id(0)>; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 89 | using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | namespace CaseStatements { |
| 93 | void f(int n) { |
| 94 | switch (n) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 95 | case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}} |
| 96 | case id(0): // expected-error {{duplicate case value '0'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | extern int &Recurse1; |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 103 | int &Recurse2 = Recurse1; // expected-note {{declared here}} |
| 104 | int &Recurse1 = Recurse2; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 105 | constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}} |
| 106 | |
| 107 | extern const int RecurseA; |
| 108 | const int RecurseB = RecurseA; // expected-note {{declared here}} |
| 109 | const int RecurseA = 10; |
| 110 | constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 111 | |
| 112 | namespace MemberEnum { |
| 113 | struct WithMemberEnum { |
| 114 | enum E { A = 42 }; |
| 115 | } wme; |
| 116 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 117 | static_assert(wme.A == 42, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | namespace DefaultArguments { |
| 121 | |
| 122 | const int z = int(); |
| 123 | constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) { |
| 124 | return a + b + *c + d; |
| 125 | } |
| 126 | const int four = 4; |
| 127 | constexpr int eight = 8; |
| 128 | constexpr const int twentyseven = 27; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 129 | static_assert(Sum() == 0, ""); |
| 130 | static_assert(Sum(1) == 1, ""); |
| 131 | static_assert(Sum(1, four) == 5, ""); |
| 132 | static_assert(Sum(1, eight, &twentyseven) == 36, ""); |
| 133 | static_assert(Sum(1, 2, &four, eight) == 15, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 134 | |
| 135 | } |
| 136 | |
| 137 | namespace Ellipsis { |
| 138 | |
| 139 | // Note, values passed through an ellipsis can't actually be used. |
| 140 | constexpr int F(int a, ...) { return a; } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 141 | static_assert(F(0) == 0, ""); |
| 142 | static_assert(F(1, 0) == 1, ""); |
| 143 | static_assert(F(2, "test") == 2, ""); |
| 144 | static_assert(F(3, &F) == 3, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 145 | int k = 0; // expected-note {{here}} |
| 146 | static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 147 | |
| 148 | } |
| 149 | |
| 150 | namespace Recursion { |
| 151 | constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 152 | static_assert(fib(11) == 89, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 153 | |
| 154 | constexpr int gcd_inner(int a, int b) { |
| 155 | return b == 0 ? a : gcd_inner(b, a % b); |
| 156 | } |
| 157 | constexpr int gcd(int a, int b) { |
| 158 | return gcd_inner(max(a, b), min(a, b)); |
| 159 | } |
| 160 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 161 | static_assert(gcd(1749237, 5628959) == 7, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | namespace FunctionCast { |
| 165 | // When folding, we allow functions to be cast to different types. Such |
| 166 | // cast functions cannot be called, even if they're constexpr. |
| 167 | constexpr int f() { return 1; } |
| 168 | typedef double (*DoubleFn)(); |
| 169 | typedef int (*IntFn)(); |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 170 | int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 171 | int b[(int)IntFn(f)()]; // ok |
| 172 | } |
| 173 | |
| 174 | namespace StaticMemberFunction { |
| 175 | struct S { |
| 176 | static constexpr int k = 42; |
| 177 | static constexpr int f(int n) { return n * k + 2; } |
| 178 | } s; |
| 179 | |
| 180 | constexpr int n = s.f(19); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 181 | static_assert(S::f(19) == 800, ""); |
| 182 | static_assert(s.f(19) == 800, ""); |
| 183 | static_assert(n == 800, ""); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 184 | |
| 185 | constexpr int (*sf1)(int) = &S::f; |
| 186 | constexpr int (*sf2)(int) = &s.f; |
| 187 | constexpr const int *sk = &s.k; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | namespace ParameterScopes { |
| 191 | |
| 192 | const int k = 42; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 193 | constexpr const int &ObscureTheTruth(const int &a) { return a; } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 194 | constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}} |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 195 | return ObscureTheTruth(b ? a : k); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 196 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 197 | static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 198 | constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 199 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 200 | constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { |
| 201 | return ObscureTheTruth(b ? a : k); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 202 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 203 | static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 204 | constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 205 | |
| 206 | constexpr int InternalReturnJunk(int n) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 207 | return MaybeReturnJunk(true, n); // expected-note {{read of variable whose lifetime has ended}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 208 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 209 | constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 210 | |
| 211 | constexpr int LToR(int &n) { return n; } |
| 212 | constexpr int GrabCallersArgument(bool which, int a, int b) { |
| 213 | return LToR(which ? b : a); |
| 214 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 215 | static_assert(GrabCallersArgument(false, 1, 2) == 1, ""); |
| 216 | static_assert(GrabCallersArgument(true, 4, 8) == 8, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 217 | |
| 218 | } |
| 219 | |
| 220 | namespace Pointers { |
| 221 | |
| 222 | constexpr int f(int n, const int *a, const int *b, const int *c) { |
| 223 | return n == 0 ? 0 : *a + f(n-1, b, c, a); |
| 224 | } |
| 225 | |
| 226 | const int x = 1, y = 10, z = 100; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 227 | static_assert(f(23, &x, &y, &z) == 788, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 228 | |
| 229 | constexpr int g(int n, int a, int b, int c) { |
| 230 | return f(n, &a, &b, &c); |
| 231 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 232 | static_assert(g(23, x, y, z) == 788, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 233 | |
| 234 | } |
| 235 | |
| 236 | namespace FunctionPointers { |
| 237 | |
| 238 | constexpr int Double(int n) { return 2 * n; } |
| 239 | constexpr int Triple(int n) { return 3 * n; } |
| 240 | constexpr int Twice(int (*F)(int), int n) { return F(F(n)); } |
| 241 | constexpr int Quadruple(int n) { return Twice(Double, n); } |
| 242 | constexpr auto Select(int n) -> int (*)(int) { |
| 243 | return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0; |
| 244 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 245 | constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 246 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 247 | static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 248 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 249 | constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 250 | |
| 251 | } |
| 252 | |
| 253 | namespace PointerComparison { |
| 254 | |
| 255 | int x, y; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 256 | static_assert(&x == &y, "false"); // expected-error {{false}} |
| 257 | static_assert(&x != &y, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 258 | constexpr bool g1 = &x == &y; |
| 259 | constexpr bool g2 = &x != &y; |
| 260 | constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} |
| 261 | constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} |
| 262 | constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} |
| 263 | constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} |
| 264 | |
| 265 | struct S { int x, y; } s; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 266 | static_assert(&s.x == &s.y, "false"); // expected-error {{false}} |
| 267 | static_assert(&s.x != &s.y, ""); |
| 268 | static_assert(&s.x <= &s.y, ""); |
| 269 | static_assert(&s.x >= &s.y, "false"); // expected-error {{false}} |
| 270 | static_assert(&s.x < &s.y, ""); |
| 271 | static_assert(&s.x > &s.y, "false"); // expected-error {{false}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 272 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 273 | static_assert(0 == &y, "false"); // expected-error {{false}} |
| 274 | static_assert(0 != &y, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 275 | constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}} |
| 276 | constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}} |
| 277 | constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}} |
| 278 | constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}} |
| 279 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 280 | static_assert(&x == 0, "false"); // expected-error {{false}} |
| 281 | static_assert(&x != 0, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 282 | constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}} |
| 283 | constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}} |
| 284 | constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}} |
| 285 | constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}} |
| 286 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 287 | static_assert(&x == &x, ""); |
| 288 | static_assert(&x != &x, "false"); // expected-error {{false}} |
| 289 | static_assert(&x <= &x, ""); |
| 290 | static_assert(&x >= &x, ""); |
| 291 | static_assert(&x < &x, "false"); // expected-error {{false}} |
| 292 | static_assert(&x > &x, "false"); // expected-error {{false}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 293 | |
| 294 | constexpr S* sptr = &s; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 295 | constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 296 | |
Richard Smith | 2fd5983 | 2012-02-08 08:11:33 +0000 | [diff] [blame] | 297 | struct U {}; |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 298 | struct Str { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 299 | int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 300 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 301 | expected-note {{dynamic_cast is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 302 | int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 303 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 304 | expected-note {{reinterpret_cast is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 305 | int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 306 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 60f24e7 | 2011-12-12 19:33:27 +0000 | [diff] [blame] | 307 | expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 308 | int d : (S*)(42) == (S*)(42); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 309 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 60f24e7 | 2011-12-12 19:33:27 +0000 | [diff] [blame] | 310 | expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 311 | int e : (Str*)(sptr) == (Str*)(sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 312 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 60f24e7 | 2011-12-12 19:33:27 +0000 | [diff] [blame] | 313 | expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
Richard Smith | 2fd5983 | 2012-02-08 08:11:33 +0000 | [diff] [blame] | 314 | int f : &(U&)(*sptr) == &(U&)(*sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 315 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 60f24e7 | 2011-12-12 19:33:27 +0000 | [diff] [blame] | 316 | expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 317 | int g : (S*)(void*)(sptr) == sptr; // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 318 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 319 | expected-note {{cast from 'void *' is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 320 | }; |
| 321 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 322 | extern char externalvar[]; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 323 | constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 324 | constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 325 | static_assert(0 != "foo", ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 326 | |
| 327 | } |
| 328 | |
| 329 | namespace MaterializeTemporary { |
| 330 | |
| 331 | constexpr int f(const int &r) { return r; } |
| 332 | constexpr int n = f(1); |
| 333 | |
| 334 | constexpr bool same(const int &a, const int &b) { return &a == &b; } |
| 335 | constexpr bool sameTemporary(const int &n) { return same(n, n); } |
| 336 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 337 | static_assert(n, ""); |
| 338 | static_assert(!same(4, 4), ""); |
| 339 | static_assert(same(n, n), ""); |
| 340 | static_assert(sameTemporary(9), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 341 | |
| 342 | } |
| 343 | |
| 344 | constexpr int strcmp_ce(const char *p, const char *q) { |
| 345 | return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1); |
| 346 | } |
| 347 | |
| 348 | namespace StringLiteral { |
| 349 | |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 350 | template<typename Char> |
| 351 | constexpr int MangleChars(const Char *p) { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 352 | return *p + 3 * (*p ? MangleChars(p+1) : 0); |
| 353 | } |
| 354 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 355 | static_assert(MangleChars("constexpr!") == 1768383, ""); |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 356 | static_assert(MangleChars(u8"constexpr!") == 1768383, ""); |
| 357 | static_assert(MangleChars(L"constexpr!") == 1768383, ""); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 358 | static_assert(MangleChars(u"constexpr!") == 1768383, ""); |
| 359 | static_assert(MangleChars(U"constexpr!") == 1768383, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 360 | |
| 361 | constexpr char c0 = "nought index"[0]; |
| 362 | constexpr char c1 = "nice index"[10]; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 363 | 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}} |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 364 | 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}} |
| 365 | constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast which performs the conversions of a reinterpret_cast}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 366 | |
| 367 | constexpr const char *p = "test" + 2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 368 | static_assert(*p == 's', ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 369 | |
| 370 | constexpr const char *max_iter(const char *a, const char *b) { |
| 371 | return *a < *b ? b : a; |
| 372 | } |
| 373 | constexpr const char *max_element(const char *a, const char *b) { |
| 374 | return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b)); |
| 375 | } |
| 376 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 377 | constexpr char str[] = "the quick brown fox jumped over the lazy dog"; |
| 378 | constexpr const char *max = max_element(begin(str), end(str)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 379 | static_assert(*max == 'z', ""); |
| 380 | static_assert(max == str + 38, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 381 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 382 | static_assert(strcmp_ce("hello world", "hello world") == 0, ""); |
| 383 | static_assert(strcmp_ce("hello world", "hello clang") > 0, ""); |
| 384 | static_assert(strcmp_ce("constexpr", "test") < 0, ""); |
| 385 | static_assert(strcmp_ce("", " ") < 0, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 386 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 387 | struct S { |
| 388 | 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}} |
| 389 | }; |
| 390 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 391 | struct T { |
| 392 | char c[6]; |
| 393 | constexpr T() : c{"foo"} {} |
| 394 | }; |
| 395 | constexpr T t; |
| 396 | |
| 397 | static_assert(t.c[0] == 'f', ""); |
| 398 | static_assert(t.c[1] == 'o', ""); |
| 399 | static_assert(t.c[2] == 'o', ""); |
| 400 | static_assert(t.c[3] == 0, ""); |
| 401 | static_assert(t.c[4] == 0, ""); |
| 402 | static_assert(t.c[5] == 0, ""); |
| 403 | static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 404 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 405 | struct U { |
| 406 | wchar_t chars[6]; |
| 407 | int n; |
| 408 | } constexpr u = { { L"test" }, 0 }; |
| 409 | static_assert(u.chars[2] == L's', ""); |
| 410 | |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 411 | struct V { |
| 412 | char c[4]; |
| 413 | constexpr V() : c("hi!") {} |
| 414 | }; |
| 415 | static_assert(V().c[1] == "i"[0], ""); |
| 416 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | namespace Array { |
| 420 | |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 421 | template<typename Iter> |
| 422 | constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 423 | return begin == end ? 0 : *begin + Sum(begin+1, end); |
| 424 | } |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 425 | |
| 426 | constexpr int xs[] = { 1, 2, 3, 4, 5 }; |
| 427 | constexpr int ys[] = { 5, 4, 3, 2, 1 }; |
| 428 | constexpr int sum_xs = Sum(begin(xs), end(xs)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 429 | static_assert(sum_xs == 15, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 430 | |
| 431 | constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, |
| 432 | const int *xs, const int *ys, int c) { |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 433 | return n ? F( |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 434 | *xs, // expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 435 | *ys, |
| 436 | ZipFoldR(F, n-1, xs+1, ys+1, c)) // \ |
| 437 | expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \ |
| 438 | expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}} |
| 439 | : c; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 440 | } |
| 441 | constexpr int MulAdd(int x, int y, int c) { return x * y + c; } |
| 442 | constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 443 | static_assert(InnerProduct == 35, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 444 | |
| 445 | constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } |
| 446 | constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 447 | static_assert(DiffProd == 8, ""); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 448 | static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \ |
| 449 | expected-error {{constant expression}} \ |
| 450 | expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 451 | |
| 452 | constexpr const int *p = xs + 3; |
| 453 | constexpr int xs4 = p[1]; // ok |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 454 | constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 455 | constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 456 | constexpr int xs0 = p[-3]; // ok |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 457 | constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 458 | |
| 459 | constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 460 | static_assert(zs[0][0][0][0] == 1, ""); |
| 461 | static_assert(zs[1][1][1][1] == 16, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 462 | static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 463 | static_assert((&zs[0][0][0][2])[-1] == 2, ""); |
| 464 | static_assert(**(**(zs + 1) + 1) == 11, ""); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 465 | 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}} |
| 466 | static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, ""); |
| 467 | 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}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 468 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 469 | constexpr int fail(const int &p) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 470 | return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}} |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 471 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 472 | static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \ |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 473 | expected-error {{static_assert expression is not an integral constant expression}} \ |
| 474 | expected-note {{in call to 'fail(zs[1][0][1][0])'}} |
| 475 | |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 476 | constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 477 | constexpr int SumNonzero(const int *p) { |
| 478 | return *p + (*p ? SumNonzero(p+1) : 0); |
| 479 | } |
| 480 | constexpr int CountZero(const int *p, const int *q) { |
| 481 | return p == q ? 0 : (*p == 0) + CountZero(p+1, q); |
| 482 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 483 | static_assert(SumNonzero(arr) == 6, ""); |
| 484 | static_assert(CountZero(arr, arr + 40) == 36, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 485 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 486 | struct ArrayElem { |
| 487 | constexpr ArrayElem() : n(0) {} |
| 488 | int n; |
| 489 | constexpr int f() { return n; } |
| 490 | }; |
| 491 | struct ArrayRVal { |
| 492 | constexpr ArrayRVal() {} |
| 493 | ArrayElem elems[10]; |
| 494 | }; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 495 | static_assert(ArrayRVal().elems[3].f() == 0, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 496 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | namespace DependentValues { |
| 500 | |
| 501 | struct I { int n; typedef I V[10]; }; |
| 502 | I::V x, y; |
| 503 | template<bool B> struct S { |
| 504 | int k; |
| 505 | void f() { |
| 506 | I::V &cells = B ? x : y; |
| 507 | I &i = cells[k]; |
| 508 | switch (i.n) {} |
| 509 | } |
| 510 | }; |
| 511 | |
| 512 | } |
| 513 | |
| 514 | namespace Class { |
| 515 | |
| 516 | struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; |
| 517 | constexpr int fn(const A &a) { return a.k; } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 518 | static_assert(fn(A(4,5)) == 9, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 519 | |
| 520 | struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}} |
| 521 | struct C { |
| 522 | constexpr C(C *this_) : m(42), n(this_->m) {} // ok |
| 523 | int m, n; |
| 524 | }; |
| 525 | struct D { |
| 526 | C c; |
| 527 | constexpr D() : c(&c) {} |
| 528 | }; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 529 | static_assert(D().c.n == 42, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 530 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 531 | struct E { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 532 | constexpr E() : p(&p) {} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 533 | void *p; |
| 534 | }; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 535 | constexpr const E &e1 = E(); // expected-error {{constant expression}} expected-note {{reference to temporary is not a constant expression}} expected-note {{temporary created here}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 536 | // This is a constant expression if we elide the copy constructor call, and |
| 537 | // is not a constant expression if we don't! But we do, so it is. |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 538 | constexpr E e2 = E(); |
| 539 | static_assert(e2.p == &e2.p, ""); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 540 | constexpr E e3; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 541 | static_assert(e3.p == &e3.p, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 542 | |
| 543 | extern const class F f; |
| 544 | struct F { |
| 545 | constexpr F() : p(&f.p) {} |
| 546 | const void *p; |
| 547 | }; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 548 | constexpr F f; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 549 | |
| 550 | struct G { |
| 551 | struct T { |
| 552 | constexpr T(T *p) : u1(), u2(p) {} |
| 553 | union U1 { |
| 554 | constexpr U1() {} |
| 555 | int a, b = 42; |
| 556 | } u1; |
| 557 | union U2 { |
| 558 | constexpr U2(T *p) : c(p->u1.b) {} |
| 559 | int c, d; |
| 560 | } u2; |
| 561 | } t; |
| 562 | constexpr G() : t(&t) {} |
| 563 | } constexpr g; |
| 564 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 565 | static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 566 | static_assert(g.t.u1.b == 42, ""); |
| 567 | static_assert(g.t.u2.c == 42, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 568 | static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 569 | |
| 570 | struct S { |
| 571 | int a, b; |
| 572 | const S *p; |
| 573 | double d; |
| 574 | const char *q; |
| 575 | |
| 576 | constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} |
| 577 | }; |
| 578 | |
| 579 | S global(43, &global); |
| 580 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 581 | static_assert(S(15, &global).b == 15, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 582 | |
| 583 | constexpr bool CheckS(const S &s) { |
| 584 | return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; |
| 585 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 586 | static_assert(CheckS(S(27, &global)), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 587 | |
| 588 | struct Arr { |
| 589 | char arr[3]; |
| 590 | constexpr Arr() : arr{'x', 'y', 'z'} {} |
| 591 | }; |
| 592 | constexpr int hash(Arr &&a) { |
| 593 | return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; |
| 594 | } |
| 595 | constexpr int k = hash(Arr()); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 596 | static_assert(k == 0x007a7978, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 597 | |
| 598 | |
| 599 | struct AggregateInit { |
| 600 | const char &c; |
| 601 | int n; |
| 602 | double d; |
| 603 | int arr[5]; |
| 604 | void *p; |
| 605 | }; |
| 606 | |
| 607 | constexpr AggregateInit agg1 = { "hello"[0] }; |
| 608 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 609 | static_assert(strcmp_ce(&agg1.c, "hello") == 0, ""); |
| 610 | static_assert(agg1.n == 0, ""); |
| 611 | static_assert(agg1.d == 0.0, ""); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 612 | static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 613 | static_assert(agg1.arr[0] == 0, ""); |
| 614 | static_assert(agg1.arr[4] == 0, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 615 | static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 616 | static_assert(agg1.p == nullptr, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 617 | |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 618 | static constexpr const unsigned char uc[] = { "foo" }; |
| 619 | static_assert(uc[0] == 'f', ""); |
| 620 | static_assert(uc[3] == 0, ""); |
| 621 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 622 | namespace SimpleDerivedClass { |
| 623 | |
| 624 | struct B { |
| 625 | constexpr B(int n) : a(n) {} |
| 626 | int a; |
| 627 | }; |
| 628 | struct D : B { |
| 629 | constexpr D(int n) : B(n) {} |
| 630 | }; |
| 631 | constexpr D d(3); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 632 | static_assert(d.a == 3, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 633 | |
| 634 | } |
| 635 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 636 | struct Bottom { constexpr Bottom() {} }; |
| 637 | struct Base : Bottom { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 638 | constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} |
| 639 | int a; |
| 640 | const char *b; |
| 641 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 642 | struct Base2 : Bottom { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 643 | constexpr Base2(const int &r) : r(r) {} |
| 644 | int q = 123; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 645 | const int &r; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 646 | }; |
| 647 | struct Derived : Base, Base2 { |
| 648 | constexpr Derived() : Base(76), Base2(a) {} |
| 649 | int c = r + b[1]; |
| 650 | }; |
| 651 | |
| 652 | constexpr bool operator==(const Base &a, const Base &b) { |
| 653 | return a.a == b.a && strcmp_ce(a.b, b.b) == 0; |
| 654 | } |
| 655 | |
| 656 | constexpr Base base; |
| 657 | constexpr Base base2(76); |
| 658 | constexpr Derived derived; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 659 | static_assert(derived.a == 76, ""); |
| 660 | static_assert(derived.b[2] == 's', ""); |
| 661 | static_assert(derived.c == 76 + 'e', ""); |
| 662 | static_assert(derived.q == 123, ""); |
| 663 | static_assert(derived.r == 76, ""); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 664 | static_assert(&derived.r == &derived.a, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 665 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 666 | static_assert(!(derived == base), ""); |
| 667 | static_assert(derived == base2, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 668 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 669 | constexpr Bottom &bot1 = (Base&)derived; |
| 670 | constexpr Bottom &bot2 = (Base2&)derived; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 671 | static_assert(&bot1 != &bot2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 672 | |
| 673 | constexpr Bottom *pb1 = (Base*)&derived; |
| 674 | constexpr Bottom *pb2 = (Base2*)&derived; |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 675 | static_assert(&pb1 != &pb2, ""); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 676 | static_assert(pb1 == &bot1, ""); |
| 677 | static_assert(pb2 == &bot2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 678 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 679 | constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}} |
| 680 | constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 681 | constexpr Base2 &ok2 = (Base2&)bot2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 682 | static_assert(&ok2 == &derived, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 683 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 684 | constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}} |
| 685 | constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 686 | constexpr Base2 *pok2 = (Base2*)pb2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 687 | static_assert(pok2 == &derived, ""); |
| 688 | static_assert(&ok2 == pok2, ""); |
| 689 | static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, ""); |
| 690 | static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 691 | |
| 692 | constexpr Base *nullB = 42 - 6 * 7; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 693 | static_assert((Bottom*)nullB == 0, ""); |
| 694 | static_assert((Derived*)nullB == 0, ""); |
| 695 | static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 696 | |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 697 | namespace ConversionOperators { |
| 698 | |
| 699 | struct T { |
| 700 | constexpr T(int n) : k(5*n - 3) {} |
| 701 | constexpr operator int() { return k; } |
| 702 | int k; |
| 703 | }; |
| 704 | |
| 705 | struct S { |
| 706 | constexpr S(int n) : k(2*n + 1) {} |
| 707 | constexpr operator int() { return k; } |
| 708 | constexpr operator T() { return T(k); } |
| 709 | int k; |
| 710 | }; |
| 711 | |
| 712 | constexpr bool check(T a, T b) { return a == b.k; } |
| 713 | |
| 714 | static_assert(S(5) == 11, ""); |
| 715 | static_assert(check(S(5), 11), ""); |
| 716 | |
| 717 | } |
| 718 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | namespace Temporaries { |
| 722 | |
| 723 | struct S { |
| 724 | constexpr S() {} |
| 725 | constexpr int f(); |
| 726 | }; |
| 727 | struct T : S { |
| 728 | constexpr T(int n) : S(), n(n) {} |
| 729 | int n; |
| 730 | }; |
| 731 | constexpr int S::f() { |
| 732 | // 'this' must be the postfix-expression in a class member access expression, |
| 733 | // so we can't just use |
| 734 | // return static_cast<T*>(this)->n; |
| 735 | return this->*(int(S::*))&T::n; |
| 736 | } |
| 737 | // The T temporary is implicitly cast to an S subobject, but we can recover the |
| 738 | // T full-object via a base-to-derived cast, or a derived-to-base-casted member |
| 739 | // pointer. |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 740 | static_assert(T(3).f() == 3, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 741 | |
| 742 | constexpr int f(const S &s) { |
| 743 | return static_cast<const T&>(s).n; |
| 744 | } |
| 745 | constexpr int n = f(T(5)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 746 | static_assert(f(T(5)) == 5, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 747 | |
Richard Smith | f2e4cd7 | 2012-01-26 04:47:34 +0000 | [diff] [blame] | 748 | constexpr bool b(int n) { return &n; } |
| 749 | static_assert(b(0), ""); |
| 750 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | namespace Union { |
| 754 | |
| 755 | union U { |
| 756 | int a; |
| 757 | int b; |
| 758 | }; |
| 759 | |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 760 | constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 761 | static_assert(u[0].a == 0, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 762 | static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 763 | static_assert(u[1].b == 1, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 764 | static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 765 | static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 766 | static_assert((&(u[1]) + 1 + 1)->b == 3, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 767 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 768 | constexpr U v = {}; |
| 769 | static_assert(v.a == 0, ""); |
| 770 | |
| 771 | union Empty {}; |
| 772 | constexpr Empty e = {}; |
| 773 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 774 | // Make sure we handle trivial copy constructors for unions. |
| 775 | constexpr U x = {42}; |
| 776 | constexpr U y = x; |
| 777 | static_assert(y.a == 42, ""); |
| 778 | static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}} |
| 779 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 782 | namespace MemberPointer { |
| 783 | struct A { |
| 784 | constexpr A(int n) : n(n) {} |
| 785 | int n; |
| 786 | constexpr int f() { return n + 3; } |
| 787 | }; |
| 788 | constexpr A a(7); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 789 | static_assert(A(5).*&A::n == 5, ""); |
| 790 | static_assert((&a)->*&A::n == 7, ""); |
| 791 | static_assert((A(8).*&A::f)() == 11, ""); |
| 792 | static_assert(((&a)->*&A::f)() == 10, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 793 | |
| 794 | struct B : A { |
| 795 | constexpr B(int n, int m) : A(n), m(m) {} |
| 796 | int m; |
| 797 | constexpr int g() { return n + m + 1; } |
| 798 | }; |
| 799 | constexpr B b(9, 13); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 800 | static_assert(B(4, 11).*&A::n == 4, ""); |
| 801 | static_assert(B(4, 11).*&B::m == 11, ""); |
| 802 | static_assert(B(4, 11).*(int(A::*))&B::m == 11, ""); |
| 803 | static_assert((&b)->*&A::n == 9, ""); |
| 804 | static_assert((&b)->*&B::m == 13, ""); |
| 805 | static_assert((&b)->*(int(A::*))&B::m == 13, ""); |
| 806 | static_assert((B(4, 11).*&A::f)() == 7, ""); |
| 807 | static_assert((B(4, 11).*&B::g)() == 16, ""); |
| 808 | static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); |
| 809 | static_assert(((&b)->*&A::f)() == 12, ""); |
| 810 | static_assert(((&b)->*&B::g)() == 23, ""); |
| 811 | static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 812 | |
| 813 | struct S { |
| 814 | constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : |
| 815 | m(m), n(n), pf(pf), pn(pn) {} |
| 816 | constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} |
| 817 | |
| 818 | constexpr int f() { return this->*pn; } |
| 819 | virtual int g() const; |
| 820 | |
| 821 | int m, n; |
| 822 | int (S::*pf)() const; |
| 823 | int S::*pn; |
| 824 | }; |
| 825 | |
| 826 | constexpr int S::*pm = &S::m; |
| 827 | constexpr int S::*pn = &S::n; |
| 828 | constexpr int (S::*pf)() const = &S::f; |
| 829 | constexpr int (S::*pg)() const = &S::g; |
| 830 | |
| 831 | constexpr S s(2, 5, &S::f, &S::m); |
| 832 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 833 | static_assert((s.*&S::f)() == 2, ""); |
| 834 | static_assert((s.*s.pf)() == 2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 835 | |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 836 | static_assert(pf == &S::f, ""); |
| 837 | static_assert(pf == s.*&S::pf, ""); |
| 838 | static_assert(pm == &S::m, ""); |
| 839 | static_assert(pm != pn, ""); |
| 840 | static_assert(s.pn != pn, ""); |
| 841 | static_assert(s.pn == pm, ""); |
| 842 | static_assert(pg != nullptr, ""); |
| 843 | static_assert(pf != nullptr, ""); |
| 844 | static_assert((int S::*)nullptr == nullptr, ""); |
| 845 | static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} |
| 846 | static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} |
| 847 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 848 | template<int n> struct T : T<n-1> {}; |
| 849 | template<> struct T<0> { int n; }; |
| 850 | template<> struct T<30> : T<29> { int m; }; |
| 851 | |
| 852 | T<17> t17; |
| 853 | T<30> t30; |
| 854 | |
| 855 | constexpr int (T<10>::*deepn) = &T<0>::n; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 856 | static_assert(&(t17.*deepn) == &t17.n, ""); |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 857 | static_assert(deepn == &T<2>::n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 858 | |
| 859 | constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; |
| 860 | constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 861 | static_assert(&(t30.*deepm) == &t30.m, ""); |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 862 | static_assert(deepm == &T<50>::m, ""); |
| 863 | static_assert(deepm != deepn, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 864 | |
| 865 | constexpr T<5> *p17_5 = &t17; |
| 866 | constexpr T<13> *p17_13 = (T<13>*)p17_5; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 867 | 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>'}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 868 | static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); |
| 869 | static_assert(&(p17_13->*deepn) == &t17.n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 870 | constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} |
| 871 | |
| 872 | constexpr T<5> *p30_5 = &t30; |
| 873 | constexpr T<23> *p30_23 = (T<23>*)p30_5; |
| 874 | constexpr T<13> *p30_13 = p30_23; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 875 | static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); |
| 876 | static_assert(&(p30_13->*deepn) == &t30.n, ""); |
| 877 | static_assert(&(p30_23->*deepn) == &t30.n, ""); |
| 878 | static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); |
| 879 | static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); |
| 880 | static_assert(&(p30_23->*deepm) == &t30.m, ""); |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 881 | |
| 882 | struct Base { int n; }; |
| 883 | template<int N> struct Mid : Base {}; |
| 884 | struct Derived : Mid<0>, Mid<1> {}; |
| 885 | static_assert(&Mid<0>::n == &Mid<1>::n, ""); |
| 886 | static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n != |
| 887 | (int Derived::*)(int Mid<1>::*)&Mid<1>::n, ""); |
| 888 | static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | namespace ArrayBaseDerived { |
| 892 | |
| 893 | struct Base { |
| 894 | constexpr Base() {} |
| 895 | int n = 0; |
| 896 | }; |
| 897 | struct Derived : Base { |
| 898 | constexpr Derived() {} |
| 899 | constexpr const int *f() { return &n; } |
| 900 | }; |
| 901 | |
| 902 | constexpr Derived a[10]; |
| 903 | constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); |
| 904 | constexpr Base *pb3 = const_cast<Derived*>(&a[3]); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 905 | static_assert(pb3 == pd3, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 906 | |
| 907 | // pb3 does not point to an array element. |
| 908 | constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 909 | constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}} |
| 910 | constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}} |
| 911 | constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}} |
| 912 | constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}} |
| 913 | constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 914 | constexpr Base *pb3a = pb4 - 1; |
| 915 | |
| 916 | // pb4 does not point to a Derived. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 917 | constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 918 | constexpr Derived *pd3a = (Derived*)pb3a; |
| 919 | constexpr int pd3n = pd3a->n; |
| 920 | |
| 921 | // pd3a still points to the Derived array. |
| 922 | constexpr Derived *pd6 = pd3a + 3; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 923 | static_assert(pd6 == &a[6], ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 924 | constexpr Derived *pd9 = pd6 + 3; |
| 925 | constexpr Derived *pd10 = pd6 + 4; |
| 926 | constexpr int pd9n = pd9->n; // ok |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 927 | constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 928 | constexpr int pd0n = pd10[-10].n; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 929 | constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 930 | |
| 931 | constexpr Base *pb9 = pd9; |
| 932 | constexpr const int *(Base::*pfb)() const = |
| 933 | static_cast<const int *(Base::*)() const>(&Derived::f); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 934 | static_assert((pb9->*pfb)() == &a[9].n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 937 | namespace Complex { |
| 938 | |
| 939 | class complex { |
| 940 | int re, im; |
| 941 | public: |
| 942 | constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} |
| 943 | constexpr complex(const complex &o) : re(o.re), im(o.im) {} |
| 944 | constexpr complex operator-() const { return complex(-re, -im); } |
| 945 | friend constexpr complex operator+(const complex &l, const complex &r) { |
| 946 | return complex(l.re + r.re, l.im + r.im); |
| 947 | } |
| 948 | friend constexpr complex operator-(const complex &l, const complex &r) { |
| 949 | return l + -r; |
| 950 | } |
| 951 | friend constexpr complex operator*(const complex &l, const complex &r) { |
| 952 | return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); |
| 953 | } |
| 954 | friend constexpr bool operator==(const complex &l, const complex &r) { |
| 955 | return l.re == r.re && l.im == r.im; |
| 956 | } |
| 957 | constexpr bool operator!=(const complex &r) const { |
| 958 | return re != r.re || im != r.im; |
| 959 | } |
| 960 | constexpr int real() const { return re; } |
| 961 | constexpr int imag() const { return im; } |
| 962 | }; |
| 963 | |
| 964 | constexpr complex i = complex(0, 1); |
| 965 | constexpr complex k = (3 + 4*i) * (6 - 4*i); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 966 | static_assert(complex(1,0).real() == 1, ""); |
| 967 | static_assert(complex(1,0).imag() == 0, ""); |
| 968 | static_assert(((complex)1).imag() == 0, ""); |
| 969 | static_assert(k.real() == 34, ""); |
| 970 | static_assert(k.imag() == 12, ""); |
| 971 | static_assert(k - 34 == 12*i, ""); |
| 972 | static_assert((complex)1 == complex(1), ""); |
| 973 | static_assert((complex)1 != complex(0, 1), ""); |
| 974 | static_assert(complex(1) == complex(1), ""); |
| 975 | static_assert(complex(1) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 976 | constexpr complex makeComplex(int re, int im) { return complex(re, im); } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 977 | static_assert(makeComplex(1,0) == complex(1), ""); |
| 978 | static_assert(makeComplex(1,0) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 979 | |
| 980 | class complex_wrap : public complex { |
| 981 | public: |
| 982 | constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} |
| 983 | constexpr complex_wrap(const complex_wrap &o) : complex(o) {} |
| 984 | }; |
| 985 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 986 | static_assert((complex_wrap)1 == complex(1), ""); |
| 987 | static_assert((complex)1 != complex_wrap(0, 1), ""); |
| 988 | static_assert(complex(1) == complex_wrap(1), ""); |
| 989 | static_assert(complex_wrap(1) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 990 | constexpr complex_wrap makeComplexWrap(int re, int im) { |
| 991 | return complex_wrap(re, im); |
| 992 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 993 | static_assert(makeComplexWrap(1,0) == complex(1), ""); |
| 994 | static_assert(makeComplexWrap(1,0) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 995 | |
| 996 | } |
Eli Friedman | f59ff8c | 2011-12-17 02:24:21 +0000 | [diff] [blame] | 997 | |
| 998 | namespace PR11595 { |
| 999 | struct A { constexpr bool operator==(int x) { return true; } }; |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 1000 | struct B { B(); A& x; }; |
| 1001 | static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} |
| 1002 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1003 | constexpr bool f(int k) { // expected-error {{constexpr function never produces a constant expression}} |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 1004 | return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} |
| 1005 | } |
Eli Friedman | f59ff8c | 2011-12-17 02:24:21 +0000 | [diff] [blame] | 1006 | } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 1007 | |
| 1008 | namespace ExprWithCleanups { |
| 1009 | struct A { A(); ~A(); int get(); }; |
| 1010 | constexpr int get(bool FromA) { return FromA ? A().get() : 1; } |
| 1011 | constexpr int n = get(false); |
| 1012 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1013 | |
| 1014 | namespace Volatile { |
| 1015 | |
| 1016 | volatile constexpr int n1 = 0; // expected-note {{here}} |
| 1017 | volatile const int n2 = 0; // expected-note {{here}} |
| 1018 | int n3 = 37; // expected-note {{declared here}} |
| 1019 | |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 1020 | constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} |
| 1021 | constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} |
| 1022 | constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}} |
| 1023 | constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}} |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1024 | |
| 1025 | struct T { int n; }; |
| 1026 | const T t = { 42 }; // expected-note {{declared here}} |
| 1027 | |
| 1028 | constexpr int f(volatile int &&r) { |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 1029 | return r; // expected-note {{read of volatile-qualified type 'volatile int'}} |
| 1030 | } |
| 1031 | constexpr int g(volatile int &&r) { |
| 1032 | return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}} |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1033 | } |
| 1034 | struct S { |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 1035 | int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}} |
| 1036 | int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}} |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1037 | int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}} |
| 1038 | int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}} |
| 1039 | }; |
| 1040 | |
| 1041 | } |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 1042 | |
| 1043 | namespace ExternConstexpr { |
| 1044 | extern constexpr int n = 0; |
| 1045 | extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}} |
| 1046 | void f() { |
| 1047 | extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}} |
| 1048 | constexpr int j = 0; |
| 1049 | constexpr int k; // expected-error {{default initialization of an object of const type}} |
| 1050 | } |
| 1051 | } |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 1052 | |
| 1053 | namespace ComplexConstexpr { |
| 1054 | constexpr _Complex float test1 = {}; |
| 1055 | constexpr _Complex float test2 = {1}; |
| 1056 | constexpr _Complex double test3 = {1,2}; |
| 1057 | constexpr _Complex int test4 = {4}; |
| 1058 | constexpr _Complex int test5 = 4; |
| 1059 | constexpr _Complex int test6 = {5,6}; |
Eli Friedman | f6c17a4 | 2012-01-13 23:34:56 +0000 | [diff] [blame] | 1060 | typedef _Complex float fcomplex; |
| 1061 | constexpr fcomplex test7 = fcomplex(); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1062 | |
| 1063 | constexpr const double &t2r = __real test3; |
| 1064 | constexpr const double &t2i = __imag test3; |
| 1065 | static_assert(&t2r + 1 == &t2i, ""); |
| 1066 | static_assert(t2r == 1.0, ""); |
| 1067 | static_assert(t2i == 2.0, ""); |
| 1068 | constexpr const double *t2p = &t2r; |
| 1069 | static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}} |
| 1070 | static_assert(t2p[0] == 1.0, ""); |
| 1071 | static_assert(t2p[1] == 2.0, ""); |
| 1072 | static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}} |
| 1073 | static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}} |
| 1074 | constexpr _Complex float *p = 0; |
| 1075 | constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{cannot access real component of null}} |
| 1076 | constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}} |
| 1077 | constexpr const _Complex double *q = &test3 + 1; |
| 1078 | constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}} |
| 1079 | constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}} |
| 1080 | |
| 1081 | static_assert(__real test6 == 5, ""); |
| 1082 | static_assert(__imag test6 == 6, ""); |
| 1083 | static_assert(&__imag test6 == &__real test6 + 1, ""); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 1084 | } |
Eli Friedman | 6b3014b | 2012-01-18 02:54:10 +0000 | [diff] [blame] | 1085 | |
| 1086 | namespace InstantiateCaseStmt { |
| 1087 | template<int x> constexpr int f() { return x; } |
| 1088 | template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; } |
| 1089 | int gg(int c) { return g<4>(c); } |
| 1090 | } |
Richard Smith | 495f42a | 2012-01-24 05:40:50 +0000 | [diff] [blame] | 1091 | |
| 1092 | namespace ConvertedConstantExpr { |
| 1093 | extern int &m; |
| 1094 | extern int &n; |
| 1095 | |
| 1096 | constexpr int k = 4; |
| 1097 | int &m = const_cast<int&>(k); |
| 1098 | |
| 1099 | // If we have nothing more interesting to say, ensure we don't produce a |
| 1100 | // useless note and instead just point to the non-constant subexpression. |
| 1101 | enum class E { |
| 1102 | em = m, |
| 1103 | en = n, // expected-error {{not a constant expression}} |
| 1104 | eo = (m + |
| 1105 | n // expected-error {{not a constant expression}} |
| 1106 | ), |
| 1107 | eq = reinterpret_cast<int>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}} |
| 1108 | }; |
| 1109 | } |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1110 | |
| 1111 | namespace IndirectField { |
| 1112 | struct S { |
| 1113 | struct { // expected-warning {{GNU extension}} |
| 1114 | union { |
| 1115 | struct { // expected-warning {{GNU extension}} |
| 1116 | int a; |
| 1117 | int b; |
| 1118 | }; |
| 1119 | int c; |
| 1120 | }; |
| 1121 | int d; |
| 1122 | }; |
| 1123 | union { |
| 1124 | int e; |
| 1125 | int f; |
| 1126 | }; |
| 1127 | constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {} |
| 1128 | constexpr S(int c, int d, int f) : c(c), d(d), f(f) {} |
| 1129 | }; |
| 1130 | |
| 1131 | constexpr S s1(1, 2, 3, 4); |
| 1132 | constexpr S s2(5, 6, 7); |
| 1133 | |
| 1134 | // FIXME: The diagnostics here do a very poor job of explaining which unnamed |
| 1135 | // member is active and which is requested. |
| 1136 | static_assert(s1.a == 1, ""); |
| 1137 | static_assert(s1.b == 2, ""); |
| 1138 | static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1139 | static_assert(s1.d == 3, ""); |
| 1140 | static_assert(s1.e == 4, ""); |
| 1141 | static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1142 | |
| 1143 | static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1144 | static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1145 | static_assert(s2.c == 5, ""); |
| 1146 | static_assert(s2.d == 6, ""); |
| 1147 | static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1148 | static_assert(s2.f == 7, ""); |
| 1149 | } |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1150 | |
Richard Smith | b4e5e28 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1151 | // DR1405: don't allow reading mutable members in constant expressions. |
| 1152 | namespace MutableMembers { |
| 1153 | struct MM { |
| 1154 | mutable int n; // expected-note 3{{declared here}} |
| 1155 | } constexpr mm = { 4 }; |
| 1156 | constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} |
| 1157 | int x = (mm.n = 1, 3); |
| 1158 | constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} |
| 1159 | |
| 1160 | // Here's one reason why allowing this would be a disaster... |
| 1161 | template<int n> struct Id { int k = n; }; |
| 1162 | int f() { |
| 1163 | constexpr MM m = { 0 }; |
| 1164 | ++m.n; |
| 1165 | 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}} |
| 1166 | } |
| 1167 | |
| 1168 | struct A { int n; }; |
| 1169 | struct B { mutable A a; }; // expected-note {{here}} |
| 1170 | struct C { B b; }; |
| 1171 | constexpr C c[3] = {}; |
| 1172 | constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}} |
| 1173 | } |
| 1174 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1175 | namespace Fold { |
| 1176 | |
| 1177 | // This macro forces its argument to be constant-folded, even if it's not |
| 1178 | // otherwise a constant expression. |
| 1179 | #define fold(x) (__builtin_constant_p(x) ? (x) : (x)) |
| 1180 | |
| 1181 | constexpr int n = (int)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}} |
| 1182 | constexpr int m = fold((int)(char*)123); // ok |
| 1183 | static_assert(m == 123, ""); |
| 1184 | |
| 1185 | #undef fold |
| 1186 | |
| 1187 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1188 | |
| 1189 | namespace DR1454 { |
| 1190 | |
| 1191 | constexpr const int &f(const int &n) { return n; } |
| 1192 | constexpr int k1 = f(0); // ok |
| 1193 | |
| 1194 | struct Wrap { |
| 1195 | const int &value; |
| 1196 | }; |
| 1197 | constexpr const Wrap &g(const Wrap &w) { return w; } |
| 1198 | constexpr int k2 = g({0}).value; // ok |
| 1199 | |
| 1200 | constexpr const int &i = 0; // expected-error {{constant expression}} expected-note {{temporary}} expected-note 2{{here}} |
| 1201 | constexpr const int j = i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}} |
| 1202 | |
| 1203 | } |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1204 | |
| 1205 | namespace RecursiveOpaqueExpr { |
| 1206 | template<typename Iter> |
| 1207 | constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) { |
| 1208 | return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}} |
| 1209 | } |
| 1210 | |
| 1211 | constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 }; |
| 1212 | static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, ""); |
| 1213 | |
| 1214 | constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 }; |
| 1215 | static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, ""); |
Richard Smith | e92b1f4 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 1216 | |
| 1217 | constexpr int arr3[] = { |
| 1218 | 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, |
| 1219 | 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, |
| 1220 | 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, |
| 1221 | 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, |
| 1222 | 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, |
| 1223 | 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, |
| 1224 | 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, |
| 1225 | 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 }; |
| 1226 | static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, ""); |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | namespace VLASizeof { |
| 1230 | |
| 1231 | void f(int k) { |
| 1232 | int arr[k]; // expected-warning {{C99}} |
| 1233 | constexpr int n = 1 + |
| 1234 | sizeof(arr) // expected-error {{constant expression}} |
| 1235 | * 3; |
| 1236 | } |
| 1237 | } |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1238 | |
| 1239 | namespace CompoundLiteral { |
| 1240 | // FIXME: |
| 1241 | // We don't model the semantics of this correctly: the compound literal is |
| 1242 | // represented as a prvalue in the AST, but actually behaves like an lvalue. |
| 1243 | // We treat the compound literal as a temporary and refuse to produce a |
| 1244 | // pointer to it. This is OK: we're not required to treat this as a constant |
| 1245 | // in C++, and in C we model compound literals as lvalues. |
| 1246 | constexpr int *p = (int*)(int[1]){0}; // expected-warning {{C99}} expected-error {{constant expression}} expected-note 2{{temporary}} |
| 1247 | } |
Richard Smith | 4b1f684 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 1248 | |
| 1249 | namespace Vector { |
| 1250 | typedef int __attribute__((vector_size(16))) VI4; |
| 1251 | constexpr VI4 f(int n) { |
| 1252 | return VI4 { n * 3, n + 4, n - 5, n / 6 }; |
| 1253 | } |
| 1254 | constexpr auto v1 = f(10); |
| 1255 | |
| 1256 | typedef double __attribute__((vector_size(32))) VD4; |
| 1257 | constexpr VD4 g(int n) { |
| 1258 | return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}} |
| 1259 | } |
| 1260 | constexpr auto v2 = g(4); |
| 1261 | } |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1262 | |
| 1263 | // PR12626, redux |
| 1264 | namespace InvalidClasses { |
| 1265 | void test0() { |
| 1266 | struct X; // expected-note {{forward declaration}} |
| 1267 | struct Y { bool b; X x; }; // expected-error {{field has incomplete type}} |
| 1268 | Y y; |
| 1269 | auto& b = y.b; |
| 1270 | } |
| 1271 | } |
Richard Smith | dbbeccc | 2012-05-15 05:04:02 +0000 | [diff] [blame] | 1272 | |
Richard Smith | 622da85 | 2012-07-02 06:15:40 +0000 | [diff] [blame] | 1273 | // Constructors can be implicitly constexpr, even for a non-literal type. |
| 1274 | namespace ImplicitConstexpr { |
| 1275 | struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}} |
| 1276 | struct R { constexpr R(); constexpr R(const R&); constexpr R(R&&); ~R(); }; |
| 1277 | struct S { R r; }; // expected-note 3{{here}} |
| 1278 | struct T { T(const T&); T(T &&); ~T(); }; |
| 1279 | struct U { T t; }; // expected-note 3{{here}} |
| 1280 | static_assert(!__is_literal_type(Q), ""); |
| 1281 | static_assert(!__is_literal_type(R), ""); |
| 1282 | static_assert(!__is_literal_type(S), ""); |
| 1283 | static_assert(!__is_literal_type(T), ""); |
| 1284 | static_assert(!__is_literal_type(U), ""); |
| 1285 | struct Test { |
| 1286 | friend Q::Q() noexcept; // expected-error {{follows constexpr}} |
| 1287 | friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}} |
| 1288 | friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}} |
| 1289 | friend S::S() noexcept; // expected-error {{follows constexpr}} |
| 1290 | friend S::S(S&&) noexcept; // expected-error {{follows constexpr}} |
| 1291 | friend S::S(const S&) noexcept; // expected-error {{follows constexpr}} |
| 1292 | friend constexpr U::U() noexcept; // expected-error {{follows non-constexpr}} |
| 1293 | friend constexpr U::U(U&&) noexcept; // expected-error {{follows non-constexpr}} |
| 1294 | friend constexpr U::U(const U&) noexcept; // expected-error {{follows non-constexpr}} |
| 1295 | }; |
| 1296 | } |
| 1297 | |
Richard Smith | dbbeccc | 2012-05-15 05:04:02 +0000 | [diff] [blame] | 1298 | // Indirectly test that an implicit lvalue to xvalue conversion performed for |
| 1299 | // an NRVO move operation isn't implemented as CK_LValueToRValue. |
| 1300 | namespace PR12826 { |
| 1301 | struct Foo {}; |
| 1302 | constexpr Foo id(Foo x) { return x; } |
| 1303 | constexpr Foo res(id(Foo())); |
| 1304 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 1305 | |
| 1306 | namespace PR13273 { |
| 1307 | struct U { |
| 1308 | int t; |
| 1309 | U() = default; |
| 1310 | }; |
| 1311 | |
| 1312 | struct S : U { |
| 1313 | S() = default; |
| 1314 | }; |
| 1315 | |
| 1316 | // S's default constructor isn't constexpr, because U's default constructor |
| 1317 | // doesn't initialize 't', but it's trivial, so value-initialization doesn't |
| 1318 | // actually call it. |
| 1319 | static_assert(S{}.t == 0, ""); |
| 1320 | } |