PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/test/SemaCXX/constant-expression-cxx11.cpp

https://bitbucket.org/larsivi/amber-clang
C++ | 1457 lines | 1155 code | 247 blank | 55 comment | 295 complexity | 235047ddff4e69506d220ab596d97464 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file