| 1 |
// RUN: %clang_cc1 -std=c++23 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx20_23,cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion |
| 2 |
// RUN: %clang_cc1 -std=c++20 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx11_20,cxx20_23,pre-cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion |
| 3 |
// RUN: %clang_cc1 -std=c++11 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx11_20,cxx11,pre-cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion |
| 4 |
|
| 5 |
// This macro forces its argument to be constant-folded, even if it's not |
| 6 |
// otherwise a constant expression. |
| 7 |
#define fold(x) (__builtin_constant_p(x) ? (x) : (x)) |
| 8 |
|
| 9 |
namespace StaticAssertFoldTest { |
| 10 |
|
| 11 |
int x; |
| 12 |
static_assert(++x, "test"); // expected-error {{not an integral constant expression}} |
| 13 |
// cxx20_23-note@-1 {{cannot modify an object that is visible outside that expression}} |
| 14 |
static_assert(false, "test"); // expected-error {{test}} |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
int array[(long)(char *)0]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \ |
| 19 |
// expected-warning {{variable length array folded to constant array as an extension}} \ |
| 20 |
// expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
| 21 |
|
| 22 |
typedef decltype(sizeof(char)) size_t; |
| 23 |
|
| 24 |
template<typename T> constexpr T id(const T &t) { return t; } |
| 25 |
template<typename T> constexpr T min(const T &a, const T &b) { |
| 26 |
return a < b ? a : b; |
| 27 |
} |
| 28 |
template<typename T> constexpr T max(const T &a, const T &b) { |
| 29 |
return a < b ? b : a; |
| 30 |
} |
| 31 |
template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; } |
| 32 |
template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; } |
| 33 |
|
| 34 |
struct MemberZero { |
| 35 |
constexpr int zero() const { return 0; } |
| 36 |
}; |
| 37 |
|
| 38 |
constexpr int arr[]; // expected-error {{constexpr variable 'arr' must be initialized by a constant expression}} |
| 39 |
constexpr int arr2[2]; // expected-error {{constexpr variable 'arr2' must be initialized by a constant expression}} |
| 40 |
constexpr int arr3[2] = {}; |
| 41 |
|
| 42 |
namespace DerivedToVBaseCast { |
| 43 |
|
| 44 |
struct U { int n; }; |
| 45 |
struct V : U { int n; }; |
| 46 |
struct A : virtual V { int n; }; |
| 47 |
struct Aa { int n; }; |
| 48 |
struct B : virtual A, Aa {}; |
| 49 |
struct C : virtual A, Aa {}; |
| 50 |
struct D : B, C {}; |
| 51 |
|
| 52 |
D d; |
| 53 |
constexpr B *p = &d; |
| 54 |
constexpr C *q = &d; |
| 55 |
|
| 56 |
static_assert((void*)p != (void*)q, ""); |
| 57 |
static_assert((A*)p == (A*)q, ""); |
| 58 |
static_assert((Aa*)p != (Aa*)q, ""); |
| 59 |
|
| 60 |
constexpr B &pp = d; |
| 61 |
constexpr C &qq = d; |
| 62 |
static_assert((void*)&pp != (void*)&qq, ""); |
| 63 |
static_assert(&(A&)pp == &(A&)qq, ""); |
| 64 |
static_assert(&(Aa&)pp != &(Aa&)qq, ""); |
| 65 |
|
| 66 |
constexpr V *v = p; |
| 67 |
constexpr V *w = q; |
| 68 |
constexpr V *x = (A*)p; |
| 69 |
static_assert(v == w, ""); |
| 70 |
static_assert(v == x, ""); |
| 71 |
|
| 72 |
static_assert((U*)&d == p, ""); |
| 73 |
static_assert((U*)&d == q, ""); |
| 74 |
static_assert((U*)&d == v, ""); |
| 75 |
static_assert((U*)&d == w, ""); |
| 76 |
static_assert((U*)&d == x, ""); |
| 77 |
|
| 78 |
struct X {}; |
| 79 |
struct Y1 : virtual X {}; |
| 80 |
struct Y2 : X {}; |
| 81 |
struct Z : Y1, Y2 {}; |
| 82 |
Z z; |
| 83 |
static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, ""); |
| 84 |
} |
| 85 |
|
| 86 |
namespace ConstCast { |
| 87 |
|
| 88 |
constexpr int n1 = 0; |
| 89 |
constexpr int n2 = const_cast<int&>(n1); |
| 90 |
constexpr int *n3 = const_cast<int*>(&n1); |
| 91 |
constexpr int n4 = *const_cast<int*>(&n1); |
| 92 |
constexpr const int * const *n5 = const_cast<const int* const*>(&n3); |
| 93 |
constexpr int **n6 = const_cast<int**>(&n3); |
| 94 |
constexpr int n7 = **n5; |
| 95 |
constexpr int n8 = **n6; |
| 96 |
|
| 97 |
// const_cast from prvalue to xvalue. |
| 98 |
struct A { int n; }; |
| 99 |
constexpr int n9 = (const_cast<A&&>(A{123})).n; |
| 100 |
static_assert(n9 == 123, ""); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
namespace TemplateArgumentConversion { |
| 105 |
template<int n> struct IntParam {}; |
| 106 |
|
| 107 |
using IntParam0 = IntParam<0>; |
| 108 |
using IntParam0 = IntParam<id(0)>; |
| 109 |
using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} |
| 110 |
} |
| 111 |
|
| 112 |
namespace CaseStatements { |
| 113 |
int x; |
| 114 |
void f(int n) { |
| 115 |
switch (n) { |
| 116 |
case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}} |
| 117 |
case id(0): // expected-error {{duplicate case value '0'}} |
| 118 |
return; |
| 119 |
case __builtin_constant_p(true) ? (__SIZE_TYPE__)&x : 0:; // expected-error {{constant}} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
extern int &Recurse1; |
| 125 |
int &Recurse2 = Recurse1; // expected-note {{declared here}} |
| 126 |
int &Recurse1 = Recurse2; |
| 127 |
constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}} |
| 128 |
|
| 129 |
extern const int RecurseA; |
| 130 |
const int RecurseB = RecurseA; // expected-note {{declared here}} |
| 131 |
const int RecurseA = 10; |
| 132 |
constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}} |
| 133 |
|
| 134 |
namespace MemberEnum { |
| 135 |
struct WithMemberEnum { |
| 136 |
enum E { A = 42 }; |
| 137 |
} wme; |
| 138 |
|
| 139 |
static_assert(wme.A == 42, ""); |
| 140 |
} |
| 141 |
|
| 142 |
namespace DefaultArguments { |
| 143 |
|
| 144 |
const int z = int(); |
| 145 |
constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) { |
| 146 |
return a + b + *c + d; |
| 147 |
} |
| 148 |
const int four = 4; |
| 149 |
constexpr int eight = 8; |
| 150 |
constexpr const int twentyseven = 27; |
| 151 |
static_assert(Sum() == 0, ""); |
| 152 |
static_assert(Sum(1) == 1, ""); |
| 153 |
static_assert(Sum(1, four) == 5, ""); |
| 154 |
static_assert(Sum(1, eight, &twentyseven) == 36, ""); |
| 155 |
static_assert(Sum(1, 2, &four, eight) == 15, ""); |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
namespace Ellipsis { |
| 160 |
|
| 161 |
// Note, values passed through an ellipsis can't actually be used. |
| 162 |
constexpr int F(int a, ...) { return a; } |
| 163 |
static_assert(F(0) == 0, ""); |
| 164 |
static_assert(F(1, 0) == 1, ""); |
| 165 |
static_assert(F(2, "test") == 2, ""); |
| 166 |
static_assert(F(3, &F) == 3, ""); |
| 167 |
int k = 0; // expected-note {{here}} |
| 168 |
static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
namespace Recursion { |
| 173 |
constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; } |
| 174 |
static_assert(fib(11) == 89, ""); |
| 175 |
|
| 176 |
constexpr int gcd_inner(int a, int b) { |
| 177 |
return b == 0 ? a : gcd_inner(b, a % b); |
| 178 |
} |
| 179 |
constexpr int gcd(int a, int b) { |
| 180 |
return gcd_inner(max(a, b), min(a, b)); |
| 181 |
} |
| 182 |
|
| 183 |
static_assert(gcd(1749237, 5628959) == 7, ""); |
| 184 |
} |
| 185 |
|
| 186 |
namespace FunctionCast { |
| 187 |
// When folding, we allow functions to be cast to different types. Such |
| 188 |
// cast functions cannot be called, even if they're constexpr. |
| 189 |
constexpr int f() { return 1; } |
| 190 |
typedef double (*DoubleFn)(); |
| 191 |
typedef int (*IntFn)(); |
| 192 |
int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{Clang extension}} |
| 193 |
int b[(int)IntFn(f)()]; // ok |
| 194 |
} |
| 195 |
|
| 196 |
namespace StaticMemberFunction { |
| 197 |
struct S { |
| 198 |
static constexpr int k = 42; |
| 199 |
static constexpr int f(int n) { return n * k + 2; } |
| 200 |
} s; |
| 201 |
|
| 202 |
constexpr int n = s.f(19); |
| 203 |
static_assert(S::f(19) == 800, ""); |
| 204 |
static_assert(s.f(19) == 800, ""); |
| 205 |
static_assert(n == 800, ""); |
| 206 |
|
| 207 |
constexpr int (*sf1)(int) = &S::f; |
| 208 |
constexpr int (*sf2)(int) = &s.f; |
| 209 |
constexpr const int *sk = &s.k; |
| 210 |
|
| 211 |
// Note, out_of_lifetime returns an invalid pointer value, but we don't do |
| 212 |
// anything with it (other than copy it around), so there's no UB there. |
| 213 |
constexpr S *out_of_lifetime(S s) { return &s; } // expected-warning {{address of stack}} |
| 214 |
static_assert(out_of_lifetime({})->k == 42, ""); |
| 215 |
static_assert(out_of_lifetime({})->f(3) == 128, ""); |
| 216 |
|
| 217 |
// Similarly, using an inactive union member breaks no rules. |
| 218 |
union U { |
| 219 |
int n; |
| 220 |
S s; |
| 221 |
}; |
| 222 |
constexpr U u = {0}; |
| 223 |
static_assert(u.s.k == 42, ""); |
| 224 |
static_assert(u.s.f(1) == 44, ""); |
| 225 |
|
| 226 |
// And likewise for a past-the-end pointer. |
| 227 |
static_assert((&s)[1].k == 42, ""); |
| 228 |
static_assert((&s)[1].f(1) == 44, ""); |
| 229 |
} |
| 230 |
|
| 231 |
namespace ParameterScopes { |
| 232 |
|
| 233 |
const int k = 42; |
| 234 |
constexpr const int &ObscureTheTruth(const int &a) { return a; } |
| 235 |
constexpr const int &MaybeReturnJunk(bool b, const int a) { |
| 236 |
return ObscureTheTruth(b ? a : k); |
| 237 |
} |
| 238 |
static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok |
| 239 |
constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}} |
| 240 |
|
| 241 |
constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { |
| 242 |
return ObscureTheTruth(b ? a : k); |
| 243 |
} |
| 244 |
static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok |
| 245 |
constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok |
| 246 |
|
| 247 |
constexpr int InternalReturnJunk(int n) { |
| 248 |
return MaybeReturnJunk(true, n); // expected-note {{read of object outside its lifetime}} |
| 249 |
} |
| 250 |
constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}} |
| 251 |
|
| 252 |
constexpr int LToR(int &n) { return n; } |
| 253 |
constexpr int GrabCallersArgument(bool which, int a, int b) { |
| 254 |
return LToR(which ? b : a); |
| 255 |
} |
| 256 |
static_assert(GrabCallersArgument(false, 1, 2) == 1, ""); |
| 257 |
static_assert(GrabCallersArgument(true, 4, 8) == 8, ""); |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
namespace Pointers { |
| 262 |
|
| 263 |
constexpr int f(int n, const int *a, const int *b, const int *c) { |
| 264 |
return n == 0 ? 0 : *a + f(n-1, b, c, a); |
| 265 |
} |
| 266 |
|
| 267 |
const int x = 1, y = 10, z = 100; |
| 268 |
static_assert(f(23, &x, &y, &z) == 788, ""); |
| 269 |
|
| 270 |
constexpr int g(int n, int a, int b, int c) { |
| 271 |
return f(n, &a, &b, &c); |
| 272 |
} |
| 273 |
static_assert(g(23, x, y, z) == 788, ""); |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
namespace FunctionPointers { |
| 278 |
|
| 279 |
constexpr int Double(int n) { return 2 * n; } |
| 280 |
constexpr int Triple(int n) { return 3 * n; } |
| 281 |
constexpr int Twice(int (*F)(int), int n) { return F(F(n)); } |
| 282 |
constexpr int Quadruple(int n) { return Twice(Double, n); } |
| 283 |
constexpr auto Select(int n) -> int (*)(int) { |
| 284 |
return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0; |
| 285 |
} |
| 286 |
constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{'F' evaluates to a null function pointer}} |
| 287 |
|
| 288 |
static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); |
| 289 |
|
| 290 |
constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(nullptr, 0)'}} |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
namespace PointerComparison { |
| 295 |
|
| 296 |
int x, y; |
| 297 |
static_assert(&x == &y, "false"); // expected-error {{false}} |
| 298 |
static_assert(&x != &y, ""); |
| 299 |
constexpr bool g1 = &x == &y; |
| 300 |
constexpr bool g2 = &x != &y; |
| 301 |
constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 302 |
constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 303 |
constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 304 |
constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 305 |
|
| 306 |
struct S { int x, y; } s; |
| 307 |
static_assert(&s.x == &s.y, "false"); // expected-error {{false}} |
| 308 |
static_assert(&s.x != &s.y, ""); |
| 309 |
static_assert(&s.x <= &s.y, ""); |
| 310 |
static_assert(&s.x >= &s.y, "false"); // expected-error {{false}} |
| 311 |
static_assert(&s.x < &s.y, ""); |
| 312 |
static_assert(&s.x > &s.y, "false"); // expected-error {{false}} |
| 313 |
|
| 314 |
static_assert(0 == &y, "false"); // expected-error {{false}} |
| 315 |
static_assert(0 != &y, ""); |
| 316 |
constexpr bool n3 = (int*)0 <= &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 317 |
constexpr bool n4 = (int*)0 >= &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 318 |
constexpr bool n5 = (int*)0 < &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 319 |
constexpr bool n6 = (int*)0 > &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 320 |
|
| 321 |
static_assert(&x == 0, "false"); // expected-error {{false}} |
| 322 |
static_assert(&x != 0, ""); |
| 323 |
constexpr bool n9 = &x <= (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 324 |
constexpr bool n10 = &x >= (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 325 |
constexpr bool n11 = &x < (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 326 |
constexpr bool n12 = &x > (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}} |
| 327 |
|
| 328 |
static_assert(&x == &x, ""); |
| 329 |
static_assert(&x != &x, "false"); // expected-error {{false}} |
| 330 |
static_assert(&x <= &x, ""); |
| 331 |
static_assert(&x >= &x, ""); |
| 332 |
static_assert(&x < &x, "false"); // expected-error {{false}} |
| 333 |
static_assert(&x > &x, "false"); // expected-error {{false}} |
| 334 |
|
| 335 |
constexpr S* sptr = &s; |
| 336 |
constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // cxx11-error {{constant expression}} cxx11-note {{dynamic_cast}} |
| 337 |
|
| 338 |
struct U {}; |
| 339 |
struct Str { |
| 340 |
int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \ |
| 341 |
cxx11-warning {{not an integral constant expression}} \ |
| 342 |
cxx11-note {{dynamic_cast is not allowed in a constant expression}} |
| 343 |
int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \ |
| 344 |
expected-warning {{not an integral constant expression}} \ |
| 345 |
expected-note {{reinterpret_cast is not allowed in a constant expression}} |
| 346 |
int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \ |
| 347 |
expected-warning {{not an integral constant expression}} \ |
| 348 |
expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
| 349 |
int d : (S*)(42) == (S*)(42); // \ |
| 350 |
expected-warning {{not an integral constant expression}} \ |
| 351 |
expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
| 352 |
int e : (Str*)(sptr) == (Str*)(sptr); // \ |
| 353 |
expected-warning {{not an integral constant expression}} \ |
| 354 |
expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
| 355 |
int f : &(U&)(*sptr) == &(U&)(*sptr); // \ |
| 356 |
expected-warning {{not an integral constant expression}} \ |
| 357 |
expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} |
| 358 |
int g : (S*)(void*)(sptr) == sptr; // \ |
| 359 |
expected-warning {{not an integral constant expression}} \ |
| 360 |
expected-note {{cast from 'void *' is not allowed in a constant expression}} |
| 361 |
}; |
| 362 |
|
| 363 |
extern char externalvar[]; |
| 364 |
constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} expected-note {{reinterpret_cast}} |
| 365 |
static_assert(0 != "foo", ""); |
| 366 |
|
| 367 |
// OK: These string literals cannot possibly overlap. |
| 368 |
static_assert(+"foo" != +"bar", ""); |
| 369 |
static_assert("xfoo" + 1 != "yfoo" + 1, ""); |
| 370 |
static_assert(+"foot" != +"foo", ""); |
| 371 |
static_assert(+"foo\0bar" != +"foo\0baz", ""); |
| 372 |
|
| 373 |
// These can't overlap because the null terminator for UTF-16 is two bytes wide. |
| 374 |
static_assert(fold((const char*)u"A" != (const char*)"\0A\0x"), ""); |
| 375 |
static_assert(fold((const char*)u"A" != (const char*)"A\0\0x"), ""); |
| 376 |
|
| 377 |
constexpr const char *string = "hello"; |
| 378 |
constexpr const char *also_string = string; |
| 379 |
static_assert(string == string, ""); |
| 380 |
static_assert(string == also_string, ""); |
| 381 |
|
| 382 |
// These strings may overlap, and so the result of the comparison is unknown. |
| 383 |
constexpr bool may_overlap_1 = +"foo" == +"foo"; // expected-error {{}} expected-note {{addresses of potentially overlapping literals}} |
| 384 |
constexpr bool may_overlap_2 = +"foo" == +"foo\0bar"; // expected-error {{}} expected-note {{addresses of potentially overlapping literals}} |
| 385 |
constexpr bool may_overlap_3 = +"foo" == "bar\0foo" + 4; // expected-error {{}} expected-note {{addresses of potentially overlapping literals}} |
| 386 |
constexpr bool may_overlap_4 = "xfoo" + 1 == "xfoo" + 1; // expected-error {{}} expected-note {{addresses of potentially overlapping literals}} |
| 387 |
|
| 388 |
// These may overlap even though they have different encodings. |
| 389 |
// One of these two comparisons is non-constant, but due to endianness we don't |
| 390 |
// know which one. |
| 391 |
constexpr bool may_overlap_different_encoding[] = |
| 392 |
{fold((const char*)u"A" != (const char*)"xA\0\0\0x" + 1), fold((const char*)u"A" != (const char*)"x\0A\0\0x" + 1)}; |
| 393 |
// expected-error@-2 {{}} expected-note@-1 {{addresses of potentially overlapping literals}} |
| 394 |
|
| 395 |
} |
| 396 |
|
| 397 |
constexpr const char *getStr() { |
| 398 |
return "abc"; // expected-note {{repeated evaluation of the same literal expression can produce different objects}} |
| 399 |
} |
| 400 |
constexpr int strMinus() { |
| 401 |
(void)(getStr() - getStr()); // expected-note {{arithmetic on addresses of potentially overlapping literals has unspecified value}} \ |
| 402 |
// cxx11-warning {{C++14 extension}} |
| 403 |
return 0; |
| 404 |
} |
| 405 |
static_assert(strMinus() == 0, ""); // expected-error {{not an integral constant expression}} \ |
| 406 |
// expected-note {{in call to}} |
| 407 |
|
| 408 |
constexpr int a = 0; |
| 409 |
constexpr int b = 1; |
| 410 |
constexpr int n = &b - &a; // expected-error {{must be initialized by a constant expression}} \ |
| 411 |
// expected-note {{arithmetic involving unrelated objects '&b' and '&a' has unspecified value}} |
| 412 |
constexpr static int arrk[2] = {1,2}; |
| 413 |
constexpr static int arrk2[2] = {3,4}; |
| 414 |
constexpr int k2 = &arrk[1] - &arrk2[0]; // expected-error {{must be initialized by a constant expression}} \ |
| 415 |
// expected-note {{arithmetic involving unrelated objects}} |
| 416 |
|
| 417 |
namespace MaterializeTemporary { |
| 418 |
|
| 419 |
constexpr int f(const int &r) { return r; } |
| 420 |
constexpr int n = f(1); |
| 421 |
|
| 422 |
constexpr bool same(const int &a, const int &b) { return &a == &b; } |
| 423 |
constexpr bool sameTemporary(const int &n) { return same(n, n); } |
| 424 |
|
| 425 |
static_assert(n, ""); |
| 426 |
static_assert(!same(4, 4), ""); |
| 427 |
static_assert(same(n, n), ""); |
| 428 |
static_assert(sameTemporary(9), ""); |
| 429 |
|
| 430 |
struct A { int &&r; }; |
| 431 |
struct B { A &&a1; A &&a2; }; |
| 432 |
|
| 433 |
constexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}} |
| 434 |
static_assert(&b1.a1 != &b1.a2, ""); |
| 435 |
static_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} |
| 436 |
|
| 437 |
constexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}} |
| 438 |
static_assert(&b1 != &b2, ""); |
| 439 |
static_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} |
| 440 |
|
| 441 |
constexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} |
| 442 |
void foo() { |
| 443 |
constexpr static B b1 { { 1 }, { 2 } }; // ok |
| 444 |
constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} |
| 445 |
constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} |
| 446 |
} |
| 447 |
|
| 448 |
constexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} }); |
| 449 |
static_assert(&b4 != &b2, ""); |
| 450 |
|
| 451 |
// Proposed DR: copy-elision doesn't trigger lifetime extension. |
| 452 |
// cxx11-warning@+1 2{{temporary whose address is used as value of local variable 'b5' will be destroyed at the end of the full-expression}} |
| 453 |
constexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} |
| 454 |
|
| 455 |
namespace NestedNonStatic { |
| 456 |
// Proposed DR: for a reference constant expression to refer to a static |
| 457 |
// storage duration temporary, that temporary must itself be initialized |
| 458 |
// by a constant expression (a core constant expression is not enough). |
| 459 |
struct A { int &&r; }; |
| 460 |
struct B { A &&a; }; |
| 461 |
constexpr B a = { A{0} }; // ok |
| 462 |
// cxx11-warning@+1 {{temporary bound to reference member of local variable 'b' will be destroyed at the end of the full-expression}} |
| 463 |
constexpr B b = { A(A{0}) }; // cxx11-error {{constant expression}} cxx11-note {{reference to temporary}} cxx11-note {{here}} |
| 464 |
} |
| 465 |
|
| 466 |
namespace FakeInitList { |
| 467 |
struct init_list_3_ints { const int (&x)[3]; }; |
| 468 |
struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; }; |
| 469 |
constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } }; |
| 470 |
} |
| 471 |
|
| 472 |
namespace ConstAddedByReference { |
| 473 |
const int &r = (0); |
| 474 |
constexpr int n = r; |
| 475 |
|
| 476 |
int &&r2 = 0; // expected-note {{created here}} |
| 477 |
constexpr int n2 = r2; // expected-error {{constant}} expected-note {{read of temporary}} |
| 478 |
|
| 479 |
struct A { constexpr operator int() const { return 0; }}; |
| 480 |
struct B { constexpr operator const int() const { return 0; }}; |
| 481 |
const int &ra = A(); |
| 482 |
const int &rb = B(); |
| 483 |
constexpr int na = ra; |
| 484 |
constexpr int nb = rb; |
| 485 |
|
| 486 |
struct C { int &&r; }; |
| 487 |
constexpr C c1 = {1}; |
| 488 |
constexpr int &c1r = c1.r; |
| 489 |
constexpr const C &c2 = {2}; |
| 490 |
constexpr int &c2r = c2.r; |
| 491 |
constexpr C &&c3 = {3}; // expected-note {{created here}} |
| 492 |
constexpr int &c3r = c3.r; // expected-error {{constant}} expected-note {{read of temporary}} |
| 493 |
} |
| 494 |
|
| 495 |
} |
| 496 |
|
| 497 |
constexpr int strcmp_ce(const char *p, const char *q) { |
| 498 |
return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1); |
| 499 |
} |
| 500 |
|
| 501 |
namespace StringLiteral { |
| 502 |
|
| 503 |
template<typename Char> |
| 504 |
constexpr int MangleChars(const Char *p) { |
| 505 |
return *p + 3 * (*p ? MangleChars(p+1) : 0); |
| 506 |
} |
| 507 |
|
| 508 |
static_assert(MangleChars("constexpr!") == 1768383, ""); |
| 509 |
static_assert(MangleChars(u8"constexpr!") == 1768383, ""); |
| 510 |
static_assert(MangleChars(L"constexpr!") == 1768383, ""); |
| 511 |
static_assert(MangleChars(u"constexpr!") == 1768383, ""); |
| 512 |
static_assert(MangleChars(U"constexpr!") == 1768383, ""); |
| 513 |
|
| 514 |
constexpr char c0 = "nought index"[0]; |
| 515 |
constexpr char c1 = "nice index"[10]; |
| 516 |
constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
| 517 |
constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-note {{cannot refer to element -1 of array of 15 elements}} |
| 518 |
constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast that performs the conversions of a reinterpret_cast}} |
| 519 |
|
| 520 |
constexpr const char *p = "test" + 2; |
| 521 |
static_assert(*p == 's', ""); |
| 522 |
|
| 523 |
constexpr const char *max_iter(const char *a, const char *b) { |
| 524 |
return *a < *b ? b : a; |
| 525 |
} |
| 526 |
constexpr const char *max_element(const char *a, const char *b) { |
| 527 |
return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b)); |
| 528 |
} |
| 529 |
|
| 530 |
constexpr char str[] = "the quick brown fox jumped over the lazy dog"; |
| 531 |
constexpr const char *max = max_element(begin(str), end(str)); |
| 532 |
static_assert(*max == 'z', ""); |
| 533 |
static_assert(max == str + 38, ""); |
| 534 |
|
| 535 |
static_assert(strcmp_ce("hello world", "hello world") == 0, ""); |
| 536 |
static_assert(strcmp_ce("hello world", "hello clang") > 0, ""); |
| 537 |
static_assert(strcmp_ce("constexpr", "test") < 0, ""); |
| 538 |
static_assert(strcmp_ce("", " ") < 0, ""); |
| 539 |
|
| 540 |
struct S { |
| 541 |
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}} |
| 542 |
}; |
| 543 |
|
| 544 |
struct T { |
| 545 |
char c[6]; |
| 546 |
constexpr T() : c{"foo"} {} |
| 547 |
}; |
| 548 |
constexpr T t; |
| 549 |
|
| 550 |
static_assert(t.c[0] == 'f', ""); |
| 551 |
static_assert(t.c[1] == 'o', ""); |
| 552 |
static_assert(t.c[2] == 'o', ""); |
| 553 |
static_assert(t.c[3] == 0, ""); |
| 554 |
static_assert(t.c[4] == 0, ""); |
| 555 |
static_assert(t.c[5] == 0, ""); |
| 556 |
static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 557 |
|
| 558 |
struct U { |
| 559 |
wchar_t chars[6]; |
| 560 |
int n; |
| 561 |
} constexpr u = { { L"test" }, 0 }; |
| 562 |
static_assert(u.chars[2] == L's', ""); |
| 563 |
|
| 564 |
struct V { |
| 565 |
char c[4]; |
| 566 |
constexpr V() : c("hi!") {} |
| 567 |
}; |
| 568 |
static_assert(V().c[1] == "i"[0], ""); |
| 569 |
|
| 570 |
namespace Parens { |
| 571 |
constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")}, |
| 572 |
d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")}; |
| 573 |
static_assert(a[0] == 'f', ""); |
| 574 |
static_assert(b[1] == 'o', ""); |
| 575 |
static_assert(c[2] == 'o', ""); |
| 576 |
static_assert(d[0] == 'f', ""); |
| 577 |
static_assert(e[1] == 'o', ""); |
| 578 |
static_assert(f[2] == 'o', ""); |
| 579 |
static_assert(f[5] == 0, ""); |
| 580 |
static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 581 |
} |
| 582 |
|
| 583 |
} |
| 584 |
|
| 585 |
namespace Array { |
| 586 |
|
| 587 |
template<typename Iter> |
| 588 |
constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) { |
| 589 |
return begin == end ? 0 : *begin + Sum(begin+1, end); |
| 590 |
} |
| 591 |
|
| 592 |
constexpr int xs[] = { 1, 2, 3, 4, 5 }; |
| 593 |
constexpr int ys[] = { 5, 4, 3, 2, 1 }; |
| 594 |
constexpr int sum_xs = Sum(begin(xs), end(xs)); |
| 595 |
static_assert(sum_xs == 15, ""); |
| 596 |
|
| 597 |
constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, |
| 598 |
const int *xs, const int *ys, int c) { |
| 599 |
return n ? F( |
| 600 |
*xs, // expected-note {{read of dereferenced one-past-the-end pointer}} |
| 601 |
*ys, |
| 602 |
ZipFoldR(F, n-1, xs+1, ys+1, c)) // \ |
| 603 |
expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \ |
| 604 |
expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}} |
| 605 |
: c; |
| 606 |
} |
| 607 |
constexpr int MulAdd(int x, int y, int c) { return x * y + c; } |
| 608 |
constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); |
| 609 |
static_assert(InnerProduct == 35, ""); |
| 610 |
|
| 611 |
constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } |
| 612 |
constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); |
| 613 |
static_assert(DiffProd == 8, ""); |
| 614 |
static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \ |
| 615 |
expected-error {{constant expression}} \ |
| 616 |
expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}} |
| 617 |
|
| 618 |
constexpr const int *p = xs + 3; |
| 619 |
constexpr int xs4 = p[1]; // ok |
| 620 |
constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
| 621 |
constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}} |
| 622 |
constexpr int xs0 = p[-3]; // ok |
| 623 |
constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} |
| 624 |
|
| 625 |
constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; |
| 626 |
static_assert(zs[0][0][0][0] == 1, ""); |
| 627 |
static_assert(zs[1][1][1][1] == 16, ""); |
| 628 |
static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
| 629 |
static_assert((&zs[0][0][0][2])[-1] == 2, ""); |
| 630 |
static_assert(**(**(zs + 1) + 1) == 11, ""); |
| 631 |
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}} |
| 632 |
static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, ""); |
| 633 |
constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // \ |
| 634 |
expected-error {{constant expression}} \ |
| 635 |
expected-note {{cannot access array element of pointer past the end}} |
| 636 |
|
| 637 |
constexpr int fail(const int &p) { |
| 638 |
return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}} |
| 639 |
} |
| 640 |
static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \ |
| 641 |
expected-error {{static assertion expression is not an integral constant expression}} \ |
| 642 |
expected-note {{in call to 'fail(zs[1][0][1][0])'}} |
| 643 |
|
| 644 |
constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; |
| 645 |
constexpr int SumNonzero(const int *p) { |
| 646 |
return *p + (*p ? SumNonzero(p+1) : 0); |
| 647 |
} |
| 648 |
constexpr int CountZero(const int *p, const int *q) { |
| 649 |
return p == q ? 0 : (*p == 0) + CountZero(p+1, q); |
| 650 |
} |
| 651 |
static_assert(SumNonzero(arr) == 6, ""); |
| 652 |
static_assert(CountZero(arr, arr + 40) == 36, ""); |
| 653 |
|
| 654 |
struct ArrayElem { |
| 655 |
constexpr ArrayElem() : n(0) {} |
| 656 |
int n; |
| 657 |
constexpr int f() const { return n; } |
| 658 |
}; |
| 659 |
struct ArrayRVal { |
| 660 |
constexpr ArrayRVal() {} |
| 661 |
ArrayElem elems[10]; |
| 662 |
}; |
| 663 |
static_assert(ArrayRVal().elems[3].f() == 0, ""); |
| 664 |
|
| 665 |
namespace CopyCtor { |
| 666 |
struct A { |
| 667 |
constexpr A() {} |
| 668 |
constexpr A(const A &) {} |
| 669 |
}; |
| 670 |
struct B { |
| 671 |
A a; |
| 672 |
int arr[10]; |
| 673 |
}; |
| 674 |
constexpr B b{{}, {1, 2, 3, 4, 5}}; |
| 675 |
constexpr B c = b; |
| 676 |
static_assert(c.arr[2] == 3, ""); |
| 677 |
static_assert(c.arr[7] == 0, ""); |
| 678 |
|
| 679 |
// OK: the copy ctor for X doesn't read any members. |
| 680 |
struct X { struct Y {} y; } x1; |
| 681 |
constexpr X x2 = x1; |
| 682 |
} |
| 683 |
|
| 684 |
constexpr int selfref[2][2][2] = { |
| 685 |
1, selfref[0][0][0] + 1, |
| 686 |
1, selfref[0][1][0] + 1, |
| 687 |
1, selfref[0][1][1] + 1 }; |
| 688 |
static_assert(selfref[0][0][0] == 1, ""); |
| 689 |
static_assert(selfref[0][0][1] == 2, ""); |
| 690 |
static_assert(selfref[0][1][0] == 1, ""); |
| 691 |
static_assert(selfref[0][1][1] == 2, ""); |
| 692 |
static_assert(selfref[1][0][0] == 1, ""); |
| 693 |
static_assert(selfref[1][0][1] == 3, ""); |
| 694 |
static_assert(selfref[1][1][0] == 0, ""); |
| 695 |
static_assert(selfref[1][1][1] == 0, ""); |
| 696 |
|
| 697 |
constexpr int badselfref[2][2][2] = { // expected-error {{constant expression}} |
| 698 |
badselfref[1][0][0] // expected-note {{outside its lifetime}} |
| 699 |
}; |
| 700 |
|
| 701 |
struct TrivialDefCtor { int n; }; |
| 702 |
typedef TrivialDefCtor TDCArray[2][2]; |
| 703 |
static_assert(TDCArray{}[1][1].n == 0, ""); |
| 704 |
|
| 705 |
struct NonAggregateTDC : TrivialDefCtor {}; |
| 706 |
typedef NonAggregateTDC NATDCArray[2][2]; |
| 707 |
static_assert(NATDCArray{}[1][1].n == 0, ""); |
| 708 |
|
| 709 |
} |
| 710 |
|
| 711 |
// Per current CWG direction, we reject any cases where pointer arithmetic is |
| 712 |
// not statically known to be valid. |
| 713 |
namespace ArrayOfUnknownBound { |
| 714 |
extern int arr[]; |
| 715 |
constexpr int *a = arr; |
| 716 |
constexpr int *b = &arr[0]; |
| 717 |
static_assert(a == b, ""); |
| 718 |
constexpr int *c = &arr[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}} |
| 719 |
constexpr int *d = &a[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}} |
| 720 |
constexpr int *e = a + 1; // expected-error {{constant}} expected-note {{indexing of array without known bound}} |
| 721 |
|
| 722 |
struct X { |
| 723 |
int a; |
| 724 |
int b[]; // expected-warning {{C99}} |
| 725 |
}; |
| 726 |
extern X x; |
| 727 |
constexpr int *xb = x.b; // expected-error {{constant}} expected-note {{not supported}} |
| 728 |
|
| 729 |
struct Y { int a; }; |
| 730 |
extern Y yarr[]; |
| 731 |
constexpr Y *p = yarr; |
| 732 |
constexpr int *q = &p->a; |
| 733 |
|
| 734 |
extern const int carr[]; // expected-note {{here}} |
| 735 |
constexpr int n = carr[0]; // expected-error {{constant}} expected-note {{non-constexpr variable}} |
| 736 |
|
| 737 |
constexpr int local_extern[] = {1, 2, 3}; |
| 738 |
void f() { extern const int local_extern[]; } |
| 739 |
static_assert(local_extern[1] == 2, ""); |
| 740 |
} |
| 741 |
|
| 742 |
namespace DependentValues { |
| 743 |
|
| 744 |
struct I { int n; typedef I V[10]; }; |
| 745 |
I::V x, y; |
| 746 |
int g(); // expected-note {{declared here}} |
| 747 |
template<bool B, typename T> struct S : T { |
| 748 |
int k; |
| 749 |
void f() { |
| 750 |
I::V &cells = B ? x : y; |
| 751 |
I &i = cells[k]; |
| 752 |
switch (i.n) {} |
| 753 |
|
| 754 |
constexpr int n = g(); // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr function 'g'}} |
| 755 |
|
| 756 |
constexpr int m = this->g(); // ok, could be constexpr |
| 757 |
} |
| 758 |
}; |
| 759 |
|
| 760 |
extern const int n; // expected-note {{declared here}} |
| 761 |
template<typename T> void f() { |
| 762 |
// This is ill-formed, because a hypothetical instantiation at the point of |
| 763 |
// template definition would be ill-formed due to a construct that does not |
| 764 |
// depend on a template parameter. |
| 765 |
constexpr int k = n; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'n' is unknown}} |
| 766 |
} |
| 767 |
// It doesn't matter that the instantiation could later become valid: |
| 768 |
constexpr int n = 4; |
| 769 |
template void f<int>(); |
| 770 |
|
| 771 |
} |
| 772 |
|
| 773 |
namespace Class { |
| 774 |
|
| 775 |
struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; |
| 776 |
constexpr int fn(const A &a) { return a.k; } |
| 777 |
static_assert(fn(A(4,5)) == 9, ""); |
| 778 |
|
| 779 |
struct B { int n; int m; } constexpr b = { 0, b.n }; |
| 780 |
struct C { |
| 781 |
constexpr C(C *this_) : m(42), n(this_->m) {} // ok |
| 782 |
int m, n; |
| 783 |
}; |
| 784 |
struct D { |
| 785 |
C c; |
| 786 |
constexpr D() : c(&c) {} |
| 787 |
}; |
| 788 |
static_assert(D().c.n == 42, ""); |
| 789 |
|
| 790 |
struct E { |
| 791 |
constexpr E() : p(&p) {} |
| 792 |
void *p; |
| 793 |
}; |
| 794 |
constexpr const E &e1 = E(); |
| 795 |
// This is a constant expression if we elide the copy constructor call, and |
| 796 |
// is not a constant expression if we don't! But we do, so it is. |
| 797 |
constexpr E e2 = E(); |
| 798 |
static_assert(e2.p == &e2.p, ""); |
| 799 |
constexpr E e3; |
| 800 |
static_assert(e3.p == &e3.p, ""); |
| 801 |
|
| 802 |
extern const class F f; |
| 803 |
struct F { |
| 804 |
constexpr F() : p(&f.p) {} |
| 805 |
const void *p; |
| 806 |
}; |
| 807 |
constexpr F f; |
| 808 |
|
| 809 |
struct G { |
| 810 |
struct T { |
| 811 |
constexpr T(T *p) : u1(), u2(p) {} |
| 812 |
union U1 { |
| 813 |
constexpr U1() {} |
| 814 |
int a, b = 42; |
| 815 |
} u1; |
| 816 |
union U2 { |
| 817 |
constexpr U2(T *p) : c(p->u1.b) {} |
| 818 |
int c, d; |
| 819 |
} u2; |
| 820 |
} t; |
| 821 |
constexpr G() : t(&t) {} |
| 822 |
} constexpr g; |
| 823 |
|
| 824 |
static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}} |
| 825 |
static_assert(g.t.u1.b == 42, ""); |
| 826 |
static_assert(g.t.u2.c == 42, ""); |
| 827 |
static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}} |
| 828 |
|
| 829 |
struct S { |
| 830 |
int a, b; |
| 831 |
const S *p; |
| 832 |
double d; |
| 833 |
const char *q; |
| 834 |
|
| 835 |
constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} |
| 836 |
}; |
| 837 |
|
| 838 |
S global(43, &global); |
| 839 |
|
| 840 |
static_assert(S(15, &global).b == 15, ""); |
| 841 |
|
| 842 |
constexpr bool CheckS(const S &s) { |
| 843 |
return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; |
| 844 |
} |
| 845 |
static_assert(CheckS(S(27, &global)), ""); |
| 846 |
|
| 847 |
struct Arr { |
| 848 |
char arr[3]; |
| 849 |
constexpr Arr() : arr{'x', 'y', 'z'} {} |
| 850 |
}; |
| 851 |
constexpr int hash(Arr &&a) { |
| 852 |
return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; |
| 853 |
} |
| 854 |
constexpr int k = hash(Arr()); |
| 855 |
static_assert(k == 0x007a7978, ""); |
| 856 |
|
| 857 |
|
| 858 |
struct AggregateInit { |
| 859 |
const char &c; |
| 860 |
int n; |
| 861 |
double d; |
| 862 |
int arr[5]; |
| 863 |
void *p; |
| 864 |
}; |
| 865 |
|
| 866 |
constexpr AggregateInit agg1 = { "hello"[0] }; |
| 867 |
|
| 868 |
static_assert(strcmp_ce(&agg1.c, "hello") == 0, ""); |
| 869 |
static_assert(agg1.n == 0, ""); |
| 870 |
static_assert(agg1.d == 0.0, ""); |
| 871 |
static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} |
| 872 |
static_assert(agg1.arr[0] == 0, ""); |
| 873 |
static_assert(agg1.arr[4] == 0, ""); |
| 874 |
static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}} |
| 875 |
static_assert(agg1.p == nullptr, ""); |
| 876 |
|
| 877 |
static constexpr const unsigned char uc[] = { "foo" }; |
| 878 |
static_assert(uc[0] == 'f', ""); |
| 879 |
static_assert(uc[3] == 0, ""); |
| 880 |
|
| 881 |
namespace SimpleDerivedClass { |
| 882 |
|
| 883 |
struct B { |
| 884 |
constexpr B(int n) : a(n) {} |
| 885 |
int a; |
| 886 |
}; |
| 887 |
struct D : B { |
| 888 |
constexpr D(int n) : B(n) {} |
| 889 |
}; |
| 890 |
constexpr D d(3); |
| 891 |
static_assert(d.a == 3, ""); |
| 892 |
|
| 893 |
} |
| 894 |
|
| 895 |
struct Bottom { constexpr Bottom() {} }; |
| 896 |
struct Base : Bottom { |
| 897 |
constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} |
| 898 |
int a; |
| 899 |
const char *b; |
| 900 |
}; |
| 901 |
struct Base2 : Bottom { |
| 902 |
constexpr Base2(const int &r) : r(r) {} |
| 903 |
int q = 123; |
| 904 |
const int &r; |
| 905 |
}; |
| 906 |
struct Derived : Base, Base2 { |
| 907 |
constexpr Derived() : Base(76), Base2(a) {} |
| 908 |
int c = r + b[1]; |
| 909 |
}; |
| 910 |
|
| 911 |
constexpr bool operator==(const Base &a, const Base &b) { |
| 912 |
return a.a == b.a && strcmp_ce(a.b, b.b) == 0; |
| 913 |
} |
| 914 |
|
| 915 |
constexpr Base base; |
| 916 |
constexpr Base base2(76); |
| 917 |
constexpr Derived derived; |
| 918 |
static_assert(derived.a == 76, ""); |
| 919 |
static_assert(derived.b[2] == 's', ""); |
| 920 |
static_assert(derived.c == 76 + 'e', ""); |
| 921 |
static_assert(derived.q == 123, ""); |
| 922 |
static_assert(derived.r == 76, ""); |
| 923 |
static_assert(&derived.r == &derived.a, ""); |
| 924 |
|
| 925 |
static_assert(!(derived == base), ""); |
| 926 |
static_assert(derived == base2, ""); |
| 927 |
|
| 928 |
constexpr Bottom &bot1 = (Base&)derived; |
| 929 |
constexpr Bottom &bot2 = (Base2&)derived; |
| 930 |
static_assert(&bot1 != &bot2, ""); |
| 931 |
|
| 932 |
constexpr Bottom *pb1 = (Base*)&derived; |
| 933 |
constexpr Bottom *pb2 = (Base2*)&derived; |
| 934 |
static_assert(&pb1 != &pb2, ""); |
| 935 |
static_assert(pb1 == &bot1, ""); |
| 936 |
static_assert(pb2 == &bot2, ""); |
| 937 |
|
| 938 |
constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base2'}} |
| 939 |
constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base'}} |
| 940 |
constexpr Base2 &ok2 = (Base2&)bot2; |
| 941 |
static_assert(&ok2 == &derived, ""); |
| 942 |
|
| 943 |
constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base2'}} |
| 944 |
constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base'}} |
| 945 |
constexpr Base2 *pok2 = (Base2*)pb2; |
| 946 |
static_assert(pok2 == &derived, ""); |
| 947 |
static_assert(&ok2 == pok2, ""); |
| 948 |
static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, ""); |
| 949 |
static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, ""); |
| 950 |
|
| 951 |
// Core issue 903: we do not perform constant evaluation when checking for a |
| 952 |
// null pointer in C++11. Just check for an integer literal with value 0. |
| 953 |
constexpr Base *nullB = 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Base *const' with an rvalue of type 'int'}} |
| 954 |
constexpr Base *nullB1 = 0; |
| 955 |
static_assert((Bottom*)nullB == 0, ""); |
| 956 |
static_assert((Derived*)nullB1 == 0, ""); |
| 957 |
static_assert((void*)(Bottom*)nullB1 == (void*)(Derived*)nullB1, ""); |
| 958 |
Base *nullB2 = '\0'; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'char'}} |
| 959 |
Base *nullB3 = (0); |
| 960 |
Base *nullB4 = false; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'bool'}} |
| 961 |
Base *nullB5 = ((0ULL)); |
| 962 |
Base *nullB6 = 0.; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'double'}} |
| 963 |
enum Null { kNull }; |
| 964 |
Base *nullB7 = kNull; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'Class::Null'}} |
| 965 |
static_assert(nullB1 == (1 - 1), ""); // expected-error {{comparison between pointer and integer}} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
namespace ConversionOperators { |
| 970 |
|
| 971 |
struct T { |
| 972 |
constexpr T(int n) : k(5*n - 3) {} |
| 973 |
constexpr operator int() const { return k; } |
| 974 |
int k; |
| 975 |
}; |
| 976 |
|
| 977 |
struct S { |
| 978 |
constexpr S(int n) : k(2*n + 1) {} |
| 979 |
constexpr operator int() const { return k; } |
| 980 |
constexpr operator T() const { return T(k); } |
| 981 |
int k; |
| 982 |
}; |
| 983 |
|
| 984 |
constexpr bool check(T a, T b) { return a == b.k; } |
| 985 |
|
| 986 |
static_assert(S(5) == 11, ""); |
| 987 |
static_assert(check(S(5), 11), ""); |
| 988 |
|
| 989 |
namespace PR14171 { |
| 990 |
|
| 991 |
struct X { |
| 992 |
constexpr (operator int)() const { return 0; } |
| 993 |
}; |
| 994 |
static_assert(X() == 0, ""); |
| 995 |
|
| 996 |
} |
| 997 |
|
| 998 |
} |
| 999 |
|
| 1000 |
struct This { |
| 1001 |
constexpr int f() const { return 0; } |
| 1002 |
static constexpr int g() { return 0; } |
| 1003 |
void h() { |
| 1004 |
constexpr int x = f(); // expected-error {{must be initialized by a constant}} |
| 1005 |
// expected-note@-1 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}} |
| 1006 |
constexpr int y = this->f(); // expected-error {{must be initialized by a constant}} |
| 1007 |
// expected-note-re@-1 {{{{^}}use of 'this' pointer}} |
| 1008 |
constexpr int z = g(); |
| 1009 |
static_assert(z == 0, ""); |
| 1010 |
} |
| 1011 |
}; |
| 1012 |
|
| 1013 |
} |
| 1014 |
|
| 1015 |
namespace Temporaries { |
| 1016 |
|
| 1017 |
struct S { |
| 1018 |
constexpr S() {} |
| 1019 |
constexpr int f() const; |
| 1020 |
constexpr int g() const; |
| 1021 |
}; |
| 1022 |
struct T : S { |
| 1023 |
constexpr T(int n) : S(), n(n) {} |
| 1024 |
int n; |
| 1025 |
}; |
| 1026 |
constexpr int S::f() const { |
| 1027 |
return static_cast<const T*>(this)->n; // expected-note 5{{cannot cast}} |
| 1028 |
} |
| 1029 |
constexpr int S::g() const { |
| 1030 |
// FIXME: Better diagnostic for this. |
| 1031 |
return this->*(int(S::*))&T::n; // expected-note {{subexpression}} |
| 1032 |
} |
| 1033 |
// The T temporary is implicitly cast to an S subobject, but we can recover the |
| 1034 |
// T full-object via a base-to-derived cast, or a derived-to-base-casted member |
| 1035 |
// pointer. |
| 1036 |
static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to 'S().f()'}} |
| 1037 |
static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to 'S().g()'}} |
| 1038 |
constexpr S sobj; |
| 1039 |
constexpr const S& slref = sobj; |
| 1040 |
constexpr const S&& srref = S(); |
| 1041 |
constexpr const S *sptr = &sobj; |
| 1042 |
static_assert(sobj.f(), ""); // expected-error {{constant expression}} \ |
| 1043 |
expected-note {{in call to 'sobj.f()'}} |
| 1044 |
static_assert(sptr->f(), ""); // expected-error {{constant expression}} \ |
| 1045 |
expected-note {{in call to 'sptr->f()'}} |
| 1046 |
static_assert(slref.f(), ""); // expected-error {{constant expression}} \ |
| 1047 |
expected-note {{in call to 'slref.f()'}} |
| 1048 |
static_assert(srref.f(), ""); // expected-error {{constant expression}} \ |
| 1049 |
expected-note {{in call to 'srref.f()'}} |
| 1050 |
static_assert(T(3).f() == 3, ""); |
| 1051 |
static_assert(T(4).g() == 4, ""); |
| 1052 |
|
| 1053 |
constexpr int f(const S &s) { |
| 1054 |
return static_cast<const T&>(s).n; |
| 1055 |
} |
| 1056 |
constexpr int n = f(T(5)); |
| 1057 |
static_assert(f(T(5)) == 5, ""); |
| 1058 |
|
| 1059 |
constexpr bool b(int n) { return &n; } |
| 1060 |
static_assert(b(0), ""); |
| 1061 |
|
| 1062 |
struct NonLiteral { |
| 1063 |
NonLiteral(); // cxx23-note {{declared here}} |
| 1064 |
int f(); |
| 1065 |
}; |
| 1066 |
constexpr int k = NonLiteral().f(); // expected-error {{constant expression}} \ |
| 1067 |
// pre-cxx23-note {{non-literal type 'NonLiteral'}} \ |
| 1068 |
// cxx23-note {{non-constexpr constructor 'NonLiteral' cannot be used in a constant expression}} |
| 1069 |
|
| 1070 |
} |
| 1071 |
|
| 1072 |
namespace Union { |
| 1073 |
|
| 1074 |
union U { |
| 1075 |
int a; |
| 1076 |
int b; |
| 1077 |
}; |
| 1078 |
|
| 1079 |
constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; |
| 1080 |
static_assert(u[0].a == 0, ""); |
| 1081 |
static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}} |
| 1082 |
static_assert(u[1].b == 1, ""); |
| 1083 |
static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} |
| 1084 |
static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}} |
| 1085 |
static_assert((&(u[1]) + 1 + 1)->b == 3, ""); |
| 1086 |
|
| 1087 |
constexpr U v = {}; |
| 1088 |
static_assert(v.a == 0, ""); |
| 1089 |
|
| 1090 |
union Empty {}; |
| 1091 |
constexpr Empty e = {}; |
| 1092 |
|
| 1093 |
// Make sure we handle trivial copy constructors for unions. |
| 1094 |
constexpr U x = {42}; |
| 1095 |
constexpr U y = x; |
| 1096 |
static_assert(y.a == 42, ""); |
| 1097 |
static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}} |
| 1098 |
|
| 1099 |
} |
| 1100 |
|
| 1101 |
namespace MemberPointer { |
| 1102 |
struct A { |
| 1103 |
constexpr A(int n) : n(n) {} |
| 1104 |
int n; |
| 1105 |
constexpr int f() const { return n + 3; } |
| 1106 |
}; |
| 1107 |
constexpr A a(7); |
| 1108 |
static_assert(A(5).*&A::n == 5, ""); |
| 1109 |
static_assert((&a)->*&A::n == 7, ""); |
| 1110 |
static_assert((A(8).*&A::f)() == 11, ""); |
| 1111 |
static_assert(((&a)->*&A::f)() == 10, ""); |
| 1112 |
|
| 1113 |
struct B : A { |
| 1114 |
constexpr B(int n, int m) : A(n), m(m) {} |
| 1115 |
int m; |
| 1116 |
constexpr int g() const { return n + m + 1; } |
| 1117 |
}; |
| 1118 |
constexpr B b(9, 13); |
| 1119 |
static_assert(B(4, 11).*&A::n == 4, ""); |
| 1120 |
static_assert(B(4, 11).*&B::m == 11, ""); |
| 1121 |
static_assert(B(4, 11).*(int(A::*))&B::m == 11, ""); |
| 1122 |
static_assert((&b)->*&A::n == 9, ""); |
| 1123 |
static_assert((&b)->*&B::m == 13, ""); |
| 1124 |
static_assert((&b)->*(int(A::*))&B::m == 13, ""); |
| 1125 |
static_assert((B(4, 11).*&A::f)() == 7, ""); |
| 1126 |
static_assert((B(4, 11).*&B::g)() == 16, ""); |
| 1127 |
static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); |
| 1128 |
static_assert(((&b)->*&A::f)() == 12, ""); |
| 1129 |
static_assert(((&b)->*&B::g)() == 23, ""); |
| 1130 |
static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); |
| 1131 |
|
| 1132 |
struct S { |
| 1133 |
constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : |
| 1134 |
m(m), n(n), pf(pf), pn(pn) {} |
| 1135 |
constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} |
| 1136 |
|
| 1137 |
constexpr int f() const { return this->*pn; } |
| 1138 |
virtual int g() const; |
| 1139 |
|
| 1140 |
int m, n; |
| 1141 |
int (S::*pf)() const; |
| 1142 |
int S::*pn; |
| 1143 |
}; |
| 1144 |
|
| 1145 |
constexpr int S::*pm = &S::m; |
| 1146 |
constexpr int S::*pn = &S::n; |
| 1147 |
constexpr int (S::*pf)() const = &S::f; |
| 1148 |
constexpr int (S::*pg)() const = &S::g; |
| 1149 |
|
| 1150 |
constexpr S s(2, 5, &S::f, &S::m); |
| 1151 |
|
| 1152 |
static_assert((s.*&S::f)() == 2, ""); |
| 1153 |
static_assert((s.*s.pf)() == 2, ""); |
| 1154 |
|
| 1155 |
static_assert(pf == &S::f, ""); |
| 1156 |
static_assert(pf == s.*&S::pf, ""); |
| 1157 |
static_assert(pm == &S::m, ""); |
| 1158 |
static_assert(pm != pn, ""); |
| 1159 |
static_assert(s.pn != pn, ""); |
| 1160 |
static_assert(s.pn == pm, ""); |
| 1161 |
static_assert(pg != nullptr, ""); |
| 1162 |
static_assert(pf != nullptr, ""); |
| 1163 |
static_assert((int S::*)nullptr == nullptr, ""); |
| 1164 |
static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} |
| 1165 |
static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} |
| 1166 |
|
| 1167 |
template<int n> struct T : T<n-1> {}; |
| 1168 |
template<> struct T<0> { int n; }; |
| 1169 |
template<> struct T<30> : T<29> { int m; }; |
| 1170 |
|
| 1171 |
T<17> t17; |
| 1172 |
T<30> t30; |
| 1173 |
|
| 1174 |
constexpr int (T<10>::*deepn) = &T<0>::n; |
| 1175 |
static_assert(&(t17.*deepn) == &t17.n, ""); |
| 1176 |
static_assert(deepn == &T<2>::n, ""); |
| 1177 |
|
| 1178 |
constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; |
| 1179 |
constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} |
| 1180 |
static_assert(&(t30.*deepm) == &t30.m, ""); |
| 1181 |
static_assert(deepm == &T<50>::m, ""); |
| 1182 |
static_assert(deepm != deepn, ""); |
| 1183 |
|
| 1184 |
constexpr T<5> *p17_5 = &t17; |
| 1185 |
constexpr T<13> *p17_13 = (T<13>*)p17_5; |
| 1186 |
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>'}} |
| 1187 |
static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); |
| 1188 |
static_assert(&(p17_13->*deepn) == &t17.n, ""); |
| 1189 |
constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} |
| 1190 |
|
| 1191 |
constexpr T<5> *p30_5 = &t30; |
| 1192 |
constexpr T<23> *p30_23 = (T<23>*)p30_5; |
| 1193 |
constexpr T<13> *p30_13 = p30_23; |
| 1194 |
static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); |
| 1195 |
static_assert(&(p30_13->*deepn) == &t30.n, ""); |
| 1196 |
static_assert(&(p30_23->*deepn) == &t30.n, ""); |
| 1197 |
static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); |
| 1198 |
static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); |
| 1199 |
static_assert(&(p30_23->*deepm) == &t30.m, ""); |
| 1200 |
|
| 1201 |
struct Base { int n; }; |
| 1202 |
template<int N> struct Mid : Base {}; |
| 1203 |
struct Derived : Mid<0>, Mid<1> {}; |
| 1204 |
static_assert(&Mid<0>::n == &Mid<1>::n, ""); |
| 1205 |
static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n != |
| 1206 |
(int Derived::*)(int Mid<1>::*)&Mid<1>::n, ""); |
| 1207 |
static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, ""); |
| 1208 |
|
| 1209 |
constexpr int apply(const A &a, int (A::*f)() const) { |
| 1210 |
return (a.*f)(); |
| 1211 |
} |
| 1212 |
static_assert(apply(A(2), &A::f) == 5, ""); |
| 1213 |
} |
| 1214 |
|
| 1215 |
namespace ArrayBaseDerived { |
| 1216 |
|
| 1217 |
struct Base { |
| 1218 |
constexpr Base() {} |
| 1219 |
int n = 0; |
| 1220 |
}; |
| 1221 |
struct Derived : Base { |
| 1222 |
constexpr Derived() {} |
| 1223 |
constexpr const int *f() const { return &n; } |
| 1224 |
}; |
| 1225 |
|
| 1226 |
constexpr Derived a[10]; |
| 1227 |
constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); |
| 1228 |
constexpr Base *pb3 = const_cast<Derived*>(&a[3]); |
| 1229 |
static_assert(pb3 == pd3, ""); |
| 1230 |
|
| 1231 |
// pb3 does not point to an array element. |
| 1232 |
constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. |
| 1233 |
constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}} |
| 1234 |
constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}} |
| 1235 |
constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}} |
| 1236 |
constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}} |
| 1237 |
constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}} |
| 1238 |
constexpr Base *pb3a = pb4 - 1; |
| 1239 |
|
| 1240 |
// pb4 does not point to a Derived. |
| 1241 |
constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}} |
| 1242 |
constexpr Derived *pd3a = (Derived*)pb3a; |
| 1243 |
constexpr int pd3n = pd3a->n; |
| 1244 |
|
| 1245 |
// pd3a still points to the Derived array. |
| 1246 |
constexpr Derived *pd6 = pd3a + 3; |
| 1247 |
static_assert(pd6 == &a[6], ""); |
| 1248 |
constexpr Derived *pd9 = pd6 + 3; |
| 1249 |
constexpr Derived *pd10 = pd6 + 4; |
| 1250 |
constexpr int pd9n = pd9->n; // ok |
| 1251 |
constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}} |
| 1252 |
constexpr int pd0n = pd10[-10].n; |
| 1253 |
constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}} |
| 1254 |
|
| 1255 |
constexpr Base *pb9 = pd9; |
| 1256 |
constexpr const int *(Base::*pfb)() const = |
| 1257 |
static_cast<const int *(Base::*)() const>(&Derived::f); |
| 1258 |
static_assert((pb9->*pfb)() == &a[9].n, ""); |
| 1259 |
} |
| 1260 |
|
| 1261 |
namespace Complex { |
| 1262 |
|
| 1263 |
class complex { |
| 1264 |
int re, im; |
| 1265 |
public: |
| 1266 |
constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} |
| 1267 |
constexpr complex(const complex &o) : re(o.re), im(o.im) {} |
| 1268 |
constexpr complex operator-() const { return complex(-re, -im); } |
| 1269 |
friend constexpr complex operator+(const complex &l, const complex &r) { |
| 1270 |
return complex(l.re + r.re, l.im + r.im); |
| 1271 |
} |
| 1272 |
friend constexpr complex operator-(const complex &l, const complex &r) { |
| 1273 |
return l + -r; |
| 1274 |
} |
| 1275 |
friend constexpr complex operator*(const complex &l, const complex &r) { |
| 1276 |
return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); |
| 1277 |
} |
| 1278 |
friend constexpr bool operator==(const complex &l, const complex &r) { |
| 1279 |
return l.re == r.re && l.im == r.im; |
| 1280 |
} |
| 1281 |
constexpr bool operator!=(const complex &r) const { |
| 1282 |
return re != r.re || im != r.im; |
| 1283 |
} |
| 1284 |
constexpr int real() const { return re; } |
| 1285 |
constexpr int imag() const { return im; } |
| 1286 |
}; |
| 1287 |
|
| 1288 |
constexpr complex i = complex(0, 1); |
| 1289 |
constexpr complex k = (3 + 4*i) * (6 - 4*i); |
| 1290 |
static_assert(complex(1,0).real() == 1, ""); |
| 1291 |
static_assert(complex(1,0).imag() == 0, ""); |
| 1292 |
static_assert(((complex)1).imag() == 0, ""); |
| 1293 |
static_assert(k.real() == 34, ""); |
| 1294 |
static_assert(k.imag() == 12, ""); |
| 1295 |
static_assert(k - 34 == 12*i, ""); |
| 1296 |
static_assert((complex)1 == complex(1), ""); |
| 1297 |
static_assert((complex)1 != complex(0, 1), ""); |
| 1298 |
static_assert(complex(1) == complex(1), ""); |
| 1299 |
static_assert(complex(1) != complex(0, 1), ""); |
| 1300 |
constexpr complex makeComplex(int re, int im) { return complex(re, im); } |
| 1301 |
static_assert(makeComplex(1,0) == complex(1), ""); |
| 1302 |
static_assert(makeComplex(1,0) != complex(0, 1), ""); |
| 1303 |
|
| 1304 |
class complex_wrap : public complex { |
| 1305 |
public: |
| 1306 |
constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} |
| 1307 |
constexpr complex_wrap(const complex_wrap &o) : complex(o) {} |
| 1308 |
}; |
| 1309 |
|
| 1310 |
static_assert((complex_wrap)1 == complex(1), ""); |
| 1311 |
static_assert((complex)1 != complex_wrap(0, 1), ""); |
| 1312 |
static_assert(complex(1) == complex_wrap(1), ""); |
| 1313 |
static_assert(complex_wrap(1) != complex(0, 1), ""); |
| 1314 |
constexpr complex_wrap makeComplexWrap(int re, int im) { |
| 1315 |
return complex_wrap(re, im); |
| 1316 |
} |
| 1317 |
static_assert(makeComplexWrap(1,0) == complex(1), ""); |
| 1318 |
static_assert(makeComplexWrap(1,0) != complex(0, 1), ""); |
| 1319 |
|
| 1320 |
constexpr auto GH55390 = 1 / 65536j; // expected-note {{division by zero}} \ |
| 1321 |
// expected-error {{constexpr variable 'GH55390' must be initialized by a constant expression}} \ |
| 1322 |
// expected-warning {{imaginary constants are a GNU extension}} |
| 1323 |
} |
| 1324 |
|
| 1325 |
namespace PR11595 { |
| 1326 |
struct A { constexpr bool operator==(int x) const { return true; } }; |
| 1327 |
struct B { B(); A& x; }; // cxx23-note {{declared here}} |
| 1328 |
static_assert(B().x == 3, ""); // expected-error {{constant expression}} \ |
| 1329 |
// pre-cxx23-note {{non-literal type 'B' cannot be used in a constant expression}} \ |
| 1330 |
// cxx23-note {{non-constexpr constructor 'B' cannot be used in a constant expression}} |
| 1331 |
|
| 1332 |
constexpr bool f(int k) { // cxx11_20-error {{constexpr function never produces a constant expression}} |
| 1333 |
return B().x == k; // cxx11_20-note {{non-literal type 'B' cannot be used in a constant expression}} |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
namespace ExprWithCleanups { |
| 1338 |
struct A { A(); ~A(); int get(); }; |
| 1339 |
constexpr int get(bool FromA) { return FromA ? A().get() : 1; } |
| 1340 |
constexpr int n = get(false); |
| 1341 |
} |
| 1342 |
|
| 1343 |
namespace Volatile { |
| 1344 |
|
| 1345 |
volatile constexpr int n1 = 0; // expected-note {{here}} |
| 1346 |
volatile const int n2 = 0; // expected-note {{here}} |
| 1347 |
int n3 = 37; // expected-note {{declared here}} |
| 1348 |
|
| 1349 |
constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} |
| 1350 |
constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} |
| 1351 |
constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}} |
| 1352 |
constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}} |
| 1353 |
|
| 1354 |
struct T { int n; }; |
| 1355 |
const T t = { 42 }; // expected-note {{declared here}} |
| 1356 |
|
| 1357 |
constexpr int f(volatile int &&r) { |
| 1358 |
return r; // expected-note {{read of volatile-qualified type 'volatile int'}} |
| 1359 |
} |
| 1360 |
constexpr int g(volatile int &&r) { |
| 1361 |
return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}} |
| 1362 |
} |
| 1363 |
struct S { |
| 1364 |
int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}} |
| 1365 |
int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}} |
| 1366 |
int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}} |
| 1367 |
int m : t.n; // expected-warning{{width of bit-field 'm' (42 bits)}} expected-warning{{expression is not an integral constant expression}} expected-note{{read of non-constexpr variable 't' is not allowed}} |
| 1368 |
}; |
| 1369 |
|
| 1370 |
} |
| 1371 |
|
| 1372 |
namespace ExternConstexpr { |
| 1373 |
extern constexpr int n = 0; |
| 1374 |
extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}} |
| 1375 |
void f() { |
| 1376 |
extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}} |
| 1377 |
constexpr int j = 0; |
| 1378 |
constexpr int k; // expected-error {{constexpr variable 'k' must be initialized by a constant expression}} |
| 1379 |
} |
| 1380 |
|
| 1381 |
extern const int q; |
| 1382 |
constexpr int g() { return q; } // expected-note {{outside its lifetime}} |
| 1383 |
constexpr int q = g(); // expected-error {{constant expression}} expected-note {{in call}} |
| 1384 |
|
| 1385 |
extern int r; // cxx11_20-note {{here}} |
| 1386 |
constexpr int h() { return r; } // cxx11_20-error {{never produces a constant}} cxx11_20-note {{read of non-const}} |
| 1387 |
|
| 1388 |
struct S { int n; }; |
| 1389 |
extern const S s; |
| 1390 |
constexpr int x() { return s.n; } // expected-note {{outside its lifetime}} |
| 1391 |
constexpr S s = {x()}; // expected-error {{constant expression}} expected-note {{in call}} |
| 1392 |
} |
| 1393 |
|
| 1394 |
namespace ComplexConstexpr { |
| 1395 |
constexpr _Complex float test1 = {}; // expected-warning {{'_Complex' is a C99 extension}} |
| 1396 |
constexpr _Complex float test2 = {1}; // expected-warning {{'_Complex' is a C99 extension}} |
| 1397 |
constexpr _Complex double test3 = {1,2}; // expected-warning {{'_Complex' is a C99 extension}} |
| 1398 |
constexpr _Complex int test4 = {4}; // expected-warning {{'_Complex' is a C99 extension}} |
| 1399 |
constexpr _Complex int test5 = 4; // expected-warning {{'_Complex' is a C99 extension}} |
| 1400 |
constexpr _Complex int test6 = {5,6}; // expected-warning {{'_Complex' is a C99 extension}} |
| 1401 |
typedef _Complex float fcomplex; // expected-warning {{'_Complex' is a C99 extension}} |
| 1402 |
constexpr fcomplex test7 = fcomplex(); |
| 1403 |
|
| 1404 |
constexpr const double &t2r = __real test3; |
| 1405 |
constexpr const double &t2i = __imag test3; |
| 1406 |
static_assert(&t2r + 1 == &t2i, ""); |
| 1407 |
static_assert(t2r == 1.0, ""); |
| 1408 |
static_assert(t2i == 2.0, ""); |
| 1409 |
constexpr const double *t2p = &t2r; |
| 1410 |
static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}} |
| 1411 |
static_assert(t2p[0] == 1.0, ""); |
| 1412 |
static_assert(t2p[1] == 2.0, ""); |
| 1413 |
static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}} |
| 1414 |
static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}} |
| 1415 |
constexpr _Complex float *p = 0; // expected-warning {{'_Complex' is a C99 extension}} |
| 1416 |
constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{dereferencing a null pointer}} |
| 1417 |
constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{dereferencing a null pointer}} |
| 1418 |
constexpr const _Complex double *q = &test3 + 1; // expected-warning {{'_Complex' is a C99 extension}} |
| 1419 |
constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}} |
| 1420 |
constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}} |
| 1421 |
|
| 1422 |
static_assert(__real test6 == 5, ""); |
| 1423 |
static_assert(__imag test6 == 6, ""); |
| 1424 |
static_assert(&__imag test6 == &__real test6 + 1, ""); |
| 1425 |
} |
| 1426 |
|
| 1427 |
// _Atomic(T) is exactly like T for the purposes of constant expression |
| 1428 |
// evaluation.. |
| 1429 |
namespace Atomic { |
| 1430 |
constexpr _Atomic int n = 3; // expected-warning {{'_Atomic' is a C11 extension}} |
| 1431 |
|
| 1432 |
struct S { _Atomic(double) d; }; // expected-warning {{'_Atomic' is a C11 extension}} |
| 1433 |
constexpr S s = { 0.5 }; |
| 1434 |
constexpr double d1 = s.d; |
| 1435 |
constexpr double d2 = n; |
| 1436 |
constexpr _Atomic double d3 = n; // expected-warning {{'_Atomic' is a C11 extension}} |
| 1437 |
|
| 1438 |
constexpr _Atomic(int) n2 = d3; // expected-warning {{'_Atomic' is a C11 extension}} |
| 1439 |
static_assert(d1 == 0.5, ""); |
| 1440 |
static_assert(d3 == 3.0, ""); |
| 1441 |
|
| 1442 |
namespace PR16056 { |
| 1443 |
struct TestVar { |
| 1444 |
_Atomic(int) value; // expected-warning {{'_Atomic' is a C11 extension}} |
| 1445 |
constexpr TestVar(int value) : value(value) {} |
| 1446 |
}; |
| 1447 |
constexpr TestVar testVar{-1}; |
| 1448 |
static_assert(testVar.value == -1, ""); |
| 1449 |
} |
| 1450 |
|
| 1451 |
namespace PR32034 { |
| 1452 |
struct A {}; |
| 1453 |
struct B { _Atomic(A) a; }; // expected-warning {{'_Atomic' is a C11 extension}} |
| 1454 |
constexpr int n = (B(), B(), 0); |
| 1455 |
|
| 1456 |
struct C { constexpr C() {} void *self = this; }; |
| 1457 |
constexpr _Atomic(C) c = C(); // expected-warning {{'_Atomic' is a C11 extension}} |
| 1458 |
} |
| 1459 |
} |
| 1460 |
|
| 1461 |
namespace InstantiateCaseStmt { |
| 1462 |
template<int x> constexpr int f() { return x; } |
| 1463 |
template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; } |
| 1464 |
int gg(int c) { return g<4>(c); } |
| 1465 |
} |
| 1466 |
|
| 1467 |
namespace ConvertedConstantExpr { |
| 1468 |
extern int &m; |
| 1469 |
extern int &n; // expected-note 2{{declared here}} |
| 1470 |
|
| 1471 |
constexpr int k = 4; |
| 1472 |
int &m = const_cast<int&>(k); |
| 1473 |
|
| 1474 |
// If we have nothing more interesting to say, ensure we don't produce a |
| 1475 |
// useless note and instead just point to the non-constant subexpression. |
| 1476 |
enum class E { |
| 1477 |
em = m, |
| 1478 |
en = n, // expected-error {{enumerator value is not a constant expression}} cxx11_20-note {{initializer of 'n' is unknown}} cxx23-note {{read of non-constexpr variable 'n'}} |
| 1479 |
eo = (m + // expected-error {{not a constant expression}} |
| 1480 |
n // cxx11_20-note {{initializer of 'n' is unknown}} cxx23-note {{read of non-constexpr variable 'n'}} |
| 1481 |
), |
| 1482 |
eq = reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}} |
| 1483 |
}; |
| 1484 |
} |
| 1485 |
|
| 1486 |
namespace IndirectField { |
| 1487 |
struct S { |
| 1488 |
struct { // expected-warning {{GNU extension}} |
| 1489 |
union { // expected-warning {{declared in an anonymous struct}} |
| 1490 |
struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}} |
| 1491 |
int a; |
| 1492 |
int b; |
| 1493 |
}; |
| 1494 |
int c; |
| 1495 |
}; |
| 1496 |
int d; |
| 1497 |
}; |
| 1498 |
union { |
| 1499 |
int e; |
| 1500 |
int f; |
| 1501 |
}; |
| 1502 |
constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {} |
| 1503 |
constexpr S(int c, int d, int f) : c(c), d(d), f(f) {} |
| 1504 |
}; |
| 1505 |
|
| 1506 |
constexpr S s1(1, 2, 3, 4); |
| 1507 |
constexpr S s2(5, 6, 7); |
| 1508 |
|
| 1509 |
// FIXME: The diagnostics here do a very poor job of explaining which unnamed |
| 1510 |
// member is active and which is requested. |
| 1511 |
static_assert(s1.a == 1, ""); |
| 1512 |
static_assert(s1.b == 2, ""); |
| 1513 |
static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1514 |
static_assert(s1.d == 3, ""); |
| 1515 |
static_assert(s1.e == 4, ""); |
| 1516 |
static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1517 |
|
| 1518 |
static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1519 |
static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1520 |
static_assert(s2.c == 5, ""); |
| 1521 |
static_assert(s2.d == 6, ""); |
| 1522 |
static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} |
| 1523 |
static_assert(s2.f == 7, ""); |
| 1524 |
} |
| 1525 |
|
| 1526 |
// DR1405: don't allow reading mutable members in constant expressions. |
| 1527 |
namespace MutableMembers { |
| 1528 |
struct MM { |
| 1529 |
mutable int n; // expected-note 3{{declared here}} |
| 1530 |
} constexpr mm = { 4 }; |
| 1531 |
constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} |
| 1532 |
int x = (mm.n = 1, 3); |
| 1533 |
constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} |
| 1534 |
|
| 1535 |
// Here's one reason why allowing this would be a disaster... |
| 1536 |
template<int n> struct Id { int k = n; }; |
| 1537 |
int f() { |
| 1538 |
constexpr MM m = { 0 }; |
| 1539 |
++m.n; |
| 1540 |
return Id<m.n>().k; // expected-error {{not a constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} |
| 1541 |
} |
| 1542 |
|
| 1543 |
struct A { int n; }; |
| 1544 |
struct B { mutable A a; }; // expected-note {{here}} |
| 1545 |
struct C { B b; }; |
| 1546 |
constexpr C c[3] = {}; |
| 1547 |
constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}} |
| 1548 |
|
| 1549 |
struct D { int x; mutable int y; }; // expected-note {{here}} |
| 1550 |
constexpr D d1 = { 1, 2 }; |
| 1551 |
int l = ++d1.y; |
| 1552 |
constexpr D d2 = d1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}} |
| 1553 |
|
| 1554 |
struct E { |
| 1555 |
union { |
| 1556 |
int a; |
| 1557 |
mutable int b; // expected-note {{here}} |
| 1558 |
}; |
| 1559 |
}; |
| 1560 |
constexpr E e1 = {{1}}; |
| 1561 |
constexpr E e2 = e1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}} |
| 1562 |
|
| 1563 |
struct F { |
| 1564 |
union U { }; |
| 1565 |
mutable U u; |
| 1566 |
struct X { }; |
| 1567 |
mutable X x; |
| 1568 |
struct Y : X { X x; U u; }; |
| 1569 |
mutable Y y; |
| 1570 |
int n; |
| 1571 |
}; |
| 1572 |
// This is OK; we don't actually read any mutable state here. |
| 1573 |
constexpr F f1 = {}; |
| 1574 |
constexpr F f2 = f1; |
| 1575 |
|
| 1576 |
struct G { |
| 1577 |
struct X {}; |
| 1578 |
union U { X a; }; |
| 1579 |
mutable U u; // expected-note {{here}} |
| 1580 |
}; |
| 1581 |
constexpr G g1 = {}; |
| 1582 |
constexpr G g2 = g1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}} |
| 1583 |
constexpr G::U gu1 = {}; |
| 1584 |
constexpr G::U gu2 = gu1; |
| 1585 |
|
| 1586 |
union H { |
| 1587 |
mutable G::X gx; // expected-note {{here}} |
| 1588 |
}; |
| 1589 |
constexpr H h1 = {}; |
| 1590 |
constexpr H h2 = h1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}} |
| 1591 |
} |
| 1592 |
|
| 1593 |
namespace Fold { |
| 1594 |
|
| 1595 |
constexpr int n = (long)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}} |
| 1596 |
constexpr int m = fold((long)(char*)123); // ok |
| 1597 |
static_assert(m == 123, ""); |
| 1598 |
|
| 1599 |
} |
| 1600 |
|
| 1601 |
namespace DR1454 { |
| 1602 |
|
| 1603 |
constexpr const int &f(const int &n) { return n; } |
| 1604 |
constexpr int k1 = f(0); // ok |
| 1605 |
|
| 1606 |
struct Wrap { |
| 1607 |
const int &value; |
| 1608 |
}; |
| 1609 |
constexpr const Wrap &g(const Wrap &w) { return w; } |
| 1610 |
constexpr int k2 = g({0}).value; // ok |
| 1611 |
|
| 1612 |
// The temporary here has static storage duration, so we can bind a constexpr |
| 1613 |
// reference to it. |
| 1614 |
constexpr const int &i = 1; |
| 1615 |
constexpr const int j = i; |
| 1616 |
static_assert(j == 1, ""); |
| 1617 |
|
| 1618 |
// The temporary here is not const, so it can't be read outside the expression |
| 1619 |
// in which it was created (per the C++14 rules, which we use to avoid a C++11 |
| 1620 |
// defect). |
| 1621 |
constexpr int &&k = 1; // expected-note {{temporary created here}} |
| 1622 |
constexpr const int l = k; // expected-error {{constant expression}} expected-note {{read of temporary}} |
| 1623 |
|
| 1624 |
void f() { |
| 1625 |
// The temporary here has automatic storage duration, so we can't bind a |
| 1626 |
// constexpr reference to it. |
| 1627 |
constexpr const int &i = 1; // expected-error {{constant expression}} expected-note 2{{temporary}} |
| 1628 |
} |
| 1629 |
|
| 1630 |
} |
| 1631 |
|
| 1632 |
namespace RecursiveOpaqueExpr { |
| 1633 |
template<typename Iter> |
| 1634 |
constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) { |
| 1635 |
return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}} |
| 1636 |
} |
| 1637 |
|
| 1638 |
constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 }; |
| 1639 |
static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, ""); |
| 1640 |
|
| 1641 |
constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 }; |
| 1642 |
static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, ""); |
| 1643 |
|
| 1644 |
constexpr int arr3[] = { |
| 1645 |
1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, |
| 1646 |
1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, |
| 1647 |
1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, |
| 1648 |
1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, |
| 1649 |
1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, |
| 1650 |
1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, |
| 1651 |
1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, |
| 1652 |
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 1653 |
static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, ""); |
| 1654 |
} |
| 1655 |
|
| 1656 |
namespace VLASizeof { |
| 1657 |
|
| 1658 |
void f(int k) { // expected-note {{here}} |
| 1659 |
int arr[k]; // expected-warning {{Clang extension}} expected-note {{function parameter 'k'}} |
| 1660 |
constexpr int n = 1 + |
| 1661 |
sizeof(arr) // expected-error {{constant expression}} |
| 1662 |
* 3; |
| 1663 |
} |
| 1664 |
} |
| 1665 |
|
| 1666 |
namespace CompoundLiteral { |
| 1667 |
// Matching GCC, file-scope array compound literals initialized by constants |
| 1668 |
// are lifetime-extended. |
| 1669 |
constexpr int *p = (int*)(int[1]){3}; // expected-warning {{C99}} |
| 1670 |
static_assert(*p == 3, ""); // expected-error {{static assertion expression is not an integral constant expression}} |
| 1671 |
// expected-note@-1 {{subexpression not valid}} |
| 1672 |
// expected-note@-3 {{declared here}} |
| 1673 |
static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}} |
| 1674 |
// expected-error@-1 {{static assertion expression is not an integral constant expression}} |
| 1675 |
// expected-note@-2 {{subexpression not valid}} |
| 1676 |
// expected-note@-3 {{declared here}} |
| 1677 |
|
| 1678 |
// Other kinds are not. |
| 1679 |
struct X { int a[2]; }; |
| 1680 |
constexpr int *n = (X){1, 2}.a; // expected-warning {{C99}} expected-warning {{temporary}} |
| 1681 |
// expected-error@-1 {{constant expression}} |
| 1682 |
// expected-note@-2 {{pointer to subobject of temporary}} |
| 1683 |
// expected-note@-3 {{temporary created here}} |
| 1684 |
|
| 1685 |
void f() { |
| 1686 |
static constexpr int *p = (int*)(int[1]){3}; // expected-warning {{C99}} expected-warning {{temporary}} |
| 1687 |
// expected-error@-1 {{constant expression}} |
| 1688 |
// expected-note@-2 {{pointer to subobject of temporary}} |
| 1689 |
// expected-note@-3 {{temporary created here}} |
| 1690 |
static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}} |
| 1691 |
} |
| 1692 |
} |
| 1693 |
|
| 1694 |
namespace Vector { |
| 1695 |
typedef int __attribute__((vector_size(16))) VI4; |
| 1696 |
constexpr VI4 f(int n) { |
| 1697 |
return VI4 { n * 3, n + 4, n - 5, n / 6 }; |
| 1698 |
} |
| 1699 |
constexpr auto v1 = f(10); |
| 1700 |
|
| 1701 |
typedef double __attribute__((vector_size(32))) VD4; |
| 1702 |
constexpr VD4 g(int n) { |
| 1703 |
return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}} |
| 1704 |
} |
| 1705 |
constexpr auto v2 = g(4); |
| 1706 |
} |
| 1707 |
|
| 1708 |
// PR12626, redux |
| 1709 |
namespace InvalidClasses { |
| 1710 |
void test0() { |
| 1711 |
struct X; // expected-note {{forward declaration}} |
| 1712 |
struct Y { bool b; X x; }; // expected-error {{field has incomplete type}} |
| 1713 |
Y y; |
| 1714 |
auto& b = y.b; |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
namespace NamespaceAlias { |
| 1719 |
constexpr int f() { |
| 1720 |
namespace NS = NamespaceAlias; // cxx11-warning {{use of this statement in a constexpr function is a C++14 extension}} |
| 1721 |
return &NS::f != nullptr; |
| 1722 |
} |
| 1723 |
} |
| 1724 |
|
| 1725 |
// Constructors can be implicitly constexpr, even for a non-literal type. |
| 1726 |
namespace ImplicitConstexpr { |
| 1727 |
struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}} |
| 1728 |
struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; }; |
| 1729 |
struct S { R r; }; // expected-note 3{{here}} |
| 1730 |
struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; }; |
| 1731 |
struct U { T t; }; // cxx11_20-note 3{{here}} |
| 1732 |
static_assert(!__is_literal_type(Q), ""); |
| 1733 |
static_assert(!__is_literal_type(R), ""); |
| 1734 |
static_assert(!__is_literal_type(S), ""); |
| 1735 |
static_assert(!__is_literal_type(T), ""); |
| 1736 |
static_assert(!__is_literal_type(U), ""); |
| 1737 |
struct Test { |
| 1738 |
friend Q::Q() noexcept; // expected-error {{follows constexpr}} |
| 1739 |
friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}} |
| 1740 |
friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}} |
| 1741 |
friend S::S() noexcept; // expected-error {{follows constexpr}} |
| 1742 |
friend S::S(S&&) noexcept; // expected-error {{follows constexpr}} |
| 1743 |
friend S::S(const S&) noexcept; // expected-error {{follows constexpr}} |
| 1744 |
friend constexpr U::U() noexcept; // cxx11_20-error {{follows non-constexpr}} |
| 1745 |
friend constexpr U::U(U&&) noexcept; // cxx11_20-error {{follows non-constexpr}} |
| 1746 |
friend constexpr U::U(const U&) noexcept; // cxx11_20-error {{follows non-constexpr}} |
| 1747 |
}; |
| 1748 |
} |
| 1749 |
|
| 1750 |
// Indirectly test that an implicit lvalue to xvalue conversion performed for |
| 1751 |
// an NRVO move operation isn't implemented as CK_LValueToRValue. |
| 1752 |
namespace PR12826 { |
| 1753 |
struct Foo {}; |
| 1754 |
constexpr Foo id(Foo x) { return x; } |
| 1755 |
constexpr Foo res(id(Foo())); |
| 1756 |
} |
| 1757 |
|
| 1758 |
namespace PR13273 { |
| 1759 |
struct U { |
| 1760 |
int t; |
| 1761 |
U() = default; |
| 1762 |
}; |
| 1763 |
|
| 1764 |
struct S : U { |
| 1765 |
S() = default; |
| 1766 |
}; |
| 1767 |
|
| 1768 |
// S's default constructor isn't constexpr, because U's default constructor |
| 1769 |
// doesn't initialize 't', but it's trivial, so value-initialization doesn't |
| 1770 |
// actually call it. |
| 1771 |
static_assert(S{}.t == 0, ""); |
| 1772 |
} |
| 1773 |
|
| 1774 |
namespace PR12670 { |
| 1775 |
struct S { |
| 1776 |
constexpr S(int a0) : m(a0) {} |
| 1777 |
constexpr S() : m(6) {} |
| 1778 |
int m; |
| 1779 |
}; |
| 1780 |
constexpr S x[3] = { {4}, 5 }; |
| 1781 |
static_assert(x[0].m == 4, ""); |
| 1782 |
static_assert(x[1].m == 5, ""); |
| 1783 |
static_assert(x[2].m == 6, ""); |
| 1784 |
} |
| 1785 |
|
| 1786 |
// Indirectly test that an implicit lvalue-to-rvalue conversion is performed |
| 1787 |
// when a conditional operator has one argument of type void and where the other |
| 1788 |
// is a glvalue of class type. |
| 1789 |
namespace ConditionalLValToRVal { |
| 1790 |
struct A { |
| 1791 |
constexpr A(int a) : v(a) {} |
| 1792 |
int v; |
| 1793 |
}; |
| 1794 |
|
| 1795 |
constexpr A f(const A &a) { |
| 1796 |
return a.v == 0 ? throw a : a; |
| 1797 |
} |
| 1798 |
|
| 1799 |
constexpr A a(4); |
| 1800 |
static_assert(f(a).v == 4, ""); |
| 1801 |
} |
| 1802 |
|
| 1803 |
namespace TLS { |
| 1804 |
__thread int n; |
| 1805 |
int m; |
| 1806 |
|
| 1807 |
constexpr bool b = &n == &n; |
| 1808 |
|
| 1809 |
constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}} |
| 1810 |
|
| 1811 |
constexpr int *f() { return &n; } |
| 1812 |
constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}} |
| 1813 |
constexpr bool c = f() == f(); |
| 1814 |
|
| 1815 |
constexpr int *g() { return &m; } |
| 1816 |
constexpr int *r = g(); |
| 1817 |
} |
| 1818 |
|
| 1819 |
namespace Void { |
| 1820 |
constexpr void f() { return; } // cxx11-error{{constexpr function's return type 'void' is not a literal type}} |
| 1821 |
|
| 1822 |
void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}} |
| 1823 |
#define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__)) |
| 1824 |
template<typename T, size_t S> |
| 1825 |
constexpr T get(T (&a)[S], size_t k) { |
| 1826 |
return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}} |
| 1827 |
} |
| 1828 |
#undef ASSERT |
| 1829 |
template int get(int (&a)[4], size_t); |
| 1830 |
constexpr int arr[] = { 4, 1, 2, 3, 4 }; |
| 1831 |
static_assert(get(arr, 1) == 1, ""); |
| 1832 |
static_assert(get(arr, 4) == 4, ""); |
| 1833 |
static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \ |
| 1834 |
// expected-note{{in call to 'get<const int, 5UL>(arr, 0)'}} |
| 1835 |
} |
| 1836 |
|
| 1837 |
namespace std { struct type_info; } |
| 1838 |
|
| 1839 |
namespace TypeId { |
| 1840 |
struct A { virtual ~A(); }; |
| 1841 |
A f(); |
| 1842 |
A &g(); // cxx20_23-note {{declared here}} |
| 1843 |
constexpr auto &x = typeid(f()); |
| 1844 |
constexpr auto &y = typeid(g()); // expected-error{{constant expression}} |
| 1845 |
// cxx11-note@-1 {{typeid applied to expression of polymorphic type 'A' is not allowed in a constant expression}} |
| 1846 |
// expected-warning@-2 {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}} |
| 1847 |
// cxx20_23-note@-3 {{non-constexpr function 'g' cannot be used in a constant expression}} |
| 1848 |
} |
| 1849 |
|
| 1850 |
namespace PR14203 { |
| 1851 |
struct duration { |
| 1852 |
constexpr duration() {} |
| 1853 |
constexpr operator int() const { return 0; } |
| 1854 |
}; |
| 1855 |
// These are valid per P0859R0 (moved as DR). |
| 1856 |
template<typename T> void f() { |
| 1857 |
constexpr duration d = duration(); |
| 1858 |
} |
| 1859 |
int n = sizeof(short{duration(duration())}); |
| 1860 |
} |
| 1861 |
|
| 1862 |
namespace ArrayEltInit { |
| 1863 |
struct A { |
| 1864 |
constexpr A() : p(&p) {} |
| 1865 |
void *p; |
| 1866 |
}; |
| 1867 |
constexpr A a[10]; |
| 1868 |
static_assert(a[0].p == &a[0].p, ""); |
| 1869 |
static_assert(a[9].p == &a[9].p, ""); |
| 1870 |
static_assert(a[0].p != &a[9].p, ""); |
| 1871 |
static_assert(a[9].p != &a[0].p, ""); |
| 1872 |
|
| 1873 |
constexpr A b[10] = {}; |
| 1874 |
static_assert(b[0].p == &b[0].p, ""); |
| 1875 |
static_assert(b[9].p == &b[9].p, ""); |
| 1876 |
static_assert(b[0].p != &b[9].p, ""); |
| 1877 |
static_assert(b[9].p != &b[0].p, ""); |
| 1878 |
} |
| 1879 |
|
| 1880 |
namespace PR15884 { |
| 1881 |
struct S {}; |
| 1882 |
constexpr S f() { return {}; } |
| 1883 |
constexpr S *p = &f(); |
| 1884 |
// expected-error@-1 {{taking the address of a temporary}} |
| 1885 |
// expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}} |
| 1886 |
// expected-note@-3 {{pointer to temporary is not a constant expression}} |
| 1887 |
// expected-note@-4 {{temporary created here}} |
| 1888 |
} |
| 1889 |
|
| 1890 |
namespace AfterError { |
| 1891 |
constexpr int error() { // pre-cxx23-error {{no return statement in constexpr function}} |
| 1892 |
return foobar; // expected-error {{undeclared identifier}} |
| 1893 |
} // cxx23-note {{control reached end of constexpr function}} |
| 1894 |
constexpr int k = error(); // cxx23-error {{constexpr variable 'k' must be initialized by a constant expression}} \ |
| 1895 |
cxx23-note {{in call to 'error()'}} |
| 1896 |
} |
| 1897 |
|
| 1898 |
namespace std { |
| 1899 |
typedef decltype(sizeof(int)) size_t; |
| 1900 |
|
| 1901 |
template <class _E> |
| 1902 |
class initializer_list |
| 1903 |
{ |
| 1904 |
const _E* __begin_; |
| 1905 |
size_t __size_; |
| 1906 |
|
| 1907 |
constexpr initializer_list(const _E* __b, size_t __s) |
| 1908 |
: __begin_(__b), |
| 1909 |
__size_(__s) |
| 1910 |
{} |
| 1911 |
|
| 1912 |
public: |
| 1913 |
typedef _E value_type; |
| 1914 |
typedef const _E& reference; |
| 1915 |
typedef const _E& const_reference; |
| 1916 |
typedef size_t size_type; |
| 1917 |
|
| 1918 |
typedef const _E* iterator; |
| 1919 |
typedef const _E* const_iterator; |
| 1920 |
|
| 1921 |
constexpr initializer_list() : __begin_(nullptr), __size_(0) {} |
| 1922 |
|
| 1923 |
constexpr size_t size() const {return __size_;} |
| 1924 |
constexpr const _E* begin() const {return __begin_;} |
| 1925 |
constexpr const _E* end() const {return __begin_ + __size_;} |
| 1926 |
}; |
| 1927 |
} |
| 1928 |
|
| 1929 |
namespace InitializerList { |
| 1930 |
constexpr int sum(const int *b, const int *e) { |
| 1931 |
return b != e ? *b + sum(b+1, e) : 0; |
| 1932 |
} |
| 1933 |
constexpr int sum(std::initializer_list<int> ints) { |
| 1934 |
return sum(ints.begin(), ints.end()); |
| 1935 |
} |
| 1936 |
static_assert(sum({1, 2, 3, 4, 5}) == 15, ""); |
| 1937 |
|
| 1938 |
static_assert(*std::initializer_list<int>{1, 2, 3}.begin() == 1, ""); |
| 1939 |
static_assert(std::initializer_list<int>{1, 2, 3}.begin()[2] == 3, ""); |
| 1940 |
|
| 1941 |
namespace DR2126 { |
| 1942 |
constexpr std::initializer_list<float> il = {1.0, 2.0, 3.0}; |
| 1943 |
static_assert(il.begin()[1] == 2.0, ""); |
| 1944 |
} |
| 1945 |
} |
| 1946 |
|
| 1947 |
namespace StmtExpr { |
| 1948 |
struct A { int k; }; |
| 1949 |
void f() { |
| 1950 |
static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}} |
| 1951 |
constexpr auto a = ({ A(); }); // expected-warning {{extension}} |
| 1952 |
} |
| 1953 |
constexpr int g(int k) { |
| 1954 |
return ({ // expected-warning {{extension}} |
| 1955 |
const int x = k; |
| 1956 |
x * x; |
| 1957 |
}); |
| 1958 |
} |
| 1959 |
static_assert(g(123) == 15129, ""); |
| 1960 |
constexpr int h() { // cxx11_20-error {{never produces a constant}} |
| 1961 |
return ({ // expected-warning {{extension}} |
| 1962 |
return 0; // cxx11_20-note {{not supported}} |
| 1963 |
1; |
| 1964 |
}); |
| 1965 |
} |
| 1966 |
} |
| 1967 |
|
| 1968 |
namespace VirtualFromBase { |
| 1969 |
struct S1 { |
| 1970 |
virtual int f() const; |
| 1971 |
}; |
| 1972 |
struct S2 { |
| 1973 |
virtual int f(); |
| 1974 |
}; |
| 1975 |
template <typename T> struct X : T { |
| 1976 |
constexpr X() {} |
| 1977 |
double d = 0.0; |
| 1978 |
constexpr int f() { return sizeof(T); } // cxx11-warning {{will not be implicitly 'const' in C++14}} |
| 1979 |
}; |
| 1980 |
|
| 1981 |
// Virtual f(), not OK. |
| 1982 |
constexpr X<X<S1>> xxs1; |
| 1983 |
constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1); |
| 1984 |
static_assert(p->f() == sizeof(X<S1>), ""); |
| 1985 |
// cxx11-error@-1 {{not an integral constant expression}} |
| 1986 |
// cxx11-note@-2 {{call to virtual function}} |
| 1987 |
// cxx20_23-error@-3 {{static assertion failed}} |
| 1988 |
// cxx20_23-note@-4 {{8 == 16}} |
| 1989 |
|
| 1990 |
// Non-virtual f(), OK. |
| 1991 |
constexpr X<X<S2>> xxs2; |
| 1992 |
constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2); |
| 1993 |
static_assert(q->f() == sizeof(S2), ""); // cxx20_23-error {{static assertion failed}} \ |
| 1994 |
// cxx20_23-note {{16 == 8}} |
| 1995 |
} |
| 1996 |
|
| 1997 |
namespace ConstexprConstructorRecovery { |
| 1998 |
class X { |
| 1999 |
public: |
| 2000 |
enum E : short { |
| 2001 |
headers = 0x1, |
| 2002 |
middlefile = 0x2, |
| 2003 |
choices = 0x4 |
| 2004 |
}; |
| 2005 |
constexpr X() noexcept {}; |
| 2006 |
protected: |
| 2007 |
E val{0}; // cxx11-error {{cannot initialize a member subobject of type 'E' with an rvalue of type 'int'}} cxx11-note {{here}} |
| 2008 |
}; |
| 2009 |
// FIXME: We should avoid issuing this follow-on diagnostic. |
| 2010 |
constexpr X x{}; // cxx11-error {{constant expression}} cxx11-note {{not initialized}} |
| 2011 |
} |
| 2012 |
|
| 2013 |
namespace Lifetime { |
| 2014 |
void f() { |
| 2015 |
constexpr int &n = n; // expected-error {{constant expression}} cxx23-note {{reference to 'n' is not a constant expression}} cxx23-note {{address of non-static constexpr variable 'n' may differ}} expected-warning {{not yet bound to a value}} |
| 2016 |
// cxx11_20-note@-1 {{use of reference outside its lifetime is not allowed in a constant expression}} |
| 2017 |
constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}} |
| 2018 |
} |
| 2019 |
|
| 2020 |
constexpr int &get(int &&n) { return n; } |
| 2021 |
// cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}} |
| 2022 |
constexpr int &&get_rv(int &&n) { return static_cast<int&&>(n); } |
| 2023 |
struct S { |
| 2024 |
int &&r; |
| 2025 |
int &s; |
| 2026 |
int t; |
| 2027 |
constexpr S() : r(get_rv(0)), s(get(0)), t(r) {} // cxx11_20-note {{read of object outside its lifetime}} |
| 2028 |
constexpr S(int) : r(get_rv(0)), s(get(0)), t(s) {} // cxx11_20-note {{read of object outside its lifetime}} |
| 2029 |
}; |
| 2030 |
constexpr int k1 = S().t; // expected-error {{constant expression}} cxx11_20-note {{in call}} |
| 2031 |
constexpr int k2 = S(0).t; // expected-error {{constant expression}} cxx11_20-note {{in call}} |
| 2032 |
|
| 2033 |
struct Q { |
| 2034 |
int n = 0; |
| 2035 |
constexpr int f() const { return 0; } |
| 2036 |
}; |
| 2037 |
constexpr Q *out_of_lifetime(Q q) { return &q; } // expected-warning {{address of stack}} |
| 2038 |
constexpr int k3 = out_of_lifetime({})->n; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}} |
| 2039 |
constexpr int k4 = out_of_lifetime({})->f(); // expected-error {{constant expression}} expected-note {{member call on object outside its lifetime}} |
| 2040 |
|
| 2041 |
constexpr int null = ((Q*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}} |
| 2042 |
|
| 2043 |
Q q; |
| 2044 |
Q qa[3]; |
| 2045 |
constexpr int pte0 = (&q)[0].f(); // ok |
| 2046 |
constexpr int pte1 = (&q)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}} |
| 2047 |
constexpr int pte2 = qa[2].f(); // ok |
| 2048 |
constexpr int pte3 = qa[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}} |
| 2049 |
|
| 2050 |
constexpr Q cq; |
| 2051 |
constexpr Q cqa[3]; |
| 2052 |
constexpr int cpte0 = (&cq)[0].f(); // ok |
| 2053 |
constexpr int cpte1 = (&cq)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}} |
| 2054 |
constexpr int cpte2 = cqa[2].f(); // ok |
| 2055 |
constexpr int cpte3 = cqa[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}} |
| 2056 |
|
| 2057 |
// FIXME: There's no way if we can tell if the first call here is valid; it |
| 2058 |
// depends on the active union member. Should we reject for that reason? |
| 2059 |
union U { |
| 2060 |
int n; |
| 2061 |
Q q; |
| 2062 |
}; |
| 2063 |
U u1 = {0}; |
| 2064 |
constexpr U u2 = {0}; |
| 2065 |
constexpr int union_member1 = u1.q.f(); |
| 2066 |
constexpr int union_member2 = u2.q.f(); // expected-error {{constant expression}} expected-note {{member call on member 'q' of union with active member 'n'}} |
| 2067 |
|
| 2068 |
struct R { // expected-note {{field init}} |
| 2069 |
struct Inner { constexpr int f() const { return 0; } }; |
| 2070 |
int a = b.f(); // expected-warning {{uninitialized}} expected-note 2{{member call on object outside its lifetime}} |
| 2071 |
Inner b; |
| 2072 |
}; |
| 2073 |
constexpr R r; // expected-error {{constant expression}} expected-note {{in call}} expected-note {{implicit default constructor for 'Lifetime::R' first required here}} |
| 2074 |
void rf() { |
| 2075 |
constexpr R r; // expected-error {{constant expression}} expected-note {{in call}} |
| 2076 |
} |
| 2077 |
} |
| 2078 |
|
| 2079 |
namespace Bitfields { |
| 2080 |
struct A { |
| 2081 |
bool b : 1; |
| 2082 |
unsigned u : 5; |
| 2083 |
int n : 5; |
| 2084 |
bool b2 : 3; |
| 2085 |
unsigned u2 : 74; // expected-warning {{exceeds the width of its type}} |
| 2086 |
int n2 : 81; // expected-warning {{exceeds the width of its type}} |
| 2087 |
}; |
| 2088 |
|
| 2089 |
constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}} |
| 2090 |
static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 && |
| 2091 |
a.u2 + 1 == 0 && a.n2 == 0x7fffffff, |
| 2092 |
"bad truncation of bitfield values"); |
| 2093 |
|
| 2094 |
struct B { |
| 2095 |
int n : 3; |
| 2096 |
constexpr B(int k) : n(k) {} |
| 2097 |
}; |
| 2098 |
static_assert(B(3).n == 3, ""); |
| 2099 |
static_assert(B(4).n == -4, ""); |
| 2100 |
static_assert(B(7).n == -1, ""); |
| 2101 |
static_assert(B(8).n == 0, ""); |
| 2102 |
static_assert(B(-1).n == -1, ""); |
| 2103 |
static_assert(B(-8889).n == -1, ""); |
| 2104 |
|
| 2105 |
namespace PR16755 { |
| 2106 |
struct X { |
| 2107 |
int x : 1; |
| 2108 |
constexpr static int f(int x) { |
| 2109 |
return X{x}.x; |
| 2110 |
} |
| 2111 |
}; |
| 2112 |
static_assert(X::f(3) == -1, "3 should truncate to -1"); |
| 2113 |
static_assert(X::f(1) == -1, "1 should truncate to -1"); |
| 2114 |
} |
| 2115 |
|
| 2116 |
struct HasUnnamedBitfield { |
| 2117 |
unsigned a; |
| 2118 |
unsigned : 20; |
| 2119 |
unsigned b; |
| 2120 |
|
| 2121 |
constexpr HasUnnamedBitfield() : a(), b() {} |
| 2122 |
constexpr HasUnnamedBitfield(unsigned a, unsigned b) : a(a), b(b) {} |
| 2123 |
}; |
| 2124 |
|
| 2125 |
void testUnnamedBitfield() { |
| 2126 |
const HasUnnamedBitfield zero{}; |
| 2127 |
int a = 1 / zero.b; // expected-warning {{division by zero is undefined}} |
| 2128 |
const HasUnnamedBitfield oneZero{1, 0}; |
| 2129 |
int b = 1 / oneZero.b; // expected-warning {{division by zero is undefined}} |
| 2130 |
} |
| 2131 |
|
| 2132 |
union UnionWithUnnamedBitfield { |
| 2133 |
int : 3; |
| 2134 |
int n; |
| 2135 |
}; |
| 2136 |
static_assert(UnionWithUnnamedBitfield().n == 0, ""); |
| 2137 |
static_assert(UnionWithUnnamedBitfield{}.n == 0, ""); |
| 2138 |
static_assert(UnionWithUnnamedBitfield{1}.n == 1, ""); |
| 2139 |
} |
| 2140 |
|
| 2141 |
namespace ZeroSizeTypes { |
| 2142 |
constexpr int (*p1)[0] = 0, (*p2)[0] = 0; |
| 2143 |
constexpr int k = p2 - p1; |
| 2144 |
// expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}} |
| 2145 |
// expected-note@-2 {{subtraction of pointers to type 'int[0]' of zero size}} |
| 2146 |
|
| 2147 |
int arr[5][0]; |
| 2148 |
constexpr int f() { // cxx11_20-error {{never produces a constant expression}} |
| 2149 |
return &arr[3] - &arr[0]; // cxx11_20-note {{subtraction of pointers to type 'int[0]' of zero size}} |
| 2150 |
} |
| 2151 |
} |
| 2152 |
|
| 2153 |
namespace BadDefaultInit { |
| 2154 |
template<int N> struct X { static const int n = N; }; |
| 2155 |
|
| 2156 |
struct A { // expected-error {{default member initializer for 'k' needed within definition of enclosing class}} |
| 2157 |
int k = // expected-note {{default member initializer declared here}} |
| 2158 |
X<A().k>::n; // expected-note {{in evaluation of exception specification for 'BadDefaultInit::A::A' needed here}} |
| 2159 |
}; |
| 2160 |
|
| 2161 |
struct B { |
| 2162 |
constexpr B( |
| 2163 |
int k = X<B().k>::n) : // expected-error {{default argument to function 'B' that is declared later}} expected-note {{here}} |
| 2164 |
k(k) {} |
| 2165 |
int k; |
| 2166 |
}; |
| 2167 |
} |
| 2168 |
|
| 2169 |
namespace NeverConstantTwoWays { |
| 2170 |
// If we see something non-constant but foldable followed by something |
| 2171 |
// non-constant and not foldable, we want the first diagnostic, not the |
| 2172 |
// second. |
| 2173 |
constexpr int f(int n) { // cxx11_20-error {{never produces a constant expression}} |
| 2174 |
return (int *)(long)&n == &n ? // cxx11_20-note {{reinterpret_cast}} |
| 2175 |
1 / 0 : // expected-warning {{division by zero}} |
| 2176 |
0; |
| 2177 |
} |
| 2178 |
|
| 2179 |
constexpr int n = // expected-error {{must be initialized by a constant expression}} |
| 2180 |
(int *)(long)&n == &n ? // expected-note {{reinterpret_cast}} |
| 2181 |
1 / 0 : |
| 2182 |
0; |
| 2183 |
} |
| 2184 |
|
| 2185 |
namespace PR17800 { |
| 2186 |
struct A { |
| 2187 |
constexpr int operator()() const { return 0; } |
| 2188 |
}; |
| 2189 |
template <typename ...T> constexpr int sink(T ...) { |
| 2190 |
return 0; |
| 2191 |
} |
| 2192 |
template <int ...N> constexpr int run() { |
| 2193 |
return sink(A()() + N ...); |
| 2194 |
} |
| 2195 |
constexpr int k = run<1, 2, 3>(); |
| 2196 |
} |
| 2197 |
|
| 2198 |
namespace BuiltinStrlen { |
| 2199 |
constexpr const char *a = "foo\0quux"; |
| 2200 |
constexpr char b[] = "foo\0quux"; |
| 2201 |
constexpr int f() { return 'u'; } |
| 2202 |
constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 }; |
| 2203 |
|
| 2204 |
static_assert(__builtin_strlen("foo") == 3, ""); |
| 2205 |
static_assert(__builtin_strlen("foo\0quux") == 3, ""); |
| 2206 |
static_assert(__builtin_strlen("foo\0quux" + 4) == 4, ""); |
| 2207 |
static_assert(__builtin_strlen("foo") + 1 + "foo" == "foo", ""); // expected-error {{static assertion expression is not an integral constant expression}} |
| 2208 |
// expected-note@-1 {{comparison against pointer '&"foo"[4]' that points past the end of a complete object has unspecified value}} |
| 2209 |
|
| 2210 |
constexpr bool check(const char *p) { |
| 2211 |
return __builtin_strlen(p) == 3 && |
| 2212 |
__builtin_strlen(p + 1) == 2 && |
| 2213 |
__builtin_strlen(p + 2) == 1 && |
| 2214 |
__builtin_strlen(p + 3) == 0 && |
| 2215 |
__builtin_strlen(p + 4) == 4 && |
| 2216 |
__builtin_strlen(p + 5) == 3 && |
| 2217 |
__builtin_strlen(p + 6) == 2 && |
| 2218 |
__builtin_strlen(p + 7) == 1 && |
| 2219 |
__builtin_strlen(p + 8) == 0; |
| 2220 |
} |
| 2221 |
|
| 2222 |
static_assert(check(a), ""); |
| 2223 |
static_assert(check(b), ""); |
| 2224 |
static_assert(check(c), ""); |
| 2225 |
|
| 2226 |
constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 2227 |
constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 2228 |
constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 2229 |
|
| 2230 |
constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} |
| 2231 |
constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} |
| 2232 |
constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} |
| 2233 |
|
| 2234 |
// FIXME: The diagnostic here could be better. |
| 2235 |
constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator. |
| 2236 |
constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}} |
| 2237 |
} |
| 2238 |
|
| 2239 |
namespace PR19010 { |
| 2240 |
struct Empty {}; |
| 2241 |
struct Empty2 : Empty {}; |
| 2242 |
struct Test : Empty2 { |
| 2243 |
constexpr Test() {} |
| 2244 |
Empty2 array[2]; |
| 2245 |
}; |
| 2246 |
void test() { constexpr Test t; } |
| 2247 |
} |
| 2248 |
|
| 2249 |
void PR21327(int a, int b) { |
| 2250 |
static_assert(&a + 1 != &b, ""); // expected-error {{constant expression}} |
| 2251 |
// expected-note@-1 {{comparison against pointer '&a + 1' that points past the end of a complete object has unspecified value}} |
| 2252 |
} |
| 2253 |
|
| 2254 |
namespace EmptyClass { |
| 2255 |
struct E1 {} e1; |
| 2256 |
union E2 {} e2; // expected-note {{here}} |
| 2257 |
struct E3 : E1 {} e3; |
| 2258 |
|
| 2259 |
// The defaulted copy constructor for an empty class does not read any |
| 2260 |
// members. The defaulted copy constructor for an empty union reads the |
| 2261 |
// object representation. |
| 2262 |
constexpr E1 e1b(e1); |
| 2263 |
constexpr E2 e2b(e2); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}} |
| 2264 |
constexpr E3 e3b(e3); |
| 2265 |
} |
| 2266 |
|
| 2267 |
namespace PR21786 { |
| 2268 |
extern void (*start[])(); |
| 2269 |
extern void (*end[])(); |
| 2270 |
static_assert(&start != &end, ""); // expected-error {{constant expression}} |
| 2271 |
// expected-note@-1 {{comparison of pointers '&start' and '&end' to unrelated zero-sized objects}} |
| 2272 |
static_assert(&start != nullptr, ""); |
| 2273 |
|
| 2274 |
struct Foo; |
| 2275 |
struct Bar { |
| 2276 |
static const Foo x; |
| 2277 |
static const Foo y; |
| 2278 |
}; |
| 2279 |
static_assert(&Bar::x != nullptr, ""); |
| 2280 |
static_assert(&Bar::x != &Bar::y, ""); |
| 2281 |
} |
| 2282 |
|
| 2283 |
namespace PR21859 { |
| 2284 |
constexpr int Fun() { return; } // expected-error {{non-void constexpr function 'Fun' should return a value}} |
| 2285 |
constexpr int Var = Fun(); |
| 2286 |
|
| 2287 |
template <typename T> constexpr int FunT1() { return; } // expected-error {{non-void constexpr function 'FunT1' should return a value}} |
| 2288 |
template <typename T> constexpr int FunT2() { return 0; } |
| 2289 |
template <> constexpr int FunT2<double>() { return 0; } |
| 2290 |
template <> constexpr int FunT2<int>() { return; } // expected-error {{non-void constexpr function 'FunT2<int>' should return a value}} |
| 2291 |
} |
| 2292 |
|
| 2293 |
struct InvalidRedef { |
| 2294 |
int f; // expected-note{{previous definition is here}} |
| 2295 |
constexpr int f(void); // expected-error{{redefinition of 'f'}} cxx11-warning{{will not be implicitly 'const'}} |
| 2296 |
}; |
| 2297 |
|
| 2298 |
namespace PR17938 { |
| 2299 |
template <typename T> constexpr T const &f(T const &x) { return x; } |
| 2300 |
|
| 2301 |
struct X {}; |
| 2302 |
struct Y : X {}; |
| 2303 |
struct Z : Y { constexpr Z() {} }; |
| 2304 |
|
| 2305 |
static constexpr auto z = f(Z()); |
| 2306 |
} |
| 2307 |
|
| 2308 |
namespace PR24597 { |
| 2309 |
struct A { |
| 2310 |
int x, *p; |
| 2311 |
constexpr A() : x(0), p(&x) {} |
| 2312 |
constexpr A(const A &a) : x(a.x), p(&x) {} |
| 2313 |
}; |
| 2314 |
constexpr A f() { return A(); } |
| 2315 |
constexpr A g() { return f(); } |
| 2316 |
constexpr int a = *f().p; |
| 2317 |
constexpr int b = *g().p; |
| 2318 |
} |
| 2319 |
|
| 2320 |
namespace IncompleteClass { |
| 2321 |
struct XX { |
| 2322 |
static constexpr int f(XX*) { return 1; } // expected-note {{here}} |
| 2323 |
friend constexpr int g(XX*) { return 2; } // expected-note {{here}} |
| 2324 |
|
| 2325 |
static constexpr int i = f(static_cast<XX*>(nullptr)); // expected-error {{constexpr variable 'i' must be initialized by a constant expression}} expected-note {{undefined function 'f' cannot be used in a constant expression}} |
| 2326 |
static constexpr int j = g(static_cast<XX*>(nullptr)); // expected-error {{constexpr variable 'j' must be initialized by a constant expression}} expected-note {{undefined function 'g' cannot be used in a constant expression}} |
| 2327 |
}; |
| 2328 |
} |
| 2329 |
|
| 2330 |
namespace InheritedCtor { |
| 2331 |
struct A { constexpr A(int) {} }; |
| 2332 |
|
| 2333 |
struct B : A { int n; using A::A; }; // expected-note {{here}} |
| 2334 |
constexpr B b(0); // expected-error {{constant expression}} cxx11_20-note {{derived class}}\ |
| 2335 |
// cxx23-note {{not initialized}} |
| 2336 |
|
| 2337 |
struct C : A { using A::A; struct { union { int n, m = 0; }; union { int a = 0; }; int k = 0; }; struct {}; union {}; }; // expected-warning 6{{}} |
| 2338 |
constexpr C c(0); |
| 2339 |
|
| 2340 |
struct D : A { |
| 2341 |
using A::A; // cxx11-note {{here}} |
| 2342 |
struct { // expected-warning {{extension}} |
| 2343 |
union { // expected-warning {{extension}} |
| 2344 |
int n; |
| 2345 |
}; |
| 2346 |
}; |
| 2347 |
}; |
| 2348 |
constexpr D d(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}} |
| 2349 |
|
| 2350 |
struct E : virtual A { using A::A; }; // expected-note {{here}} |
| 2351 |
// cxx20_23-note@-1 {{struct with virtual base class is not a literal type}} |
| 2352 |
// We wrap a function around this to avoid implicit zero-initialization |
| 2353 |
// happening first; the zero-initialization step would produce the same |
| 2354 |
// error and defeat the point of this test. |
| 2355 |
void f() { |
| 2356 |
constexpr E e(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}} |
| 2357 |
// cxx20_23-error@-1 {{constexpr variable cannot have non-literal type}} |
| 2358 |
} |
| 2359 |
// FIXME: This produces a note with no source location. |
| 2360 |
//constexpr E e(0); |
| 2361 |
|
| 2362 |
struct W { constexpr W(int n) : w(n) {} int w; }; |
| 2363 |
struct X : W { using W::W; int x = 2; }; |
| 2364 |
struct Y : X { using X::X; int y = 3; }; |
| 2365 |
struct Z : Y { using Y::Y; int z = 4; }; |
| 2366 |
constexpr Z z(1); |
| 2367 |
static_assert(z.w == 1 && z.x == 2 && z.y == 3 && z.z == 4, ""); |
| 2368 |
} |
| 2369 |
|
| 2370 |
|
| 2371 |
namespace PR28366 { |
| 2372 |
namespace ns1 { |
| 2373 |
|
| 2374 |
void f(char c) { //expected-note{{declared here}} |
| 2375 |
//cxx11_20-note@-1{{declared here}} |
| 2376 |
struct X { |
| 2377 |
static constexpr char f() { // cxx11_20-error {{never produces a constant expression}} |
| 2378 |
return c; //expected-error{{reference to local}} cxx11_20-note{{function parameter}} |
| 2379 |
} |
| 2380 |
}; |
| 2381 |
int I = X::f(); |
| 2382 |
} |
| 2383 |
|
| 2384 |
void g() { |
| 2385 |
const int c = 'c'; |
| 2386 |
static const int d = 'd'; |
| 2387 |
struct X { |
| 2388 |
static constexpr int f() { |
| 2389 |
return c + d; |
| 2390 |
} |
| 2391 |
}; |
| 2392 |
static_assert(X::f() == 'c' + 'd',""); |
| 2393 |
} |
| 2394 |
|
| 2395 |
|
| 2396 |
} // end ns1 |
| 2397 |
|
| 2398 |
} //end ns PR28366 |
| 2399 |
|
| 2400 |
namespace PointerArithmeticOverflow { |
| 2401 |
int n; |
| 2402 |
int a[1]; |
| 2403 |
constexpr int *b = &n + 1 + (long)-1; |
| 2404 |
constexpr int *c = &n + 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}} |
| 2405 |
constexpr int *d = &n + 1 - (unsigned long)1; |
| 2406 |
constexpr int *e = a + 1 + (long)-1; |
| 2407 |
constexpr int *f = a + 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}} |
| 2408 |
constexpr int *g = a + 1 - (unsigned long)1; |
| 2409 |
|
| 2410 |
constexpr int *p = (&n + 1) + (unsigned __int128)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}} |
| 2411 |
constexpr int *q = (&n + 1) - (unsigned __int128)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element -3402}} |
| 2412 |
constexpr int *r = &(&n + 1)[(unsigned __int128)-1]; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}} |
| 2413 |
} |
| 2414 |
|
| 2415 |
namespace PR40430 { |
| 2416 |
struct S { |
| 2417 |
char c[10] = "asdf"; |
| 2418 |
constexpr char foo() const { return c[3]; } |
| 2419 |
}; |
| 2420 |
static_assert(S().foo() == 'f', ""); |
| 2421 |
} |
| 2422 |
|
| 2423 |
namespace PR41854 { |
| 2424 |
struct e { operator int(); }; |
| 2425 |
struct f { e c; }; |
| 2426 |
int a; |
| 2427 |
f &d = reinterpret_cast<f&>(a); |
| 2428 |
unsigned b = d.c; |
| 2429 |
} |
| 2430 |
|
| 2431 |
namespace array_size { |
| 2432 |
template<int N> struct array { |
| 2433 |
static constexpr int size() { return N; } |
| 2434 |
}; |
| 2435 |
template<typename T> void f1(T t) { |
| 2436 |
constexpr int k = t.size(); |
| 2437 |
} |
| 2438 |
template<typename T> void f2(const T &t) { // cxx11_20-note 2{{declared here}} |
| 2439 |
constexpr int k = t.size(); // cxx11_20-error 2{{constexpr variable 'k' must be initialized by a constant expression}} cxx11_20-note 2{{function parameter 't' with unknown value cannot be used in a constant expression}} |
| 2440 |
} |
| 2441 |
template<typename T> void f3(const T &t) { |
| 2442 |
constexpr int k = T::size(); |
| 2443 |
} |
| 2444 |
void g(array<3> a) { |
| 2445 |
f1(a); |
| 2446 |
f2(a); // cxx11_20-note {{in instantiation of function template}} |
| 2447 |
f3(a); |
| 2448 |
} |
| 2449 |
|
| 2450 |
template<int N> struct array_nonstatic { |
| 2451 |
constexpr int size() const { return N; } |
| 2452 |
}; |
| 2453 |
void h(array_nonstatic<3> a) { |
| 2454 |
f1(a); |
| 2455 |
f2(a); // cxx11_20-note {{instantiation of}} |
| 2456 |
} |
| 2457 |
//static_assert(f2(array_size::array<3>{})); |
| 2458 |
} |
| 2459 |
|
| 2460 |
namespace flexible_array { |
| 2461 |
struct A { int x; char arr[]; }; // expected-warning {{C99}} expected-note {{here}} |
| 2462 |
constexpr A a = {1}; |
| 2463 |
static_assert(a.x == 1, ""); |
| 2464 |
static_assert(&a.arr != nullptr, ""); |
| 2465 |
static_assert(a.arr[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}} |
| 2466 |
static_assert(a.arr[1], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}} |
| 2467 |
|
| 2468 |
constexpr A b[] = {{1}, {2}, {3}}; // expected-warning {{flexible array member}} |
| 2469 |
static_assert(b[0].x == 1, ""); |
| 2470 |
static_assert(b[1].x == 2, ""); |
| 2471 |
static_assert(b[2].x == 3, ""); |
| 2472 |
static_assert(b[2].arr[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}} |
| 2473 |
|
| 2474 |
// Flexible array initialization is currently not supported by constant |
| 2475 |
// evaluation. Make sure we emit an error message, for now. |
| 2476 |
constexpr A c = {1, 2, 3}; // expected-error {{constexpr variable 'c' must be initialized by a constant expression}} |
| 2477 |
// expected-note@-1 {{flexible array initialization is not yet supported}} |
| 2478 |
// expected-warning@-2 {{flexible array initialization is a GNU extension}} |
| 2479 |
} |
| 2480 |
|
| 2481 |
void local_constexpr_var() { |
| 2482 |
constexpr int a = 0; // expected-note {{address of non-static constexpr variable 'a' may differ on each invocation of the enclosing function; add 'static' to give it a constant address}} |
| 2483 |
constexpr const int *p = &a; // expected-error {{constant expression}} expected-note {{pointer to 'a' is not a constant expression}} |
| 2484 |
} |
| 2485 |
|
| 2486 |
namespace GH50055 { |
| 2487 |
// Enums without fixed underlying type |
| 2488 |
enum E1 {e11=-4, e12=4}; |
| 2489 |
enum E2 {e21=0, e22=4}; |
| 2490 |
enum E3 {e31=-4, e32=1024}; |
| 2491 |
enum E4 {e41=0}; |
| 2492 |
// Empty but as-if it had a single enumerator with value 0 |
| 2493 |
enum EEmpty {}; |
| 2494 |
|
| 2495 |
// Enum with fixed underlying type because the underlying type is explicitly specified |
| 2496 |
enum EFixed : int {efixed1=-4, efixed2=4}; |
| 2497 |
// Enum with fixed underlying type because it is scoped |
| 2498 |
enum class EScoped {escoped1=-4, escoped2=4}; |
| 2499 |
|
| 2500 |
enum EMaxInt {emaxint1=-1, emaxint2=__INT_MAX__}; |
| 2501 |
|
| 2502 |
enum NumberType {}; |
| 2503 |
|
| 2504 |
E2 testDefaultArgForParam(E2 e2Param = (E2)-1) { // ok, not a constant expression context |
| 2505 |
E2 e2LocalInit = e2Param; // ok, not a constant expression context |
| 2506 |
return e2LocalInit; |
| 2507 |
} |
| 2508 |
|
| 2509 |
#include <enum-constexpr-conversion-system-header.h> |
| 2510 |
|
| 2511 |
void testValueInRangeOfEnumerationValues() { |
| 2512 |
constexpr E1 x1 = static_cast<E1>(-8); |
| 2513 |
constexpr E1 x2 = static_cast<E1>(8); |
| 2514 |
// expected-error@-1 {{constexpr variable 'x2' must be initialized by a constant expression}} |
| 2515 |
// expected-note@-2 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}} |
| 2516 |
E1 x2b = static_cast<E1>(8); // ok, not a constant expression context |
| 2517 |
static_assert(static_cast<E1>(8), ""); |
| 2518 |
// expected-error@-1 {{static assertion expression is not an integral constant expression}} |
| 2519 |
// expected-note@-2 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}} |
| 2520 |
|
| 2521 |
constexpr E2 x3 = static_cast<E2>(-8); |
| 2522 |
// expected-error@-1 {{constexpr variable 'x3' must be initialized by a constant expression}} |
| 2523 |
// expected-note@-2 {{integer value -8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}} |
| 2524 |
constexpr E2 x4 = static_cast<E2>(0); |
| 2525 |
constexpr E2 x5 = static_cast<E2>(8); |
| 2526 |
// expected-error@-1 {{constexpr variable 'x5' must be initialized by a constant expression}} |
| 2527 |
// expected-note@-2 {{integer value 8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}} |
| 2528 |
|
| 2529 |
constexpr E3 x6 = static_cast<E3>(-2048); |
| 2530 |
constexpr E3 x7 = static_cast<E3>(-8); |
| 2531 |
constexpr E3 x8 = static_cast<E3>(0); |
| 2532 |
constexpr E3 x9 = static_cast<E3>(8); |
| 2533 |
constexpr E3 x10 = static_cast<E3>(2048); |
| 2534 |
// expected-error@-1 {{constexpr variable 'x10' must be initialized by a constant expression}} |
| 2535 |
// expected-note@-2 {{integer value 2048 is outside the valid range of values [-2048, 2047] for the enumeration type 'E3'}} |
| 2536 |
|
| 2537 |
constexpr E4 x11 = static_cast<E4>(0); |
| 2538 |
constexpr E4 x12 = static_cast<E4>(1); |
| 2539 |
constexpr E4 x13 = static_cast<E4>(2); |
| 2540 |
// expected-error@-1 {{constexpr variable 'x13' must be initialized by a constant expression}} |
| 2541 |
// expected-note@-2 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'E4'}} |
| 2542 |
|
| 2543 |
constexpr EEmpty x14 = static_cast<EEmpty>(0); |
| 2544 |
constexpr EEmpty x15 = static_cast<EEmpty>(1); |
| 2545 |
constexpr EEmpty x16 = static_cast<EEmpty>(2); |
| 2546 |
// expected-error@-1 {{constexpr variable 'x16' must be initialized by a constant expression}} |
| 2547 |
// expected-note@-2 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'EEmpty'}} |
| 2548 |
|
| 2549 |
constexpr EFixed x17 = static_cast<EFixed>(100); |
| 2550 |
constexpr EScoped x18 = static_cast<EScoped>(100); |
| 2551 |
|
| 2552 |
constexpr EMaxInt x19 = static_cast<EMaxInt>(__INT_MAX__-1); |
| 2553 |
constexpr EMaxInt x20 = static_cast<EMaxInt>((long)__INT_MAX__+1); |
| 2554 |
// expected-error@-1 {{constexpr variable 'x20' must be initialized by a constant expression}} |
| 2555 |
// expected-note@-2 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}} |
| 2556 |
|
| 2557 |
const NumberType neg_one = (NumberType) ((NumberType) 0 - (NumberType) 1); // ok, not a constant expression context |
| 2558 |
constexpr NumberType neg_one_constexpr = neg_one; |
| 2559 |
// expected-error@-1 {{constexpr variable 'neg_one_constexpr' must be initialized by a constant expression}} |
| 2560 |
// expected-note@-2 {{initializer of 'neg_one' is not a constant expression}} |
| 2561 |
// expected-note@-4 {{declared here}} |
| 2562 |
|
| 2563 |
CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE; |
| 2564 |
// expected-error@-1 {{constexpr variable 'system_enum' must be initialized by a constant expression}} |
| 2565 |
// expected-note@-2 {{integer value 123 is outside the valid range of values [0, 1] for the enumeration type 'SystemEnum'}} |
| 2566 |
} |
| 2567 |
|
| 2568 |
template<class T, unsigned size> struct Bitfield { |
| 2569 |
static constexpr T max = static_cast<T>((1 << size) - 1); |
| 2570 |
// cxx11-error@-1 {{constexpr variable 'max' must be initialized by a constant expression}} |
| 2571 |
// cxx11-note@-2 {{integer value 15 is outside the valid range of values [0, 7] for the enumeration type 'E2'}} |
| 2572 |
}; |
| 2573 |
|
| 2574 |
void testValueInRangeOfEnumerationValuesViaTemplate() { |
| 2575 |
Bitfield<E2, 3> good; |
| 2576 |
Bitfield<E2, 4> bad; // cxx11-note {{in instantiation}} |
| 2577 |
} |
| 2578 |
|
| 2579 |
enum SortOrder { |
| 2580 |
AscendingOrder, |
| 2581 |
DescendingOrder |
| 2582 |
}; |
| 2583 |
|
| 2584 |
class A { |
| 2585 |
static void f(SortOrder order); |
| 2586 |
}; |
| 2587 |
|
| 2588 |
void A::f(SortOrder order) { |
| 2589 |
if (order == SortOrder(-1)) // ok, not a constant expression context |
| 2590 |
return; |
| 2591 |
} |
| 2592 |
} |
| 2593 |
|
| 2594 |
GH50055::E2 GlobalInitNotCE1 = (GH50055::E2)-1; // ok, not a constant expression context |
| 2595 |
GH50055::E2 GlobalInitNotCE2 = GH50055::testDefaultArgForParam(); // ok, not a constant expression context |
| 2596 |
constexpr GH50055::E2 GlobalInitCE = (GH50055::E2)-1; |
| 2597 |
// expected-error@-1 {{constexpr variable 'GlobalInitCE' must be initialized by a constant expression}} |
| 2598 |
// expected-note@-2 {{integer value -1 is outside the valid range of values [0, 7] for the enumeration type 'E2'}} |
| 2599 |
|
| 2600 |
namespace GH112140 { |
| 2601 |
struct S { |
| 2602 |
constexpr S(const int &a = ) { } // expected-error {{expected expression}} |
| 2603 |
}; |
| 2604 |
|
| 2605 |
void foo() { |
| 2606 |
constexpr S s[2] = { }; // expected-error {{constexpr variable 's' must be initialized by a constant expression}} |
| 2607 |
} |
| 2608 |
} |
| 2609 |
|
| 2610 |
namespace DoubleCapture { |
| 2611 |
int DC() { |
| 2612 |
int a = 1000; |
| 2613 |
static auto f = |
| 2614 |
[a, &a] { // expected-error {{'a' can appear only once in a capture list}} |
| 2615 |
}; |
| 2616 |
} |
| 2617 |
} |
| 2618 |
|
| 2619 |
namespace GH150709 { |
| 2620 |
struct C { }; |
| 2621 |
struct D : C { |
| 2622 |
constexpr int f() const { return 1; }; |
| 2623 |
}; |
| 2624 |
struct E : C { }; |
| 2625 |
struct F : D { }; |
| 2626 |
struct G : E { }; |
| 2627 |
|
| 2628 |
constexpr C c1, c2[2]; |
| 2629 |
constexpr D d1, d2[2]; |
| 2630 |
constexpr E e1, e2[2]; |
| 2631 |
constexpr F f; |
| 2632 |
constexpr G g; |
| 2633 |
|
| 2634 |
constexpr auto mp = static_cast<int (C::*)() const>(&D::f); |
| 2635 |
|
| 2636 |
// sanity checks for fix of GH150709 (unchanged behavior) |
| 2637 |
static_assert((c1.*mp)() == 1, ""); // expected-error {{constant expression}} |
| 2638 |
static_assert((d1.*mp)() == 1, ""); |
| 2639 |
static_assert((f.*mp)() == 1, ""); |
| 2640 |
static_assert((c2[0].*mp)() == 1, ""); // expected-error {{constant expression}} |
| 2641 |
static_assert((d2[0].*mp)() == 1, ""); |
| 2642 |
|
| 2643 |
// incorrectly undiagnosed before fix of GH150709 |
| 2644 |
static_assert((e1.*mp)() == 1, ""); // expected-error {{constant expression}} |
| 2645 |
static_assert((e2[0].*mp)() == 1, ""); // expected-error {{constant expression}} |
| 2646 |
static_assert((g.*mp)() == 1, ""); // expected-error {{constant expression}} |
| 2647 |
} |
| 2648 |
|
| 2649 |
namespace GH154567 { |
| 2650 |
struct T { |
| 2651 |
int i; |
| 2652 |
}; |
| 2653 |
|
| 2654 |
struct S { |
| 2655 |
struct { // expected-warning {{GNU extension}} |
| 2656 |
T val; |
| 2657 |
}; |
| 2658 |
constexpr S() : val() {} |
| 2659 |
}; |
| 2660 |
|
| 2661 |
constexpr S s{}; |
| 2662 |
static_assert(s.val.i == 0, ""); |
| 2663 |
} |