Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i686-linux -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 | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 103 | int &Recurse2 = Recurse1; // expected-note 2{{declared here}} expected-note {{initializer of 'Recurse1' is not a constant expression}} |
| 104 | int &Recurse1 = Recurse2; // expected-note {{declared here}} expected-note {{initializer of 'Recurse2' is not a constant expression}} |
| 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 | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 193 | constexpr const int &ObscureTheTruth(const int &a) { return a; } // expected-note 3{{reference to 'a' cannot be returned from a constexpr function}} |
| 194 | constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}} |
| 195 | return ObscureTheTruth(b ? a : k); // expected-note 2{{in call to 'ObscureTheTruth(a)'}} |
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 | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 198 | constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnJunk(1, 0)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 199 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 200 | constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 201 | // If ObscureTheTruth returns a reference to 'a', the result is not a |
| 202 | // constant expression even though 'a' is still in scope. |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 203 | return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 204 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 205 | static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 206 | constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnNonstaticRef(1, 0)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 207 | |
| 208 | constexpr int InternalReturnJunk(int n) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 209 | // TODO: We could reject this: it never produces a constant expression. |
| 210 | // However, we currently don't evaluate function calls while testing for |
| 211 | // potential constant expressions, for performance. |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 212 | return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 213 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 214 | 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] | 215 | |
| 216 | constexpr int LToR(int &n) { return n; } |
| 217 | constexpr int GrabCallersArgument(bool which, int a, int b) { |
| 218 | return LToR(which ? b : a); |
| 219 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 220 | static_assert(GrabCallersArgument(false, 1, 2) == 1, ""); |
| 221 | static_assert(GrabCallersArgument(true, 4, 8) == 8, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 222 | |
| 223 | } |
| 224 | |
| 225 | namespace Pointers { |
| 226 | |
| 227 | constexpr int f(int n, const int *a, const int *b, const int *c) { |
| 228 | return n == 0 ? 0 : *a + f(n-1, b, c, a); |
| 229 | } |
| 230 | |
| 231 | const int x = 1, y = 10, z = 100; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 232 | static_assert(f(23, &x, &y, &z) == 788, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 233 | |
| 234 | constexpr int g(int n, int a, int b, int c) { |
| 235 | return f(n, &a, &b, &c); |
| 236 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 237 | static_assert(g(23, x, y, z) == 788, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 238 | |
| 239 | } |
| 240 | |
| 241 | namespace FunctionPointers { |
| 242 | |
| 243 | constexpr int Double(int n) { return 2 * n; } |
| 244 | constexpr int Triple(int n) { return 3 * n; } |
| 245 | constexpr int Twice(int (*F)(int), int n) { return F(F(n)); } |
| 246 | constexpr int Quadruple(int n) { return Twice(Double, n); } |
| 247 | constexpr auto Select(int n) -> int (*)(int) { |
| 248 | return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0; |
| 249 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 250 | 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] | 251 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 252 | static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 253 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 254 | constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 255 | |
| 256 | } |
| 257 | |
| 258 | namespace PointerComparison { |
| 259 | |
| 260 | int x, y; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 261 | static_assert(&x == &y, "false"); // expected-error {{false}} |
| 262 | static_assert(&x != &y, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 263 | constexpr bool g1 = &x == &y; |
| 264 | constexpr bool g2 = &x != &y; |
| 265 | constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} |
| 266 | constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} |
| 267 | constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} |
| 268 | constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} |
| 269 | |
| 270 | struct S { int x, y; } s; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 271 | static_assert(&s.x == &s.y, "false"); // expected-error {{false}} |
| 272 | static_assert(&s.x != &s.y, ""); |
| 273 | static_assert(&s.x <= &s.y, ""); |
| 274 | static_assert(&s.x >= &s.y, "false"); // expected-error {{false}} |
| 275 | static_assert(&s.x < &s.y, ""); |
| 276 | static_assert(&s.x > &s.y, "false"); // expected-error {{false}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 277 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 278 | static_assert(0 == &y, "false"); // expected-error {{false}} |
| 279 | static_assert(0 != &y, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 280 | constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}} |
| 281 | constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}} |
| 282 | constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}} |
| 283 | constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}} |
| 284 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 285 | static_assert(&x == 0, "false"); // expected-error {{false}} |
| 286 | static_assert(&x != 0, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 287 | constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}} |
| 288 | constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}} |
| 289 | constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}} |
| 290 | constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}} |
| 291 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 292 | static_assert(&x == &x, ""); |
| 293 | static_assert(&x != &x, "false"); // expected-error {{false}} |
| 294 | static_assert(&x <= &x, ""); |
| 295 | static_assert(&x >= &x, ""); |
| 296 | static_assert(&x < &x, "false"); // expected-error {{false}} |
| 297 | static_assert(&x > &x, "false"); // expected-error {{false}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 298 | |
| 299 | constexpr S* sptr = &s; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 300 | 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] | 301 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 302 | struct Str { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 303 | int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 304 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 305 | expected-note {{dynamic_cast is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 306 | int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 307 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 308 | expected-note {{reinterpret_cast is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 309 | int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 310 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 60f24e7 | 2011-12-12 19:33:27 +0000 | [diff] [blame] | 311 | 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] | 312 | int d : (S*)(42) == (S*)(42); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 313 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 60f24e7 | 2011-12-12 19:33:27 +0000 | [diff] [blame] | 314 | 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] | 315 | int e : (Str*)(sptr) == (Str*)(sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 316 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 60f24e7 | 2011-12-12 19:33:27 +0000 | [diff] [blame] | 317 | 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] | 318 | int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 319 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 60f24e7 | 2011-12-12 19:33:27 +0000 | [diff] [blame] | 320 | 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] | 321 | int g : (S*)(void*)(sptr) == sptr; // \ |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 322 | expected-warning {{not an integral constant expression}} \ |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 323 | expected-note {{cast from 'void *' is not allowed in a constant expression}} |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 326 | extern char externalvar[]; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 327 | 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] | 328 | 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] | 329 | static_assert(0 != "foo", ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 330 | |
| 331 | } |
| 332 | |
| 333 | namespace MaterializeTemporary { |
| 334 | |
| 335 | constexpr int f(const int &r) { return r; } |
| 336 | constexpr int n = f(1); |
| 337 | |
| 338 | constexpr bool same(const int &a, const int &b) { return &a == &b; } |
| 339 | constexpr bool sameTemporary(const int &n) { return same(n, n); } |
| 340 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 341 | static_assert(n, ""); |
| 342 | static_assert(!same(4, 4), ""); |
| 343 | static_assert(same(n, n), ""); |
| 344 | static_assert(sameTemporary(9), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 345 | |
| 346 | } |
| 347 | |
| 348 | constexpr int strcmp_ce(const char *p, const char *q) { |
| 349 | return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1); |
| 350 | } |
| 351 | |
| 352 | namespace StringLiteral { |
| 353 | |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 354 | template<typename Char> |
| 355 | constexpr int MangleChars(const Char *p) { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 356 | return *p + 3 * (*p ? MangleChars(p+1) : 0); |
| 357 | } |
| 358 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 359 | static_assert(MangleChars("constexpr!") == 1768383, ""); |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 360 | static_assert(MangleChars(u8"constexpr!") == 1768383, ""); |
| 361 | static_assert(MangleChars(L"constexpr!") == 1768383, ""); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 362 | static_assert(MangleChars(u"constexpr!") == 1768383, ""); |
| 363 | static_assert(MangleChars(U"constexpr!") == 1768383, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 364 | |
| 365 | constexpr char c0 = "nought index"[0]; |
| 366 | constexpr char c1 = "nice index"[10]; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 367 | 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] | 368 | 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}} |
| 369 | 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] | 370 | |
| 371 | constexpr const char *p = "test" + 2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 372 | static_assert(*p == 's', ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 373 | |
| 374 | constexpr const char *max_iter(const char *a, const char *b) { |
| 375 | return *a < *b ? b : a; |
| 376 | } |
| 377 | constexpr const char *max_element(const char *a, const char *b) { |
| 378 | return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b)); |
| 379 | } |
| 380 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 381 | constexpr char str[] = "the quick brown fox jumped over the lazy dog"; |
| 382 | constexpr const char *max = max_element(begin(str), end(str)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 383 | static_assert(*max == 'z', ""); |
| 384 | static_assert(max == str + 38, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 385 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 386 | static_assert(strcmp_ce("hello world", "hello world") == 0, ""); |
| 387 | static_assert(strcmp_ce("hello world", "hello clang") > 0, ""); |
| 388 | static_assert(strcmp_ce("constexpr", "test") < 0, ""); |
| 389 | static_assert(strcmp_ce("", " ") < 0, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 390 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 391 | struct S { |
| 392 | 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}} |
| 393 | }; |
| 394 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 395 | struct T { |
| 396 | char c[6]; |
| 397 | constexpr T() : c{"foo"} {} |
| 398 | }; |
| 399 | constexpr T t; |
| 400 | |
| 401 | static_assert(t.c[0] == 'f', ""); |
| 402 | static_assert(t.c[1] == 'o', ""); |
| 403 | static_assert(t.c[2] == 'o', ""); |
| 404 | static_assert(t.c[3] == 0, ""); |
| 405 | static_assert(t.c[4] == 0, ""); |
| 406 | static_assert(t.c[5] == 0, ""); |
| 407 | static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 408 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 409 | struct U { |
| 410 | wchar_t chars[6]; |
| 411 | int n; |
| 412 | } constexpr u = { { L"test" }, 0 }; |
| 413 | static_assert(u.chars[2] == L's', ""); |
| 414 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | namespace Array { |
| 418 | |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 419 | template<typename Iter> |
| 420 | constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 421 | return begin == end ? 0 : *begin + Sum(begin+1, end); |
| 422 | } |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 423 | |
| 424 | constexpr int xs[] = { 1, 2, 3, 4, 5 }; |
| 425 | constexpr int ys[] = { 5, 4, 3, 2, 1 }; |
| 426 | constexpr int sum_xs = Sum(begin(xs), end(xs)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 427 | static_assert(sum_xs == 15, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 428 | |
| 429 | constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, |
| 430 | const int *xs, const int *ys, int c) { |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 431 | return n ? F( |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 432 | *xs, // expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 433 | *ys, |
| 434 | ZipFoldR(F, n-1, xs+1, ys+1, c)) // \ |
| 435 | expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \ |
| 436 | expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}} |
| 437 | : c; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 438 | } |
| 439 | constexpr int MulAdd(int x, int y, int c) { return x * y + c; } |
| 440 | constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 441 | static_assert(InnerProduct == 35, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 442 | |
| 443 | constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } |
| 444 | constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 445 | static_assert(DiffProd == 8, ""); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 446 | static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \ |
| 447 | expected-error {{constant expression}} \ |
| 448 | 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] | 449 | |
| 450 | constexpr const int *p = xs + 3; |
| 451 | constexpr int xs4 = p[1]; // ok |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 452 | 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] | 453 | 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] | 454 | constexpr int xs0 = p[-3]; // ok |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 455 | 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] | 456 | |
| 457 | 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] | 458 | static_assert(zs[0][0][0][0] == 1, ""); |
| 459 | static_assert(zs[1][1][1][1] == 16, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 460 | 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] | 461 | static_assert((&zs[0][0][0][2])[-1] == 2, ""); |
| 462 | static_assert(**(**(zs + 1) + 1) == 11, ""); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 463 | 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}} |
| 464 | static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, ""); |
| 465 | 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] | 466 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 467 | constexpr int fail(const int &p) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 468 | 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] | 469 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 470 | 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] | 471 | expected-error {{static_assert expression is not an integral constant expression}} \ |
| 472 | expected-note {{in call to 'fail(zs[1][0][1][0])'}} |
| 473 | |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 474 | 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] | 475 | constexpr int SumNonzero(const int *p) { |
| 476 | return *p + (*p ? SumNonzero(p+1) : 0); |
| 477 | } |
| 478 | constexpr int CountZero(const int *p, const int *q) { |
| 479 | return p == q ? 0 : (*p == 0) + CountZero(p+1, q); |
| 480 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 481 | static_assert(SumNonzero(arr) == 6, ""); |
| 482 | static_assert(CountZero(arr, arr + 40) == 36, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 483 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 484 | struct ArrayElem { |
| 485 | constexpr ArrayElem() : n(0) {} |
| 486 | int n; |
| 487 | constexpr int f() { return n; } |
| 488 | }; |
| 489 | struct ArrayRVal { |
| 490 | constexpr ArrayRVal() {} |
| 491 | ArrayElem elems[10]; |
| 492 | }; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 493 | static_assert(ArrayRVal().elems[3].f() == 0, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 494 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | namespace DependentValues { |
| 498 | |
| 499 | struct I { int n; typedef I V[10]; }; |
| 500 | I::V x, y; |
| 501 | template<bool B> struct S { |
| 502 | int k; |
| 503 | void f() { |
| 504 | I::V &cells = B ? x : y; |
| 505 | I &i = cells[k]; |
| 506 | switch (i.n) {} |
| 507 | } |
| 508 | }; |
| 509 | |
| 510 | } |
| 511 | |
| 512 | namespace Class { |
| 513 | |
| 514 | struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; |
| 515 | constexpr int fn(const A &a) { return a.k; } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 516 | static_assert(fn(A(4,5)) == 9, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 517 | |
| 518 | struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}} |
| 519 | struct C { |
| 520 | constexpr C(C *this_) : m(42), n(this_->m) {} // ok |
| 521 | int m, n; |
| 522 | }; |
| 523 | struct D { |
| 524 | C c; |
| 525 | constexpr D() : c(&c) {} |
| 526 | }; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 527 | static_assert(D().c.n == 42, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 528 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 529 | struct E { |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 530 | constexpr E() : p(&p) {} // expected-note {{pointer to subobject of temporary cannot be used to initialize a member in a constant expression}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 531 | void *p; |
| 532 | }; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 533 | constexpr const E &e1 = E(); // expected-error {{constant expression}} expected-note {{in call to 'E()'}} expected-note {{temporary created here}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 534 | // This is a constant expression if we elide the copy constructor call, and |
| 535 | // 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] | 536 | constexpr E e2 = E(); |
| 537 | static_assert(e2.p == &e2.p, ""); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 538 | constexpr E e3; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 539 | static_assert(e3.p == &e3.p, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 540 | |
| 541 | extern const class F f; |
| 542 | struct F { |
| 543 | constexpr F() : p(&f.p) {} |
| 544 | const void *p; |
| 545 | }; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 546 | constexpr F f; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 547 | |
| 548 | struct G { |
| 549 | struct T { |
| 550 | constexpr T(T *p) : u1(), u2(p) {} |
| 551 | union U1 { |
| 552 | constexpr U1() {} |
| 553 | int a, b = 42; |
| 554 | } u1; |
| 555 | union U2 { |
| 556 | constexpr U2(T *p) : c(p->u1.b) {} |
| 557 | int c, d; |
| 558 | } u2; |
| 559 | } t; |
| 560 | constexpr G() : t(&t) {} |
| 561 | } constexpr g; |
| 562 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 563 | 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] | 564 | static_assert(g.t.u1.b == 42, ""); |
| 565 | static_assert(g.t.u2.c == 42, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 566 | 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] | 567 | |
| 568 | struct S { |
| 569 | int a, b; |
| 570 | const S *p; |
| 571 | double d; |
| 572 | const char *q; |
| 573 | |
| 574 | constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} |
| 575 | }; |
| 576 | |
| 577 | S global(43, &global); |
| 578 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 579 | static_assert(S(15, &global).b == 15, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 580 | |
| 581 | constexpr bool CheckS(const S &s) { |
| 582 | return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; |
| 583 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 584 | static_assert(CheckS(S(27, &global)), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 585 | |
| 586 | struct Arr { |
| 587 | char arr[3]; |
| 588 | constexpr Arr() : arr{'x', 'y', 'z'} {} |
| 589 | }; |
| 590 | constexpr int hash(Arr &&a) { |
| 591 | return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; |
| 592 | } |
| 593 | constexpr int k = hash(Arr()); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 594 | static_assert(k == 0x007a7978, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 595 | |
| 596 | |
| 597 | struct AggregateInit { |
| 598 | const char &c; |
| 599 | int n; |
| 600 | double d; |
| 601 | int arr[5]; |
| 602 | void *p; |
| 603 | }; |
| 604 | |
| 605 | constexpr AggregateInit agg1 = { "hello"[0] }; |
| 606 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 607 | static_assert(strcmp_ce(&agg1.c, "hello") == 0, ""); |
| 608 | static_assert(agg1.n == 0, ""); |
| 609 | static_assert(agg1.d == 0.0, ""); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 610 | 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] | 611 | static_assert(agg1.arr[0] == 0, ""); |
| 612 | static_assert(agg1.arr[4] == 0, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 613 | 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] | 614 | static_assert(agg1.p == nullptr, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 615 | |
| 616 | namespace SimpleDerivedClass { |
| 617 | |
| 618 | struct B { |
| 619 | constexpr B(int n) : a(n) {} |
| 620 | int a; |
| 621 | }; |
| 622 | struct D : B { |
| 623 | constexpr D(int n) : B(n) {} |
| 624 | }; |
| 625 | constexpr D d(3); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 626 | static_assert(d.a == 3, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 627 | |
| 628 | } |
| 629 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 630 | struct Bottom { constexpr Bottom() {} }; |
| 631 | struct Base : Bottom { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 632 | constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} |
| 633 | int a; |
| 634 | const char *b; |
| 635 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 636 | struct Base2 : Bottom { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 637 | constexpr Base2(const int &r) : r(r) {} |
| 638 | int q = 123; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 639 | const int &r; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 640 | }; |
| 641 | struct Derived : Base, Base2 { |
| 642 | constexpr Derived() : Base(76), Base2(a) {} |
| 643 | int c = r + b[1]; |
| 644 | }; |
| 645 | |
| 646 | constexpr bool operator==(const Base &a, const Base &b) { |
| 647 | return a.a == b.a && strcmp_ce(a.b, b.b) == 0; |
| 648 | } |
| 649 | |
| 650 | constexpr Base base; |
| 651 | constexpr Base base2(76); |
| 652 | constexpr Derived derived; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 653 | static_assert(derived.a == 76, ""); |
| 654 | static_assert(derived.b[2] == 's', ""); |
| 655 | static_assert(derived.c == 76 + 'e', ""); |
| 656 | static_assert(derived.q == 123, ""); |
| 657 | static_assert(derived.r == 76, ""); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 658 | static_assert(&derived.r == &derived.a, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 659 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 660 | static_assert(!(derived == base), ""); |
| 661 | static_assert(derived == base2, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 662 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 663 | constexpr Bottom &bot1 = (Base&)derived; |
| 664 | constexpr Bottom &bot2 = (Base2&)derived; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 665 | static_assert(&bot1 != &bot2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 666 | |
| 667 | constexpr Bottom *pb1 = (Base*)&derived; |
| 668 | constexpr Bottom *pb2 = (Base2*)&derived; |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame^] | 669 | static_assert(&pb1 != &pb2, ""); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 670 | static_assert(pb1 == &bot1, ""); |
| 671 | static_assert(pb2 == &bot2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 672 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 673 | constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}} |
| 674 | 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] | 675 | constexpr Base2 &ok2 = (Base2&)bot2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 676 | static_assert(&ok2 == &derived, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 677 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 678 | constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}} |
| 679 | 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] | 680 | constexpr Base2 *pok2 = (Base2*)pb2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 681 | static_assert(pok2 == &derived, ""); |
| 682 | static_assert(&ok2 == pok2, ""); |
| 683 | static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, ""); |
| 684 | static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 685 | |
| 686 | constexpr Base *nullB = 42 - 6 * 7; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 687 | static_assert((Bottom*)nullB == 0, ""); |
| 688 | static_assert((Derived*)nullB == 0, ""); |
| 689 | static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 690 | |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 691 | namespace ConversionOperators { |
| 692 | |
| 693 | struct T { |
| 694 | constexpr T(int n) : k(5*n - 3) {} |
| 695 | constexpr operator int() { return k; } |
| 696 | int k; |
| 697 | }; |
| 698 | |
| 699 | struct S { |
| 700 | constexpr S(int n) : k(2*n + 1) {} |
| 701 | constexpr operator int() { return k; } |
| 702 | constexpr operator T() { return T(k); } |
| 703 | int k; |
| 704 | }; |
| 705 | |
| 706 | constexpr bool check(T a, T b) { return a == b.k; } |
| 707 | |
| 708 | static_assert(S(5) == 11, ""); |
| 709 | static_assert(check(S(5), 11), ""); |
| 710 | |
| 711 | } |
| 712 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | namespace Temporaries { |
| 716 | |
| 717 | struct S { |
| 718 | constexpr S() {} |
| 719 | constexpr int f(); |
| 720 | }; |
| 721 | struct T : S { |
| 722 | constexpr T(int n) : S(), n(n) {} |
| 723 | int n; |
| 724 | }; |
| 725 | constexpr int S::f() { |
| 726 | // 'this' must be the postfix-expression in a class member access expression, |
| 727 | // so we can't just use |
| 728 | // return static_cast<T*>(this)->n; |
| 729 | return this->*(int(S::*))&T::n; |
| 730 | } |
| 731 | // The T temporary is implicitly cast to an S subobject, but we can recover the |
| 732 | // T full-object via a base-to-derived cast, or a derived-to-base-casted member |
| 733 | // pointer. |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 734 | static_assert(T(3).f() == 3, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 735 | |
| 736 | constexpr int f(const S &s) { |
| 737 | return static_cast<const T&>(s).n; |
| 738 | } |
| 739 | constexpr int n = f(T(5)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 740 | static_assert(f(T(5)) == 5, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 741 | |
Richard Smith | f2e4cd7 | 2012-01-26 04:47:34 +0000 | [diff] [blame] | 742 | constexpr bool b(int n) { return &n; } |
| 743 | static_assert(b(0), ""); |
| 744 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | namespace Union { |
| 748 | |
| 749 | union U { |
| 750 | int a; |
| 751 | int b; |
| 752 | }; |
| 753 | |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 754 | 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] | 755 | static_assert(u[0].a == 0, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 756 | 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] | 757 | static_assert(u[1].b == 1, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 758 | 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] | 759 | 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] | 760 | static_assert((&(u[1]) + 1 + 1)->b == 3, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 761 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 762 | constexpr U v = {}; |
| 763 | static_assert(v.a == 0, ""); |
| 764 | |
| 765 | union Empty {}; |
| 766 | constexpr Empty e = {}; |
| 767 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 768 | // Make sure we handle trivial copy constructors for unions. |
| 769 | constexpr U x = {42}; |
| 770 | constexpr U y = x; |
| 771 | static_assert(y.a == 42, ""); |
| 772 | static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}} |
| 773 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 776 | namespace MemberPointer { |
| 777 | struct A { |
| 778 | constexpr A(int n) : n(n) {} |
| 779 | int n; |
| 780 | constexpr int f() { return n + 3; } |
| 781 | }; |
| 782 | constexpr A a(7); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 783 | static_assert(A(5).*&A::n == 5, ""); |
| 784 | static_assert((&a)->*&A::n == 7, ""); |
| 785 | static_assert((A(8).*&A::f)() == 11, ""); |
| 786 | static_assert(((&a)->*&A::f)() == 10, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 787 | |
| 788 | struct B : A { |
| 789 | constexpr B(int n, int m) : A(n), m(m) {} |
| 790 | int m; |
| 791 | constexpr int g() { return n + m + 1; } |
| 792 | }; |
| 793 | constexpr B b(9, 13); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 794 | static_assert(B(4, 11).*&A::n == 4, ""); |
| 795 | static_assert(B(4, 11).*&B::m == 11, ""); |
| 796 | static_assert(B(4, 11).*(int(A::*))&B::m == 11, ""); |
| 797 | static_assert((&b)->*&A::n == 9, ""); |
| 798 | static_assert((&b)->*&B::m == 13, ""); |
| 799 | static_assert((&b)->*(int(A::*))&B::m == 13, ""); |
| 800 | static_assert((B(4, 11).*&A::f)() == 7, ""); |
| 801 | static_assert((B(4, 11).*&B::g)() == 16, ""); |
| 802 | static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); |
| 803 | static_assert(((&b)->*&A::f)() == 12, ""); |
| 804 | static_assert(((&b)->*&B::g)() == 23, ""); |
| 805 | static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 806 | |
| 807 | struct S { |
| 808 | constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : |
| 809 | m(m), n(n), pf(pf), pn(pn) {} |
| 810 | constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} |
| 811 | |
| 812 | constexpr int f() { return this->*pn; } |
| 813 | virtual int g() const; |
| 814 | |
| 815 | int m, n; |
| 816 | int (S::*pf)() const; |
| 817 | int S::*pn; |
| 818 | }; |
| 819 | |
| 820 | constexpr int S::*pm = &S::m; |
| 821 | constexpr int S::*pn = &S::n; |
| 822 | constexpr int (S::*pf)() const = &S::f; |
| 823 | constexpr int (S::*pg)() const = &S::g; |
| 824 | |
| 825 | constexpr S s(2, 5, &S::f, &S::m); |
| 826 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 827 | static_assert((s.*&S::f)() == 2, ""); |
| 828 | static_assert((s.*s.pf)() == 2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 829 | |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 830 | static_assert(pf == &S::f, ""); |
| 831 | static_assert(pf == s.*&S::pf, ""); |
| 832 | static_assert(pm == &S::m, ""); |
| 833 | static_assert(pm != pn, ""); |
| 834 | static_assert(s.pn != pn, ""); |
| 835 | static_assert(s.pn == pm, ""); |
| 836 | static_assert(pg != nullptr, ""); |
| 837 | static_assert(pf != nullptr, ""); |
| 838 | static_assert((int S::*)nullptr == nullptr, ""); |
| 839 | static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} |
| 840 | static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} |
| 841 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 842 | template<int n> struct T : T<n-1> {}; |
| 843 | template<> struct T<0> { int n; }; |
| 844 | template<> struct T<30> : T<29> { int m; }; |
| 845 | |
| 846 | T<17> t17; |
| 847 | T<30> t30; |
| 848 | |
| 849 | constexpr int (T<10>::*deepn) = &T<0>::n; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 850 | static_assert(&(t17.*deepn) == &t17.n, ""); |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 851 | static_assert(deepn == &T<2>::n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 852 | |
| 853 | constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; |
| 854 | constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 855 | static_assert(&(t30.*deepm) == &t30.m, ""); |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 856 | static_assert(deepm == &T<50>::m, ""); |
| 857 | static_assert(deepm != deepn, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 858 | |
| 859 | constexpr T<5> *p17_5 = &t17; |
| 860 | constexpr T<13> *p17_13 = (T<13>*)p17_5; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 861 | 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] | 862 | static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); |
| 863 | static_assert(&(p17_13->*deepn) == &t17.n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 864 | constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} |
| 865 | |
| 866 | constexpr T<5> *p30_5 = &t30; |
| 867 | constexpr T<23> *p30_23 = (T<23>*)p30_5; |
| 868 | constexpr T<13> *p30_13 = p30_23; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 869 | static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); |
| 870 | static_assert(&(p30_13->*deepn) == &t30.n, ""); |
| 871 | static_assert(&(p30_23->*deepn) == &t30.n, ""); |
| 872 | static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); |
| 873 | static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); |
| 874 | static_assert(&(p30_23->*deepm) == &t30.m, ""); |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 875 | |
| 876 | struct Base { int n; }; |
| 877 | template<int N> struct Mid : Base {}; |
| 878 | struct Derived : Mid<0>, Mid<1> {}; |
| 879 | static_assert(&Mid<0>::n == &Mid<1>::n, ""); |
| 880 | static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n != |
| 881 | (int Derived::*)(int Mid<1>::*)&Mid<1>::n, ""); |
| 882 | static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | namespace ArrayBaseDerived { |
| 886 | |
| 887 | struct Base { |
| 888 | constexpr Base() {} |
| 889 | int n = 0; |
| 890 | }; |
| 891 | struct Derived : Base { |
| 892 | constexpr Derived() {} |
| 893 | constexpr const int *f() { return &n; } |
| 894 | }; |
| 895 | |
| 896 | constexpr Derived a[10]; |
| 897 | constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); |
| 898 | constexpr Base *pb3 = const_cast<Derived*>(&a[3]); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 899 | static_assert(pb3 == pd3, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 900 | |
| 901 | // pb3 does not point to an array element. |
| 902 | constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 903 | constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}} |
| 904 | constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}} |
| 905 | constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}} |
| 906 | constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}} |
| 907 | 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] | 908 | constexpr Base *pb3a = pb4 - 1; |
| 909 | |
| 910 | // pb4 does not point to a Derived. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 911 | 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] | 912 | constexpr Derived *pd3a = (Derived*)pb3a; |
| 913 | constexpr int pd3n = pd3a->n; |
| 914 | |
| 915 | // pd3a still points to the Derived array. |
| 916 | constexpr Derived *pd6 = pd3a + 3; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 917 | static_assert(pd6 == &a[6], ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 918 | constexpr Derived *pd9 = pd6 + 3; |
| 919 | constexpr Derived *pd10 = pd6 + 4; |
| 920 | constexpr int pd9n = pd9->n; // ok |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 921 | 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] | 922 | constexpr int pd0n = pd10[-10].n; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 923 | 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] | 924 | |
| 925 | constexpr Base *pb9 = pd9; |
| 926 | constexpr const int *(Base::*pfb)() const = |
| 927 | static_cast<const int *(Base::*)() const>(&Derived::f); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 928 | static_assert((pb9->*pfb)() == &a[9].n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 931 | namespace Complex { |
| 932 | |
| 933 | class complex { |
| 934 | int re, im; |
| 935 | public: |
| 936 | constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} |
| 937 | constexpr complex(const complex &o) : re(o.re), im(o.im) {} |
| 938 | constexpr complex operator-() const { return complex(-re, -im); } |
| 939 | friend constexpr complex operator+(const complex &l, const complex &r) { |
| 940 | return complex(l.re + r.re, l.im + r.im); |
| 941 | } |
| 942 | friend constexpr complex operator-(const complex &l, const complex &r) { |
| 943 | return l + -r; |
| 944 | } |
| 945 | friend constexpr complex operator*(const complex &l, const complex &r) { |
| 946 | return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); |
| 947 | } |
| 948 | friend constexpr bool operator==(const complex &l, const complex &r) { |
| 949 | return l.re == r.re && l.im == r.im; |
| 950 | } |
| 951 | constexpr bool operator!=(const complex &r) const { |
| 952 | return re != r.re || im != r.im; |
| 953 | } |
| 954 | constexpr int real() const { return re; } |
| 955 | constexpr int imag() const { return im; } |
| 956 | }; |
| 957 | |
| 958 | constexpr complex i = complex(0, 1); |
| 959 | constexpr complex k = (3 + 4*i) * (6 - 4*i); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 960 | static_assert(complex(1,0).real() == 1, ""); |
| 961 | static_assert(complex(1,0).imag() == 0, ""); |
| 962 | static_assert(((complex)1).imag() == 0, ""); |
| 963 | static_assert(k.real() == 34, ""); |
| 964 | static_assert(k.imag() == 12, ""); |
| 965 | static_assert(k - 34 == 12*i, ""); |
| 966 | static_assert((complex)1 == complex(1), ""); |
| 967 | static_assert((complex)1 != complex(0, 1), ""); |
| 968 | static_assert(complex(1) == complex(1), ""); |
| 969 | static_assert(complex(1) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 970 | constexpr complex makeComplex(int re, int im) { return complex(re, im); } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 971 | static_assert(makeComplex(1,0) == complex(1), ""); |
| 972 | static_assert(makeComplex(1,0) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 973 | |
| 974 | class complex_wrap : public complex { |
| 975 | public: |
| 976 | constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} |
| 977 | constexpr complex_wrap(const complex_wrap &o) : complex(o) {} |
| 978 | }; |
| 979 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 980 | static_assert((complex_wrap)1 == complex(1), ""); |
| 981 | static_assert((complex)1 != complex_wrap(0, 1), ""); |
| 982 | static_assert(complex(1) == complex_wrap(1), ""); |
| 983 | static_assert(complex_wrap(1) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 984 | constexpr complex_wrap makeComplexWrap(int re, int im) { |
| 985 | return complex_wrap(re, im); |
| 986 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 987 | static_assert(makeComplexWrap(1,0) == complex(1), ""); |
| 988 | static_assert(makeComplexWrap(1,0) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 989 | |
| 990 | } |
Eli Friedman | f59ff8c | 2011-12-17 02:24:21 +0000 | [diff] [blame] | 991 | |
| 992 | namespace PR11595 { |
| 993 | struct A { constexpr bool operator==(int x) { return true; } }; |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 994 | struct B { B(); A& x; }; |
| 995 | static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} |
| 996 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 997 | 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] | 998 | return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} |
| 999 | } |
Eli Friedman | f59ff8c | 2011-12-17 02:24:21 +0000 | [diff] [blame] | 1000 | } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 1001 | |
| 1002 | namespace ExprWithCleanups { |
| 1003 | struct A { A(); ~A(); int get(); }; |
| 1004 | constexpr int get(bool FromA) { return FromA ? A().get() : 1; } |
| 1005 | constexpr int n = get(false); |
| 1006 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1007 | |
| 1008 | namespace Volatile { |
| 1009 | |
| 1010 | volatile constexpr int n1 = 0; // expected-note {{here}} |
| 1011 | volatile const int n2 = 0; // expected-note {{here}} |
| 1012 | int n3 = 37; // expected-note {{declared here}} |
| 1013 | |
| 1014 | constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}} |
| 1015 | constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}} |
| 1016 | |
| 1017 | struct T { int n; }; |
| 1018 | const T t = { 42 }; // expected-note {{declared here}} |
| 1019 | |
| 1020 | constexpr int f(volatile int &&r) { |
| 1021 | return r; // expected-note {{read of volatile temporary is not allowed in a constant expression}} |
| 1022 | } |
| 1023 | struct S { |
| 1024 | int k : f(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'f(0)'}} |
| 1025 | int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}} |
| 1026 | int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}} |
| 1027 | }; |
| 1028 | |
| 1029 | } |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 1030 | |
| 1031 | namespace ExternConstexpr { |
| 1032 | extern constexpr int n = 0; |
| 1033 | extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}} |
| 1034 | void f() { |
| 1035 | extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}} |
| 1036 | constexpr int j = 0; |
| 1037 | constexpr int k; // expected-error {{default initialization of an object of const type}} |
| 1038 | } |
| 1039 | } |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 1040 | |
| 1041 | namespace ComplexConstexpr { |
| 1042 | constexpr _Complex float test1 = {}; |
| 1043 | constexpr _Complex float test2 = {1}; |
| 1044 | constexpr _Complex double test3 = {1,2}; |
| 1045 | constexpr _Complex int test4 = {4}; |
| 1046 | constexpr _Complex int test5 = 4; |
| 1047 | constexpr _Complex int test6 = {5,6}; |
Eli Friedman | f6c17a4 | 2012-01-13 23:34:56 +0000 | [diff] [blame] | 1048 | typedef _Complex float fcomplex; |
| 1049 | constexpr fcomplex test7 = fcomplex(); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 1050 | } |
Eli Friedman | 6b3014b | 2012-01-18 02:54:10 +0000 | [diff] [blame] | 1051 | |
| 1052 | namespace InstantiateCaseStmt { |
| 1053 | template<int x> constexpr int f() { return x; } |
| 1054 | template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; } |
| 1055 | int gg(int c) { return g<4>(c); } |
| 1056 | } |
Richard Smith | 495f42a | 2012-01-24 05:40:50 +0000 | [diff] [blame] | 1057 | |
| 1058 | namespace ConvertedConstantExpr { |
| 1059 | extern int &m; |
| 1060 | extern int &n; |
| 1061 | |
| 1062 | constexpr int k = 4; |
| 1063 | int &m = const_cast<int&>(k); |
| 1064 | |
| 1065 | // If we have nothing more interesting to say, ensure we don't produce a |
| 1066 | // useless note and instead just point to the non-constant subexpression. |
| 1067 | enum class E { |
| 1068 | em = m, |
| 1069 | en = n, // expected-error {{not a constant expression}} |
| 1070 | eo = (m + |
| 1071 | n // expected-error {{not a constant expression}} |
| 1072 | ), |
| 1073 | eq = reinterpret_cast<int>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}} |
| 1074 | }; |
| 1075 | } |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1076 | |
| 1077 | namespace IndirectField { |
| 1078 | struct S { |
| 1079 | struct { // expected-warning {{GNU extension}} |
| 1080 | union { |
| 1081 | struct { // expected-warning {{GNU extension}} |
| 1082 | int a; |
| 1083 | int b; |
| 1084 | }; |
| 1085 | int c; |
| 1086 | }; |
| 1087 | int d; |
| 1088 | }; |
| 1089 | union { |
| 1090 | int e; |
| 1091 | int f; |
| 1092 | }; |
| 1093 | constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {} |
| 1094 | constexpr S(int c, int d, int f) : c(c), d(d), f(f) {} |
| 1095 | }; |
| 1096 | |
| 1097 | constexpr S s1(1, 2, 3, 4); |
| 1098 | constexpr S s2(5, 6, 7); |
| 1099 | |
| 1100 | // FIXME: The diagnostics here do a very poor job of explaining which unnamed |
| 1101 | // member is active and which is requested. |
| 1102 | static_assert(s1.a == 1, ""); |
| 1103 | static_assert(s1.b == 2, ""); |
| 1104 | static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1105 | static_assert(s1.d == 3, ""); |
| 1106 | static_assert(s1.e == 4, ""); |
| 1107 | static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1108 | |
| 1109 | static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1110 | static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1111 | static_assert(s2.c == 5, ""); |
| 1112 | static_assert(s2.d == 6, ""); |
| 1113 | static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1114 | static_assert(s2.f == 7, ""); |
| 1115 | } |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame^] | 1116 | |
| 1117 | namespace Fold { |
| 1118 | |
| 1119 | // This macro forces its argument to be constant-folded, even if it's not |
| 1120 | // otherwise a constant expression. |
| 1121 | #define fold(x) (__builtin_constant_p(x) ? (x) : (x)) |
| 1122 | |
| 1123 | constexpr int n = (int)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}} |
| 1124 | constexpr int m = fold((int)(char*)123); // ok |
| 1125 | static_assert(m == 123, ""); |
| 1126 | |
| 1127 | #undef fold |
| 1128 | |
| 1129 | } |