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 | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 40 | static_assert((void*)p != (void*)q, ""); |
| 41 | static_assert((A*)p == (A*)q, ""); |
| 42 | static_assert((Aa*)p != (Aa*)q, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 43 | |
| 44 | constexpr B &pp = d; |
| 45 | constexpr C &qq = d; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 46 | static_assert((void*)&pp != (void*)&qq, ""); |
| 47 | static_assert(&(A&)pp == &(A&)qq, ""); |
| 48 | static_assert(&(Aa&)pp != &(Aa&)qq, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 49 | |
| 50 | constexpr V *v = p; |
| 51 | constexpr V *w = q; |
| 52 | constexpr V *x = (A*)p; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 53 | static_assert(v == w, ""); |
| 54 | static_assert(v == x, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 55 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 56 | static_assert((U*)&d == p, ""); |
| 57 | static_assert((U*)&d == q, ""); |
| 58 | static_assert((U*)&d == v, ""); |
| 59 | static_assert((U*)&d == w, ""); |
| 60 | static_assert((U*)&d == x, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 61 | |
| 62 | struct X {}; |
| 63 | struct Y1 : virtual X {}; |
| 64 | struct Y2 : X {}; |
| 65 | struct Z : Y1, Y2 {}; |
| 66 | Z z; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 67 | static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 68 | |
| 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) { |
| 95 | // FIXME: Produce the 'add ()' fixit for this. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 96 | case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}} expected-note {{non-literal type '<bound member function type>'}} |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 97 | case id(1): |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | extern int &Recurse1; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 104 | int &Recurse2 = Recurse1; // expected-note 2{{declared here}} expected-note {{initializer of 'Recurse1' is not a constant expression}} |
| 105 | int &Recurse1 = Recurse2; // expected-note {{declared here}} expected-note {{initializer of 'Recurse2' is not a constant expression}} |
| 106 | constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}} |
| 107 | |
| 108 | extern const int RecurseA; |
| 109 | const int RecurseB = RecurseA; // expected-note {{declared here}} |
| 110 | const int RecurseA = 10; |
| 111 | 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] | 112 | |
| 113 | namespace MemberEnum { |
| 114 | struct WithMemberEnum { |
| 115 | enum E { A = 42 }; |
| 116 | } wme; |
| 117 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 118 | static_assert(wme.A == 42, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | namespace DefaultArguments { |
| 122 | |
| 123 | const int z = int(); |
| 124 | constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) { |
| 125 | return a + b + *c + d; |
| 126 | } |
| 127 | const int four = 4; |
| 128 | constexpr int eight = 8; |
| 129 | constexpr const int twentyseven = 27; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 130 | static_assert(Sum() == 0, ""); |
| 131 | static_assert(Sum(1) == 1, ""); |
| 132 | static_assert(Sum(1, four) == 5, ""); |
| 133 | static_assert(Sum(1, eight, &twentyseven) == 36, ""); |
| 134 | static_assert(Sum(1, 2, &four, eight) == 15, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 135 | |
| 136 | } |
| 137 | |
| 138 | namespace Ellipsis { |
| 139 | |
| 140 | // Note, values passed through an ellipsis can't actually be used. |
| 141 | constexpr int F(int a, ...) { return a; } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 142 | static_assert(F(0) == 0, ""); |
| 143 | static_assert(F(1, 0) == 1, ""); |
| 144 | static_assert(F(2, "test") == 2, ""); |
| 145 | static_assert(F(3, &F) == 3, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 146 | int k = 0; // expected-note {{here}} |
| 147 | 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] | 148 | |
| 149 | } |
| 150 | |
| 151 | namespace Recursion { |
| 152 | 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] | 153 | static_assert(fib(11) == 89, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 154 | |
| 155 | constexpr int gcd_inner(int a, int b) { |
| 156 | return b == 0 ? a : gcd_inner(b, a % b); |
| 157 | } |
| 158 | constexpr int gcd(int a, int b) { |
| 159 | return gcd_inner(max(a, b), min(a, b)); |
| 160 | } |
| 161 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 162 | static_assert(gcd(1749237, 5628959) == 7, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | namespace FunctionCast { |
| 166 | // When folding, we allow functions to be cast to different types. Such |
| 167 | // cast functions cannot be called, even if they're constexpr. |
| 168 | constexpr int f() { return 1; } |
| 169 | typedef double (*DoubleFn)(); |
| 170 | typedef int (*IntFn)(); |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame^] | 171 | 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] | 172 | int b[(int)IntFn(f)()]; // ok |
| 173 | } |
| 174 | |
| 175 | namespace StaticMemberFunction { |
| 176 | struct S { |
| 177 | static constexpr int k = 42; |
| 178 | static constexpr int f(int n) { return n * k + 2; } |
| 179 | } s; |
| 180 | |
| 181 | constexpr int n = s.f(19); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 182 | static_assert(S::f(19) == 800, ""); |
| 183 | static_assert(s.f(19) == 800, ""); |
| 184 | static_assert(n == 800, ""); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 185 | |
| 186 | constexpr int (*sf1)(int) = &S::f; |
| 187 | constexpr int (*sf2)(int) = &s.f; |
| 188 | constexpr const int *sk = &s.k; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | namespace ParameterScopes { |
| 192 | |
| 193 | const int k = 42; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 194 | constexpr const int &ObscureTheTruth(const int &a) { return a; } // expected-note 3{{reference to 'a' cannot be returned from a constexpr function}} |
| 195 | constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}} |
| 196 | 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] | 197 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 198 | static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 199 | 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] | 200 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 201 | constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 202 | // If ObscureTheTruth returns a reference to 'a', the result is not a |
| 203 | // constant expression even though 'a' is still in scope. |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 204 | return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 205 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 206 | static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 207 | 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] | 208 | |
| 209 | constexpr int InternalReturnJunk(int n) { |
| 210 | // FIXME: We should reject this: it never produces a constant expression. |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 211 | return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 212 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 213 | 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] | 214 | |
| 215 | constexpr int LToR(int &n) { return n; } |
| 216 | constexpr int GrabCallersArgument(bool which, int a, int b) { |
| 217 | return LToR(which ? b : a); |
| 218 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 219 | static_assert(GrabCallersArgument(false, 1, 2) == 1, ""); |
| 220 | static_assert(GrabCallersArgument(true, 4, 8) == 8, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 221 | |
| 222 | } |
| 223 | |
| 224 | namespace Pointers { |
| 225 | |
| 226 | constexpr int f(int n, const int *a, const int *b, const int *c) { |
| 227 | return n == 0 ? 0 : *a + f(n-1, b, c, a); |
| 228 | } |
| 229 | |
| 230 | const int x = 1, y = 10, z = 100; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 231 | static_assert(f(23, &x, &y, &z) == 788, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 232 | |
| 233 | constexpr int g(int n, int a, int b, int c) { |
| 234 | return f(n, &a, &b, &c); |
| 235 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 236 | static_assert(g(23, x, y, z) == 788, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 237 | |
| 238 | } |
| 239 | |
| 240 | namespace FunctionPointers { |
| 241 | |
| 242 | constexpr int Double(int n) { return 2 * n; } |
| 243 | constexpr int Triple(int n) { return 3 * n; } |
| 244 | constexpr int Twice(int (*F)(int), int n) { return F(F(n)); } |
| 245 | constexpr int Quadruple(int n) { return Twice(Double, n); } |
| 246 | constexpr auto Select(int n) -> int (*)(int) { |
| 247 | return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0; |
| 248 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 249 | 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] | 250 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 251 | static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 252 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 253 | 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] | 254 | |
| 255 | } |
| 256 | |
| 257 | namespace PointerComparison { |
| 258 | |
| 259 | int x, y; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 260 | static_assert(&x == &y, "false"); // expected-error {{false}} |
| 261 | static_assert(&x != &y, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 262 | constexpr bool g1 = &x == &y; |
| 263 | constexpr bool g2 = &x != &y; |
| 264 | constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} |
| 265 | constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} |
| 266 | constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} |
| 267 | constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} |
| 268 | |
| 269 | struct S { int x, y; } s; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 270 | static_assert(&s.x == &s.y, "false"); // expected-error {{false}} |
| 271 | static_assert(&s.x != &s.y, ""); |
| 272 | static_assert(&s.x <= &s.y, ""); |
| 273 | static_assert(&s.x >= &s.y, "false"); // expected-error {{false}} |
| 274 | static_assert(&s.x < &s.y, ""); |
| 275 | static_assert(&s.x > &s.y, "false"); // expected-error {{false}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 276 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 277 | static_assert(0 == &y, "false"); // expected-error {{false}} |
| 278 | static_assert(0 != &y, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 279 | constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}} |
| 280 | constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}} |
| 281 | constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}} |
| 282 | constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}} |
| 283 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 284 | static_assert(&x == 0, "false"); // expected-error {{false}} |
| 285 | static_assert(&x != 0, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 286 | constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}} |
| 287 | constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}} |
| 288 | constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}} |
| 289 | constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}} |
| 290 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 291 | static_assert(&x == &x, ""); |
| 292 | static_assert(&x != &x, "false"); // expected-error {{false}} |
| 293 | static_assert(&x <= &x, ""); |
| 294 | static_assert(&x >= &x, ""); |
| 295 | static_assert(&x < &x, "false"); // expected-error {{false}} |
| 296 | static_assert(&x > &x, "false"); // expected-error {{false}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 297 | |
| 298 | constexpr S* sptr = &s; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 299 | 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] | 300 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 301 | struct Str { |
| 302 | // FIXME: In C++ mode, we should say 'integral' not 'integer' |
| 303 | int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \ |
| 304 | expected-warning {{not integer 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); // \ |
| 307 | expected-warning {{not integer 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); // \ |
| 310 | expected-warning {{not integer 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); // \ |
| 313 | expected-warning {{not integer 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); // \ |
| 316 | expected-warning {{not integer 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); // \ |
| 319 | expected-warning {{not integer 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; // \ |
| 322 | expected-warning {{not integer 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, ""); |
| 360 | static_assert(MangleChars(u"constexpr!") == 1768383, ""); |
| 361 | static_assert(MangleChars(U"constexpr!") == 1768383, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 362 | |
| 363 | constexpr char c0 = "nought index"[0]; |
| 364 | constexpr char c1 = "nice index"[10]; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 365 | 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}} |
| 366 | // FIXME: block the pointer arithmetic with undefined behavior here |
| 367 | constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 368 | constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} |
| 369 | |
| 370 | constexpr const char *p = "test" + 2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 371 | static_assert(*p == 's', ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 372 | |
| 373 | constexpr const char *max_iter(const char *a, const char *b) { |
| 374 | return *a < *b ? b : a; |
| 375 | } |
| 376 | constexpr const char *max_element(const char *a, const char *b) { |
| 377 | return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b)); |
| 378 | } |
| 379 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 380 | constexpr char str[] = "the quick brown fox jumped over the lazy dog"; |
| 381 | constexpr const char *max = max_element(begin(str), end(str)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 382 | static_assert(*max == 'z', ""); |
| 383 | static_assert(max == str + 38, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 384 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 385 | static_assert(strcmp_ce("hello world", "hello world") == 0, ""); |
| 386 | static_assert(strcmp_ce("hello world", "hello clang") > 0, ""); |
| 387 | static_assert(strcmp_ce("constexpr", "test") < 0, ""); |
| 388 | static_assert(strcmp_ce("", " ") < 0, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 389 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 390 | struct S { |
| 391 | 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}} |
| 392 | }; |
| 393 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 394 | struct T { |
| 395 | char c[6]; |
| 396 | constexpr T() : c{"foo"} {} |
| 397 | }; |
| 398 | constexpr T t; |
| 399 | |
| 400 | static_assert(t.c[0] == 'f', ""); |
| 401 | static_assert(t.c[1] == 'o', ""); |
| 402 | static_assert(t.c[2] == 'o', ""); |
| 403 | static_assert(t.c[3] == 0, ""); |
| 404 | static_assert(t.c[4] == 0, ""); |
| 405 | static_assert(t.c[5] == 0, ""); |
| 406 | static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 407 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | namespace Array { |
| 411 | |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 412 | template<typename Iter> |
| 413 | constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 414 | return begin == end ? 0 : *begin + Sum(begin+1, end); |
| 415 | } |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 416 | |
| 417 | constexpr int xs[] = { 1, 2, 3, 4, 5 }; |
| 418 | constexpr int ys[] = { 5, 4, 3, 2, 1 }; |
| 419 | constexpr int sum_xs = Sum(begin(xs), end(xs)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 420 | static_assert(sum_xs == 15, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 421 | |
| 422 | constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, |
| 423 | const int *xs, const int *ys, int c) { |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 424 | return n ? F( |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 425 | *xs, // expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 426 | *ys, |
| 427 | ZipFoldR(F, n-1, xs+1, ys+1, c)) // \ |
| 428 | expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \ |
| 429 | expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}} |
| 430 | : c; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 431 | } |
| 432 | constexpr int MulAdd(int x, int y, int c) { return x * y + c; } |
| 433 | constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 434 | static_assert(InnerProduct == 35, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 435 | |
| 436 | constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } |
| 437 | constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 438 | static_assert(DiffProd == 8, ""); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 439 | static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \ |
| 440 | expected-error {{constant expression}} \ |
| 441 | 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] | 442 | |
| 443 | constexpr const int *p = xs + 3; |
| 444 | constexpr int xs4 = p[1]; // ok |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 445 | constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 446 | constexpr int xs0 = p[-3]; // ok |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 447 | // FIXME: check pointer arithmetic here |
| 448 | constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 449 | |
| 450 | 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] | 451 | static_assert(zs[0][0][0][0] == 1, ""); |
| 452 | static_assert(zs[1][1][1][1] == 16, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 453 | 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] | 454 | static_assert((&zs[0][0][0][2])[-1] == 2, ""); |
| 455 | static_assert(**(**(zs + 1) + 1) == 11, ""); |
| 456 | static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 457 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 458 | constexpr int fail(const int &p) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 459 | return (&p)[64]; // expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 460 | } |
| 461 | static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1)) == 11, ""); // \ |
| 462 | expected-error {{static_assert expression is not an integral constant expression}} \ |
| 463 | expected-note {{in call to 'fail(zs[1][0][1][0])'}} |
| 464 | |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame^] | 465 | 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] | 466 | constexpr int SumNonzero(const int *p) { |
| 467 | return *p + (*p ? SumNonzero(p+1) : 0); |
| 468 | } |
| 469 | constexpr int CountZero(const int *p, const int *q) { |
| 470 | return p == q ? 0 : (*p == 0) + CountZero(p+1, q); |
| 471 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 472 | static_assert(SumNonzero(arr) == 6, ""); |
| 473 | static_assert(CountZero(arr, arr + 40) == 36, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 474 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 475 | struct ArrayElem { |
| 476 | constexpr ArrayElem() : n(0) {} |
| 477 | int n; |
| 478 | constexpr int f() { return n; } |
| 479 | }; |
| 480 | struct ArrayRVal { |
| 481 | constexpr ArrayRVal() {} |
| 482 | ArrayElem elems[10]; |
| 483 | }; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 484 | static_assert(ArrayRVal().elems[3].f() == 0, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 485 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | namespace DependentValues { |
| 489 | |
| 490 | struct I { int n; typedef I V[10]; }; |
| 491 | I::V x, y; |
| 492 | template<bool B> struct S { |
| 493 | int k; |
| 494 | void f() { |
| 495 | I::V &cells = B ? x : y; |
| 496 | I &i = cells[k]; |
| 497 | switch (i.n) {} |
| 498 | } |
| 499 | }; |
| 500 | |
| 501 | } |
| 502 | |
| 503 | namespace Class { |
| 504 | |
| 505 | struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; |
| 506 | constexpr int fn(const A &a) { return a.k; } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 507 | static_assert(fn(A(4,5)) == 9, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 508 | |
| 509 | struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}} |
| 510 | struct C { |
| 511 | constexpr C(C *this_) : m(42), n(this_->m) {} // ok |
| 512 | int m, n; |
| 513 | }; |
| 514 | struct D { |
| 515 | C c; |
| 516 | constexpr D() : c(&c) {} |
| 517 | }; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 518 | static_assert(D().c.n == 42, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 519 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 520 | struct E { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 521 | constexpr E() : p(&p) {} // expected-note {{pointer to temporary cannot be used to initialize a member in a constant expression}} |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 522 | void *p; |
| 523 | }; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 524 | 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] | 525 | // This is a constant expression if we elide the copy constructor call, and |
| 526 | // 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] | 527 | constexpr E e2 = E(); |
| 528 | static_assert(e2.p == &e2.p, ""); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 529 | constexpr E e3; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 530 | static_assert(e3.p == &e3.p, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 531 | |
| 532 | extern const class F f; |
| 533 | struct F { |
| 534 | constexpr F() : p(&f.p) {} |
| 535 | const void *p; |
| 536 | }; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 537 | constexpr F f; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 538 | |
| 539 | struct G { |
| 540 | struct T { |
| 541 | constexpr T(T *p) : u1(), u2(p) {} |
| 542 | union U1 { |
| 543 | constexpr U1() {} |
| 544 | int a, b = 42; |
| 545 | } u1; |
| 546 | union U2 { |
| 547 | constexpr U2(T *p) : c(p->u1.b) {} |
| 548 | int c, d; |
| 549 | } u2; |
| 550 | } t; |
| 551 | constexpr G() : t(&t) {} |
| 552 | } constexpr g; |
| 553 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 554 | 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] | 555 | static_assert(g.t.u1.b == 42, ""); |
| 556 | static_assert(g.t.u2.c == 42, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 557 | 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] | 558 | |
| 559 | struct S { |
| 560 | int a, b; |
| 561 | const S *p; |
| 562 | double d; |
| 563 | const char *q; |
| 564 | |
| 565 | constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} |
| 566 | }; |
| 567 | |
| 568 | S global(43, &global); |
| 569 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 570 | static_assert(S(15, &global).b == 15, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 571 | |
| 572 | constexpr bool CheckS(const S &s) { |
| 573 | return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; |
| 574 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 575 | static_assert(CheckS(S(27, &global)), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 576 | |
| 577 | struct Arr { |
| 578 | char arr[3]; |
| 579 | constexpr Arr() : arr{'x', 'y', 'z'} {} |
| 580 | }; |
| 581 | constexpr int hash(Arr &&a) { |
| 582 | return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; |
| 583 | } |
| 584 | constexpr int k = hash(Arr()); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 585 | static_assert(k == 0x007a7978, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 586 | |
| 587 | |
| 588 | struct AggregateInit { |
| 589 | const char &c; |
| 590 | int n; |
| 591 | double d; |
| 592 | int arr[5]; |
| 593 | void *p; |
| 594 | }; |
| 595 | |
| 596 | constexpr AggregateInit agg1 = { "hello"[0] }; |
| 597 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 598 | static_assert(strcmp_ce(&agg1.c, "hello") == 0, ""); |
| 599 | static_assert(agg1.n == 0, ""); |
| 600 | static_assert(agg1.d == 0.0, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 601 | // FIXME: check pointer arithmetic here. |
| 602 | static_assert(agg1.arr[-1] == 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] | 603 | static_assert(agg1.arr[0] == 0, ""); |
| 604 | static_assert(agg1.arr[4] == 0, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 605 | 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] | 606 | static_assert(agg1.p == nullptr, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 607 | |
| 608 | namespace SimpleDerivedClass { |
| 609 | |
| 610 | struct B { |
| 611 | constexpr B(int n) : a(n) {} |
| 612 | int a; |
| 613 | }; |
| 614 | struct D : B { |
| 615 | constexpr D(int n) : B(n) {} |
| 616 | }; |
| 617 | constexpr D d(3); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 618 | static_assert(d.a == 3, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 619 | |
| 620 | } |
| 621 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 622 | struct Bottom { constexpr Bottom() {} }; |
| 623 | struct Base : Bottom { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 624 | constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} |
| 625 | int a; |
| 626 | const char *b; |
| 627 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 628 | struct Base2 : Bottom { |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 629 | constexpr Base2(const int &r) : r(r) {} |
| 630 | int q = 123; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 631 | const int &r; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 632 | }; |
| 633 | struct Derived : Base, Base2 { |
| 634 | constexpr Derived() : Base(76), Base2(a) {} |
| 635 | int c = r + b[1]; |
| 636 | }; |
| 637 | |
| 638 | constexpr bool operator==(const Base &a, const Base &b) { |
| 639 | return a.a == b.a && strcmp_ce(a.b, b.b) == 0; |
| 640 | } |
| 641 | |
| 642 | constexpr Base base; |
| 643 | constexpr Base base2(76); |
| 644 | constexpr Derived derived; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 645 | static_assert(derived.a == 76, ""); |
| 646 | static_assert(derived.b[2] == 's', ""); |
| 647 | static_assert(derived.c == 76 + 'e', ""); |
| 648 | static_assert(derived.q == 123, ""); |
| 649 | static_assert(derived.r == 76, ""); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 650 | static_assert(&derived.r == &derived.a, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 651 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 652 | static_assert(!(derived == base), ""); |
| 653 | static_assert(derived == base2, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 654 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 655 | constexpr Bottom &bot1 = (Base&)derived; |
| 656 | constexpr Bottom &bot2 = (Base2&)derived; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 657 | static_assert(&bot1 != &bot2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 658 | |
| 659 | constexpr Bottom *pb1 = (Base*)&derived; |
| 660 | constexpr Bottom *pb2 = (Base2*)&derived; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 661 | static_assert(pb1 != pb2, ""); |
| 662 | static_assert(pb1 == &bot1, ""); |
| 663 | static_assert(pb2 == &bot2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 664 | |
| 665 | constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} |
| 666 | constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} |
| 667 | constexpr Base2 &ok2 = (Base2&)bot2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 668 | static_assert(&ok2 == &derived, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 669 | |
| 670 | constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} |
| 671 | constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} |
| 672 | constexpr Base2 *pok2 = (Base2*)pb2; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 673 | static_assert(pok2 == &derived, ""); |
| 674 | static_assert(&ok2 == pok2, ""); |
| 675 | static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, ""); |
| 676 | static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 677 | |
| 678 | constexpr Base *nullB = 42 - 6 * 7; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 679 | static_assert((Bottom*)nullB == 0, ""); |
| 680 | static_assert((Derived*)nullB == 0, ""); |
| 681 | static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 682 | |
| 683 | } |
| 684 | |
| 685 | namespace Temporaries { |
| 686 | |
| 687 | struct S { |
| 688 | constexpr S() {} |
| 689 | constexpr int f(); |
| 690 | }; |
| 691 | struct T : S { |
| 692 | constexpr T(int n) : S(), n(n) {} |
| 693 | int n; |
| 694 | }; |
| 695 | constexpr int S::f() { |
| 696 | // 'this' must be the postfix-expression in a class member access expression, |
| 697 | // so we can't just use |
| 698 | // return static_cast<T*>(this)->n; |
| 699 | return this->*(int(S::*))&T::n; |
| 700 | } |
| 701 | // The T temporary is implicitly cast to an S subobject, but we can recover the |
| 702 | // T full-object via a base-to-derived cast, or a derived-to-base-casted member |
| 703 | // pointer. |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 704 | static_assert(T(3).f() == 3, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 705 | |
| 706 | constexpr int f(const S &s) { |
| 707 | return static_cast<const T&>(s).n; |
| 708 | } |
| 709 | constexpr int n = f(T(5)); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 710 | static_assert(f(T(5)) == 5, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 711 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | namespace Union { |
| 715 | |
| 716 | union U { |
| 717 | int a; |
| 718 | int b; |
| 719 | }; |
| 720 | |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame^] | 721 | 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] | 722 | static_assert(u[0].a == 0, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 723 | 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] | 724 | static_assert(u[1].b == 1, ""); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 725 | static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 726 | static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 727 | static_assert((&(u[1]) + 1 + 1)->b == 3, ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 728 | |
| 729 | } |
| 730 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 731 | namespace MemberPointer { |
| 732 | struct A { |
| 733 | constexpr A(int n) : n(n) {} |
| 734 | int n; |
| 735 | constexpr int f() { return n + 3; } |
| 736 | }; |
| 737 | constexpr A a(7); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 738 | static_assert(A(5).*&A::n == 5, ""); |
| 739 | static_assert((&a)->*&A::n == 7, ""); |
| 740 | static_assert((A(8).*&A::f)() == 11, ""); |
| 741 | static_assert(((&a)->*&A::f)() == 10, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 742 | |
| 743 | struct B : A { |
| 744 | constexpr B(int n, int m) : A(n), m(m) {} |
| 745 | int m; |
| 746 | constexpr int g() { return n + m + 1; } |
| 747 | }; |
| 748 | constexpr B b(9, 13); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 749 | static_assert(B(4, 11).*&A::n == 4, ""); |
| 750 | static_assert(B(4, 11).*&B::m == 11, ""); |
| 751 | static_assert(B(4, 11).*(int(A::*))&B::m == 11, ""); |
| 752 | static_assert((&b)->*&A::n == 9, ""); |
| 753 | static_assert((&b)->*&B::m == 13, ""); |
| 754 | static_assert((&b)->*(int(A::*))&B::m == 13, ""); |
| 755 | static_assert((B(4, 11).*&A::f)() == 7, ""); |
| 756 | static_assert((B(4, 11).*&B::g)() == 16, ""); |
| 757 | static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); |
| 758 | static_assert(((&b)->*&A::f)() == 12, ""); |
| 759 | static_assert(((&b)->*&B::g)() == 23, ""); |
| 760 | static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 761 | |
| 762 | struct S { |
| 763 | constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : |
| 764 | m(m), n(n), pf(pf), pn(pn) {} |
| 765 | constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} |
| 766 | |
| 767 | constexpr int f() { return this->*pn; } |
| 768 | virtual int g() const; |
| 769 | |
| 770 | int m, n; |
| 771 | int (S::*pf)() const; |
| 772 | int S::*pn; |
| 773 | }; |
| 774 | |
| 775 | constexpr int S::*pm = &S::m; |
| 776 | constexpr int S::*pn = &S::n; |
| 777 | constexpr int (S::*pf)() const = &S::f; |
| 778 | constexpr int (S::*pg)() const = &S::g; |
| 779 | |
| 780 | constexpr S s(2, 5, &S::f, &S::m); |
| 781 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 782 | static_assert((s.*&S::f)() == 2, ""); |
| 783 | static_assert((s.*s.pf)() == 2, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 784 | |
| 785 | template<int n> struct T : T<n-1> {}; |
| 786 | template<> struct T<0> { int n; }; |
| 787 | template<> struct T<30> : T<29> { int m; }; |
| 788 | |
| 789 | T<17> t17; |
| 790 | T<30> t30; |
| 791 | |
| 792 | constexpr int (T<10>::*deepn) = &T<0>::n; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 793 | static_assert(&(t17.*deepn) == &t17.n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 794 | |
| 795 | constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; |
| 796 | constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 797 | static_assert(&(t30.*deepm) == &t30.m, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 798 | |
| 799 | constexpr T<5> *p17_5 = &t17; |
| 800 | constexpr T<13> *p17_13 = (T<13>*)p17_5; |
| 801 | constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 802 | static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); |
| 803 | static_assert(&(p17_13->*deepn) == &t17.n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 804 | constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} |
| 805 | |
| 806 | constexpr T<5> *p30_5 = &t30; |
| 807 | constexpr T<23> *p30_23 = (T<23>*)p30_5; |
| 808 | constexpr T<13> *p30_13 = p30_23; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 809 | static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); |
| 810 | static_assert(&(p30_13->*deepn) == &t30.n, ""); |
| 811 | static_assert(&(p30_23->*deepn) == &t30.n, ""); |
| 812 | static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); |
| 813 | static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); |
| 814 | static_assert(&(p30_23->*deepm) == &t30.m, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | namespace ArrayBaseDerived { |
| 818 | |
| 819 | struct Base { |
| 820 | constexpr Base() {} |
| 821 | int n = 0; |
| 822 | }; |
| 823 | struct Derived : Base { |
| 824 | constexpr Derived() {} |
| 825 | constexpr const int *f() { return &n; } |
| 826 | }; |
| 827 | |
| 828 | constexpr Derived a[10]; |
| 829 | constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); |
| 830 | constexpr Base *pb3 = const_cast<Derived*>(&a[3]); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 831 | static_assert(pb3 == pd3, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 832 | |
| 833 | // pb3 does not point to an array element. |
| 834 | constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. |
| 835 | constexpr int pb4n = pb4->n; // expected-error {{constant expression}} |
| 836 | constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this. |
| 837 | constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} |
| 838 | constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this. |
| 839 | constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} |
| 840 | constexpr Base *pb3a = pb4 - 1; |
| 841 | |
| 842 | // pb4 does not point to a Derived. |
| 843 | constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} |
| 844 | constexpr Derived *pd3a = (Derived*)pb3a; |
| 845 | constexpr int pd3n = pd3a->n; |
| 846 | |
| 847 | // pd3a still points to the Derived array. |
| 848 | constexpr Derived *pd6 = pd3a + 3; |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 849 | static_assert(pd6 == &a[6], ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 850 | constexpr Derived *pd9 = pd6 + 3; |
| 851 | constexpr Derived *pd10 = pd6 + 4; |
| 852 | constexpr int pd9n = pd9->n; // ok |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 853 | constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 854 | constexpr int pd0n = pd10[-10].n; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 855 | // FIXME: check pointer arithmetic here. |
| 856 | constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 857 | |
| 858 | constexpr Base *pb9 = pd9; |
| 859 | constexpr const int *(Base::*pfb)() const = |
| 860 | static_cast<const int *(Base::*)() const>(&Derived::f); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 861 | static_assert((pb9->*pfb)() == &a[9].n, ""); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 864 | namespace Complex { |
| 865 | |
| 866 | class complex { |
| 867 | int re, im; |
| 868 | public: |
| 869 | constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} |
| 870 | constexpr complex(const complex &o) : re(o.re), im(o.im) {} |
| 871 | constexpr complex operator-() const { return complex(-re, -im); } |
| 872 | friend constexpr complex operator+(const complex &l, const complex &r) { |
| 873 | return complex(l.re + r.re, l.im + r.im); |
| 874 | } |
| 875 | friend constexpr complex operator-(const complex &l, const complex &r) { |
| 876 | return l + -r; |
| 877 | } |
| 878 | friend constexpr complex operator*(const complex &l, const complex &r) { |
| 879 | return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); |
| 880 | } |
| 881 | friend constexpr bool operator==(const complex &l, const complex &r) { |
| 882 | return l.re == r.re && l.im == r.im; |
| 883 | } |
| 884 | constexpr bool operator!=(const complex &r) const { |
| 885 | return re != r.re || im != r.im; |
| 886 | } |
| 887 | constexpr int real() const { return re; } |
| 888 | constexpr int imag() const { return im; } |
| 889 | }; |
| 890 | |
| 891 | constexpr complex i = complex(0, 1); |
| 892 | constexpr complex k = (3 + 4*i) * (6 - 4*i); |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 893 | static_assert(complex(1,0).real() == 1, ""); |
| 894 | static_assert(complex(1,0).imag() == 0, ""); |
| 895 | static_assert(((complex)1).imag() == 0, ""); |
| 896 | static_assert(k.real() == 34, ""); |
| 897 | static_assert(k.imag() == 12, ""); |
| 898 | static_assert(k - 34 == 12*i, ""); |
| 899 | static_assert((complex)1 == complex(1), ""); |
| 900 | static_assert((complex)1 != complex(0, 1), ""); |
| 901 | static_assert(complex(1) == complex(1), ""); |
| 902 | static_assert(complex(1) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 903 | constexpr complex makeComplex(int re, int im) { return complex(re, im); } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 904 | static_assert(makeComplex(1,0) == complex(1), ""); |
| 905 | static_assert(makeComplex(1,0) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 906 | |
| 907 | class complex_wrap : public complex { |
| 908 | public: |
| 909 | constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} |
| 910 | constexpr complex_wrap(const complex_wrap &o) : complex(o) {} |
| 911 | }; |
| 912 | |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 913 | static_assert((complex_wrap)1 == complex(1), ""); |
| 914 | static_assert((complex)1 != complex_wrap(0, 1), ""); |
| 915 | static_assert(complex(1) == complex_wrap(1), ""); |
| 916 | static_assert(complex_wrap(1) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 917 | constexpr complex_wrap makeComplexWrap(int re, int im) { |
| 918 | return complex_wrap(re, im); |
| 919 | } |
Richard Smith | 9eed49c | 2011-12-09 23:00:37 +0000 | [diff] [blame] | 920 | static_assert(makeComplexWrap(1,0) == complex(1), ""); |
| 921 | static_assert(makeComplexWrap(1,0) != complex(0, 1), ""); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 922 | |
| 923 | } |
Eli Friedman | f59ff8c | 2011-12-17 02:24:21 +0000 | [diff] [blame] | 924 | |
| 925 | namespace PR11595 { |
| 926 | struct A { constexpr bool operator==(int x) { return true; } }; |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 927 | struct B { B(); A& x; }; |
| 928 | static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} |
| 929 | |
| 930 | constexpr bool f(int k) { |
| 931 | return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} |
| 932 | } |
| 933 | constexpr int n = f(1); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'f(1)'}} |
Eli Friedman | f59ff8c | 2011-12-17 02:24:21 +0000 | [diff] [blame] | 934 | } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 935 | |
| 936 | namespace ExprWithCleanups { |
| 937 | struct A { A(); ~A(); int get(); }; |
| 938 | constexpr int get(bool FromA) { return FromA ? A().get() : 1; } |
| 939 | constexpr int n = get(false); |
| 940 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 941 | |
| 942 | namespace Volatile { |
| 943 | |
| 944 | volatile constexpr int n1 = 0; // expected-note {{here}} |
| 945 | volatile const int n2 = 0; // expected-note {{here}} |
| 946 | int n3 = 37; // expected-note {{declared here}} |
| 947 | |
| 948 | constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}} |
| 949 | constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}} |
| 950 | |
| 951 | struct T { int n; }; |
| 952 | const T t = { 42 }; // expected-note {{declared here}} |
| 953 | |
| 954 | constexpr int f(volatile int &&r) { |
| 955 | return r; // expected-note {{read of volatile temporary is not allowed in a constant expression}} |
| 956 | } |
| 957 | struct S { |
| 958 | int k : f(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'f(0)'}} |
| 959 | int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}} |
| 960 | int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}} |
| 961 | }; |
| 962 | |
| 963 | } |
Richard Smith | dd4b350 | 2011-12-25 21:17:58 +0000 | [diff] [blame] | 964 | |
| 965 | namespace ExternConstexpr { |
| 966 | extern constexpr int n = 0; |
| 967 | extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}} |
| 968 | void f() { |
| 969 | extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}} |
| 970 | constexpr int j = 0; |
| 971 | constexpr int k; // expected-error {{default initialization of an object of const type}} |
| 972 | } |
| 973 | } |