constant-expression-cxx11.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. // RUN: %clang_cc1 -triple i686-linux -fsyntax-only -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. // FIXME: Produce the 'add ()' fixit for this.
  76. case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}} expected-note {{non-literal type '<bound member function type>'}}
  77. case id(1):
  78. return;
  79. }
  80. }
  81. }
  82. extern int &Recurse1;
  83. int &Recurse2 = Recurse1; // expected-note 2{{declared here}} expected-note {{initializer of 'Recurse1' is not a constant expression}}
  84. int &Recurse1 = Recurse2; // expected-note {{declared here}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
  85. constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
  86. extern const int RecurseA;
  87. const int RecurseB = RecurseA; // expected-note {{declared here}}
  88. const int RecurseA = 10;
  89. constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
  90. namespace MemberEnum {
  91. struct WithMemberEnum {
  92. enum E { A = 42 };
  93. } wme;
  94. static_assert(wme.A == 42, "");
  95. }
  96. namespace DefaultArguments {
  97. const int z = int();
  98. constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
  99. return a + b + *c + d;
  100. }
  101. const int four = 4;
  102. constexpr int eight = 8;
  103. constexpr const int twentyseven = 27;
  104. static_assert(Sum() == 0, "");
  105. static_assert(Sum(1) == 1, "");
  106. static_assert(Sum(1, four) == 5, "");
  107. static_assert(Sum(1, eight, &twentyseven) == 36, "");
  108. static_assert(Sum(1, 2, &four, eight) == 15, "");
  109. }
  110. namespace Ellipsis {
  111. // Note, values passed through an ellipsis can't actually be used.
  112. constexpr int F(int a, ...) { return a; }
  113. static_assert(F(0) == 0, "");
  114. static_assert(F(1, 0) == 1, "");
  115. static_assert(F(2, "test") == 2, "");
  116. static_assert(F(3, &F) == 3, "");
  117. int k = 0; // expected-note {{here}}
  118. static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
  119. }
  120. namespace Recursion {
  121. constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
  122. static_assert(fib(11) == 89, "");
  123. constexpr int gcd_inner(int a, int b) {
  124. return b == 0 ? a : gcd_inner(b, a % b);
  125. }
  126. constexpr int gcd(int a, int b) {
  127. return gcd_inner(max(a, b), min(a, b));
  128. }
  129. static_assert(gcd(1749237, 5628959) == 7, "");
  130. }
  131. namespace FunctionCast {
  132. // When folding, we allow functions to be cast to different types. Such
  133. // cast functions cannot be called, even if they're constexpr.
  134. constexpr int f() { return 1; }
  135. typedef double (*DoubleFn)();
  136. typedef int (*IntFn)();
  137. int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
  138. int b[(int)IntFn(f)()]; // ok
  139. }
  140. namespace StaticMemberFunction {
  141. struct S {
  142. static constexpr int k = 42;
  143. static constexpr int f(int n) { return n * k + 2; }
  144. } s;
  145. constexpr int n = s.f(19);
  146. static_assert(S::f(19) == 800, "");
  147. static_assert(s.f(19) == 800, "");
  148. static_assert(n == 800, "");
  149. constexpr int (*sf1)(int) = &S::f;
  150. constexpr int (*sf2)(int) = &s.f;
  151. constexpr const int *sk = &s.k;
  152. }
  153. namespace ParameterScopes {
  154. const int k = 42;
  155. constexpr const int &ObscureTheTruth(const int &a) { return a; } // expected-note 3{{reference to 'a' cannot be returned from a constexpr function}}
  156. constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
  157. return ObscureTheTruth(b ? a : k); // expected-note 2{{in call to 'ObscureTheTruth(a)'}}
  158. }
  159. static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
  160. constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
  161. constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}}
  162. // If ObscureTheTruth returns a reference to 'a', the result is not a
  163. // constant expression even though 'a' is still in scope.
  164. return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}}
  165. }
  166. static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
  167. constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnNonstaticRef(1, 0)'}}
  168. constexpr int InternalReturnJunk(int n) {
  169. // FIXME: We should reject this: it never produces a constant expression.
  170. return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
  171. }
  172. constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
  173. constexpr int LToR(int &n) { return n; }
  174. constexpr int GrabCallersArgument(bool which, int a, int b) {
  175. return LToR(which ? b : a);
  176. }
  177. static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
  178. static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
  179. }
  180. namespace Pointers {
  181. constexpr int f(int n, const int *a, const int *b, const int *c) {
  182. return n == 0 ? 0 : *a + f(n-1, b, c, a);
  183. }
  184. const int x = 1, y = 10, z = 100;
  185. static_assert(f(23, &x, &y, &z) == 788, "");
  186. constexpr int g(int n, int a, int b, int c) {
  187. return f(n, &a, &b, &c);
  188. }
  189. static_assert(g(23, x, y, z) == 788, "");
  190. }
  191. namespace FunctionPointers {
  192. constexpr int Double(int n) { return 2 * n; }
  193. constexpr int Triple(int n) { return 3 * n; }
  194. constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
  195. constexpr int Quadruple(int n) { return Twice(Double, n); }
  196. constexpr auto Select(int n) -> int (*)(int) {
  197. return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
  198. }
  199. constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
  200. static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
  201. constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}}
  202. }
  203. namespace PointerComparison {
  204. int x, y;
  205. static_assert(&x == &y, "false"); // expected-error {{false}}
  206. static_assert(&x != &y, "");
  207. constexpr bool g1 = &x == &y;
  208. constexpr bool g2 = &x != &y;
  209. constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
  210. constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
  211. constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
  212. constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
  213. struct S { int x, y; } s;
  214. static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
  215. static_assert(&s.x != &s.y, "");
  216. static_assert(&s.x <= &s.y, "");
  217. static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
  218. static_assert(&s.x < &s.y, "");
  219. static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
  220. static_assert(0 == &y, "false"); // expected-error {{false}}
  221. static_assert(0 != &y, "");
  222. constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
  223. constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
  224. constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
  225. constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
  226. static_assert(&x == 0, "false"); // expected-error {{false}}
  227. static_assert(&x != 0, "");
  228. constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
  229. constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
  230. constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
  231. constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
  232. static_assert(&x == &x, "");
  233. static_assert(&x != &x, "false"); // expected-error {{false}}
  234. static_assert(&x <= &x, "");
  235. static_assert(&x >= &x, "");
  236. static_assert(&x < &x, "false"); // expected-error {{false}}
  237. static_assert(&x > &x, "false"); // expected-error {{false}}
  238. constexpr S* sptr = &s;
  239. constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
  240. struct Str {
  241. // FIXME: In C++ mode, we should say 'integral' not 'integer'
  242. int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
  243. expected-warning {{not integer constant expression}} \
  244. expected-note {{dynamic_cast is not allowed in a constant expression}}
  245. int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
  246. expected-warning {{not integer constant expression}} \
  247. expected-note {{reinterpret_cast is not allowed in a constant expression}}
  248. int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
  249. expected-warning {{not integer constant expression}} \
  250. expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
  251. int d : (S*)(42) == (S*)(42); // \
  252. expected-warning {{not integer constant expression}} \
  253. expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
  254. int e : (Str*)(sptr) == (Str*)(sptr); // \
  255. expected-warning {{not integer constant expression}} \
  256. expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
  257. int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \
  258. expected-warning {{not integer constant expression}} \
  259. expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
  260. int g : (S*)(void*)(sptr) == sptr; // \
  261. expected-warning {{not integer constant expression}} \
  262. expected-note {{cast from 'void *' is not allowed in a constant expression}}
  263. };
  264. extern char externalvar[];
  265. constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
  266. constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
  267. static_assert(0 != "foo", "");
  268. }
  269. namespace MaterializeTemporary {
  270. constexpr int f(const int &r) { return r; }
  271. constexpr int n = f(1);
  272. constexpr bool same(const int &a, const int &b) { return &a == &b; }
  273. constexpr bool sameTemporary(const int &n) { return same(n, n); }
  274. static_assert(n, "");
  275. static_assert(!same(4, 4), "");
  276. static_assert(same(n, n), "");
  277. static_assert(sameTemporary(9), "");
  278. }
  279. constexpr int strcmp_ce(const char *p, const char *q) {
  280. return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
  281. }
  282. namespace StringLiteral {
  283. template<typename Char>
  284. constexpr int MangleChars(const Char *p) {
  285. return *p + 3 * (*p ? MangleChars(p+1) : 0);
  286. }
  287. static_assert(MangleChars("constexpr!") == 1768383, "");
  288. static_assert(MangleChars(u"constexpr!") == 1768383, "");
  289. static_assert(MangleChars(U"constexpr!") == 1768383, "");
  290. constexpr char c0 = "nought index"[0];
  291. constexpr char c1 = "nice index"[10];
  292. 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}}
  293. // FIXME: block the pointer arithmetic with undefined behavior here
  294. constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{read of dereferenced one-past-the-end pointer}}
  295. constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}}
  296. constexpr const char *p = "test" + 2;
  297. static_assert(*p == 's', "");
  298. constexpr const char *max_iter(const char *a, const char *b) {
  299. return *a < *b ? b : a;
  300. }
  301. constexpr const char *max_element(const char *a, const char *b) {
  302. return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
  303. }
  304. constexpr char str[] = "the quick brown fox jumped over the lazy dog";
  305. constexpr const char *max = max_element(begin(str), end(str));
  306. static_assert(*max == 'z', "");
  307. static_assert(max == str + 38, "");
  308. static_assert(strcmp_ce("hello world", "hello world") == 0, "");
  309. static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
  310. static_assert(strcmp_ce("constexpr", "test") < 0, "");
  311. static_assert(strcmp_ce("", " ") < 0, "");
  312. struct S {
  313. 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}}
  314. };
  315. struct T {
  316. char c[6];
  317. constexpr T() : c{"foo"} {}
  318. };
  319. constexpr T t;
  320. static_assert(t.c[0] == 'f', "");
  321. static_assert(t.c[1] == 'o', "");
  322. static_assert(t.c[2] == 'o', "");
  323. static_assert(t.c[3] == 0, "");
  324. static_assert(t.c[4] == 0, "");
  325. static_assert(t.c[5] == 0, "");
  326. static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
  327. }
  328. namespace Array {
  329. template<typename Iter>
  330. constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
  331. return begin == end ? 0 : *begin + Sum(begin+1, end);
  332. }
  333. constexpr int xs[] = { 1, 2, 3, 4, 5 };
  334. constexpr int ys[] = { 5, 4, 3, 2, 1 };
  335. constexpr int sum_xs = Sum(begin(xs), end(xs));
  336. static_assert(sum_xs == 15, "");
  337. constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
  338. const int *xs, const int *ys, int c) {
  339. return n ? F(
  340. *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
  341. *ys,
  342. ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
  343. expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
  344. expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
  345. : c;
  346. }
  347. constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
  348. constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
  349. static_assert(InnerProduct == 35, "");
  350. constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
  351. constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
  352. static_assert(DiffProd == 8, "");
  353. static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
  354. expected-error {{constant expression}} \
  355. expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
  356. constexpr const int *p = xs + 3;
  357. constexpr int xs4 = p[1]; // ok
  358. constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
  359. constexpr int xs0 = p[-3]; // ok
  360. // FIXME: check pointer arithmetic here
  361. constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
  362. constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  363. static_assert(zs[0][0][0][0] == 1, "");
  364. static_assert(zs[1][1][1][1] == 16, "");
  365. static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
  366. static_assert((&zs[0][0][0][2])[-1] == 2, "");
  367. static_assert(**(**(zs + 1) + 1) == 11, "");
  368. static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, "");
  369. constexpr int fail(const int &p) {
  370. return (&p)[64]; // expected-note {{read of dereferenced one-past-the-end pointer}}
  371. }
  372. static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1)) == 11, ""); // \
  373. expected-error {{static_assert expression is not an integral constant expression}} \
  374. expected-note {{in call to 'fail(zs[1][0][1][0])'}}
  375. constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
  376. constexpr int SumNonzero(const int *p) {
  377. return *p + (*p ? SumNonzero(p+1) : 0);
  378. }
  379. constexpr int CountZero(const int *p, const int *q) {
  380. return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
  381. }
  382. static_assert(SumNonzero(arr) == 6, "");
  383. static_assert(CountZero(arr, arr + 40) == 36, "");
  384. struct ArrayElem {
  385. constexpr ArrayElem() : n(0) {}
  386. int n;
  387. constexpr int f() { return n; }
  388. };
  389. struct ArrayRVal {
  390. constexpr ArrayRVal() {}
  391. ArrayElem elems[10];
  392. };
  393. static_assert(ArrayRVal().elems[3].f() == 0, "");
  394. }
  395. namespace DependentValues {
  396. struct I { int n; typedef I V[10]; };
  397. I::V x, y;
  398. template<bool B> struct S {
  399. int k;
  400. void f() {
  401. I::V &cells = B ? x : y;
  402. I &i = cells[k];
  403. switch (i.n) {}
  404. }
  405. };
  406. }
  407. namespace Class {
  408. struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
  409. constexpr int fn(const A &a) { return a.k; }
  410. static_assert(fn(A(4,5)) == 9, "");
  411. struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
  412. struct C {
  413. constexpr C(C *this_) : m(42), n(this_->m) {} // ok
  414. int m, n;
  415. };
  416. struct D {
  417. C c;
  418. constexpr D() : c(&c) {}
  419. };
  420. static_assert(D().c.n == 42, "");
  421. struct E {
  422. constexpr E() : p(&p) {} // expected-note {{pointer to temporary cannot be used to initialize a member in a constant expression}}
  423. void *p;
  424. };
  425. constexpr const E &e1 = E(); // expected-error {{constant expression}} expected-note {{in call to 'E()'}} expected-note {{temporary created here}}
  426. // This is a constant expression if we elide the copy constructor call, and
  427. // is not a constant expression if we don't! But we do, so it is.
  428. constexpr E e2 = E();
  429. static_assert(e2.p == &e2.p, "");
  430. constexpr E e3;
  431. static_assert(e3.p == &e3.p, "");
  432. extern const class F f;
  433. struct F {
  434. constexpr F() : p(&f.p) {}
  435. const void *p;
  436. };
  437. constexpr F f;
  438. struct G {
  439. struct T {
  440. constexpr T(T *p) : u1(), u2(p) {}
  441. union U1 {
  442. constexpr U1() {}
  443. int a, b = 42;
  444. } u1;
  445. union U2 {
  446. constexpr U2(T *p) : c(p->u1.b) {}
  447. int c, d;
  448. } u2;
  449. } t;
  450. constexpr G() : t(&t) {}
  451. } constexpr g;
  452. static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
  453. static_assert(g.t.u1.b == 42, "");
  454. static_assert(g.t.u2.c == 42, "");
  455. static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
  456. struct S {
  457. int a, b;
  458. const S *p;
  459. double d;
  460. const char *q;
  461. constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
  462. };
  463. S global(43, &global);
  464. static_assert(S(15, &global).b == 15, "");
  465. constexpr bool CheckS(const S &s) {
  466. return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
  467. }
  468. static_assert(CheckS(S(27, &global)), "");
  469. struct Arr {
  470. char arr[3];
  471. constexpr Arr() : arr{'x', 'y', 'z'} {}
  472. };
  473. constexpr int hash(Arr &&a) {
  474. return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
  475. }
  476. constexpr int k = hash(Arr());
  477. static_assert(k == 0x007a7978, "");
  478. struct AggregateInit {
  479. const char &c;
  480. int n;
  481. double d;
  482. int arr[5];
  483. void *p;
  484. };
  485. constexpr AggregateInit agg1 = { "hello"[0] };
  486. static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
  487. static_assert(agg1.n == 0, "");
  488. static_assert(agg1.d == 0.0, "");
  489. // FIXME: check pointer arithmetic here.
  490. static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
  491. static_assert(agg1.arr[0] == 0, "");
  492. static_assert(agg1.arr[4] == 0, "");
  493. static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
  494. static_assert(agg1.p == nullptr, "");
  495. namespace SimpleDerivedClass {
  496. struct B {
  497. constexpr B(int n) : a(n) {}
  498. int a;
  499. };
  500. struct D : B {
  501. constexpr D(int n) : B(n) {}
  502. };
  503. constexpr D d(3);
  504. static_assert(d.a == 3, "");
  505. }
  506. struct Bottom { constexpr Bottom() {} };
  507. struct Base : Bottom {
  508. constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
  509. int a;
  510. const char *b;
  511. };
  512. struct Base2 : Bottom {
  513. constexpr Base2(const int &r) : r(r) {}
  514. int q = 123;
  515. const int &r;
  516. };
  517. struct Derived : Base, Base2 {
  518. constexpr Derived() : Base(76), Base2(a) {}
  519. int c = r + b[1];
  520. };
  521. constexpr bool operator==(const Base &a, const Base &b) {
  522. return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
  523. }
  524. constexpr Base base;
  525. constexpr Base base2(76);
  526. constexpr Derived derived;
  527. static_assert(derived.a == 76, "");
  528. static_assert(derived.b[2] == 's', "");
  529. static_assert(derived.c == 76 + 'e', "");
  530. static_assert(derived.q == 123, "");
  531. static_assert(derived.r == 76, "");
  532. static_assert(&derived.r == &derived.a, "");
  533. static_assert(!(derived == base), "");
  534. static_assert(derived == base2, "");
  535. constexpr Bottom &bot1 = (Base&)derived;
  536. constexpr Bottom &bot2 = (Base2&)derived;
  537. static_assert(&bot1 != &bot2, "");
  538. constexpr Bottom *pb1 = (Base*)&derived;
  539. constexpr Bottom *pb2 = (Base2*)&derived;
  540. static_assert(pb1 != pb2, "");
  541. static_assert(pb1 == &bot1, "");
  542. static_assert(pb2 == &bot2, "");
  543. constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}}
  544. constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}}
  545. constexpr Base2 &ok2 = (Base2&)bot2;
  546. static_assert(&ok2 == &derived, "");
  547. constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}}
  548. constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}}
  549. constexpr Base2 *pok2 = (Base2*)pb2;
  550. static_assert(pok2 == &derived, "");
  551. static_assert(&ok2 == pok2, "");
  552. static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
  553. static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
  554. constexpr Base *nullB = 42 - 6 * 7;
  555. static_assert((Bottom*)nullB == 0, "");
  556. static_assert((Derived*)nullB == 0, "");
  557. static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
  558. }
  559. namespace Temporaries {
  560. struct S {
  561. constexpr S() {}
  562. constexpr int f();
  563. };
  564. struct T : S {
  565. constexpr T(int n) : S(), n(n) {}
  566. int n;
  567. };
  568. constexpr int S::f() {
  569. // 'this' must be the postfix-expression in a class member access expression,
  570. // so we can't just use
  571. // return static_cast<T*>(this)->n;
  572. return this->*(int(S::*))&T::n;
  573. }
  574. // The T temporary is implicitly cast to an S subobject, but we can recover the
  575. // T full-object via a base-to-derived cast, or a derived-to-base-casted member
  576. // pointer.
  577. static_assert(T(3).f() == 3, "");
  578. constexpr int f(const S &s) {
  579. return static_cast<const T&>(s).n;
  580. }
  581. constexpr int n = f(T(5));
  582. static_assert(f(T(5)) == 5, "");
  583. }
  584. namespace Union {
  585. union U {
  586. int a;
  587. int b;
  588. };
  589. constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}}
  590. static_assert(u[0].a == 0, "");
  591. static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
  592. static_assert(u[1].b == 1, "");
  593. static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
  594. static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
  595. static_assert((&(u[1]) + 1 + 1)->b == 3, "");
  596. }
  597. namespace MemberPointer {
  598. struct A {
  599. constexpr A(int n) : n(n) {}
  600. int n;
  601. constexpr int f() { return n + 3; }
  602. };
  603. constexpr A a(7);
  604. static_assert(A(5).*&A::n == 5, "");
  605. static_assert((&a)->*&A::n == 7, "");
  606. static_assert((A(8).*&A::f)() == 11, "");
  607. static_assert(((&a)->*&A::f)() == 10, "");
  608. struct B : A {
  609. constexpr B(int n, int m) : A(n), m(m) {}
  610. int m;
  611. constexpr int g() { return n + m + 1; }
  612. };
  613. constexpr B b(9, 13);
  614. static_assert(B(4, 11).*&A::n == 4, "");
  615. static_assert(B(4, 11).*&B::m == 11, "");
  616. static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
  617. static_assert((&b)->*&A::n == 9, "");
  618. static_assert((&b)->*&B::m == 13, "");
  619. static_assert((&b)->*(int(A::*))&B::m == 13, "");
  620. static_assert((B(4, 11).*&A::f)() == 7, "");
  621. static_assert((B(4, 11).*&B::g)() == 16, "");
  622. static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
  623. static_assert(((&b)->*&A::f)() == 12, "");
  624. static_assert(((&b)->*&B::g)() == 23, "");
  625. static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
  626. struct S {
  627. constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
  628. m(m), n(n), pf(pf), pn(pn) {}
  629. constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
  630. constexpr int f() { return this->*pn; }
  631. virtual int g() const;
  632. int m, n;
  633. int (S::*pf)() const;
  634. int S::*pn;
  635. };
  636. constexpr int S::*pm = &S::m;
  637. constexpr int S::*pn = &S::n;
  638. constexpr int (S::*pf)() const = &S::f;
  639. constexpr int (S::*pg)() const = &S::g;
  640. constexpr S s(2, 5, &S::f, &S::m);
  641. static_assert((s.*&S::f)() == 2, "");
  642. static_assert((s.*s.pf)() == 2, "");
  643. template<int n> struct T : T<n-1> {};
  644. template<> struct T<0> { int n; };
  645. template<> struct T<30> : T<29> { int m; };
  646. T<17> t17;
  647. T<30> t30;
  648. constexpr int (T<10>::*deepn) = &T<0>::n;
  649. static_assert(&(t17.*deepn) == &t17.n, "");
  650. constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
  651. constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
  652. static_assert(&(t30.*deepm) == &t30.m, "");
  653. constexpr T<5> *p17_5 = &t17;
  654. constexpr T<13> *p17_13 = (T<13>*)p17_5;
  655. constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}}
  656. static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
  657. static_assert(&(p17_13->*deepn) == &t17.n, "");
  658. constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
  659. constexpr T<5> *p30_5 = &t30;
  660. constexpr T<23> *p30_23 = (T<23>*)p30_5;
  661. constexpr T<13> *p30_13 = p30_23;
  662. static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
  663. static_assert(&(p30_13->*deepn) == &t30.n, "");
  664. static_assert(&(p30_23->*deepn) == &t30.n, "");
  665. static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
  666. static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
  667. static_assert(&(p30_23->*deepm) == &t30.m, "");
  668. }
  669. namespace ArrayBaseDerived {
  670. struct Base {
  671. constexpr Base() {}
  672. int n = 0;
  673. };
  674. struct Derived : Base {
  675. constexpr Derived() {}
  676. constexpr const int *f() { return &n; }
  677. };
  678. constexpr Derived a[10];
  679. constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
  680. constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
  681. static_assert(pb3 == pd3, "");
  682. // pb3 does not point to an array element.
  683. constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
  684. constexpr int pb4n = pb4->n; // expected-error {{constant expression}}
  685. constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this.
  686. constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}}
  687. constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this.
  688. constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}}
  689. constexpr Base *pb3a = pb4 - 1;
  690. // pb4 does not point to a Derived.
  691. constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}}
  692. constexpr Derived *pd3a = (Derived*)pb3a;
  693. constexpr int pd3n = pd3a->n;
  694. // pd3a still points to the Derived array.
  695. constexpr Derived *pd6 = pd3a + 3;
  696. static_assert(pd6 == &a[6], "");
  697. constexpr Derived *pd9 = pd6 + 3;
  698. constexpr Derived *pd10 = pd6 + 4;
  699. constexpr int pd9n = pd9->n; // ok
  700. constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
  701. constexpr int pd0n = pd10[-10].n;
  702. // FIXME: check pointer arithmetic here.
  703. constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
  704. constexpr Base *pb9 = pd9;
  705. constexpr const int *(Base::*pfb)() const =
  706. static_cast<const int *(Base::*)() const>(&Derived::f);
  707. static_assert((pb9->*pfb)() == &a[9].n, "");
  708. }
  709. namespace Complex {
  710. class complex {
  711. int re, im;
  712. public:
  713. constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
  714. constexpr complex(const complex &o) : re(o.re), im(o.im) {}
  715. constexpr complex operator-() const { return complex(-re, -im); }
  716. friend constexpr complex operator+(const complex &l, const complex &r) {
  717. return complex(l.re + r.re, l.im + r.im);
  718. }
  719. friend constexpr complex operator-(const complex &l, const complex &r) {
  720. return l + -r;
  721. }
  722. friend constexpr complex operator*(const complex &l, const complex &r) {
  723. return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
  724. }
  725. friend constexpr bool operator==(const complex &l, const complex &r) {
  726. return l.re == r.re && l.im == r.im;
  727. }
  728. constexpr bool operator!=(const complex &r) const {
  729. return re != r.re || im != r.im;
  730. }
  731. constexpr int real() const { return re; }
  732. constexpr int imag() const { return im; }
  733. };
  734. constexpr complex i = complex(0, 1);
  735. constexpr complex k = (3 + 4*i) * (6 - 4*i);
  736. static_assert(complex(1,0).real() == 1, "");
  737. static_assert(complex(1,0).imag() == 0, "");
  738. static_assert(((complex)1).imag() == 0, "");
  739. static_assert(k.real() == 34, "");
  740. static_assert(k.imag() == 12, "");
  741. static_assert(k - 34 == 12*i, "");
  742. static_assert((complex)1 == complex(1), "");
  743. static_assert((complex)1 != complex(0, 1), "");
  744. static_assert(complex(1) == complex(1), "");
  745. static_assert(complex(1) != complex(0, 1), "");
  746. constexpr complex makeComplex(int re, int im) { return complex(re, im); }
  747. static_assert(makeComplex(1,0) == complex(1), "");
  748. static_assert(makeComplex(1,0) != complex(0, 1), "");
  749. class complex_wrap : public complex {
  750. public:
  751. constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
  752. constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
  753. };
  754. static_assert((complex_wrap)1 == complex(1), "");
  755. static_assert((complex)1 != complex_wrap(0, 1), "");
  756. static_assert(complex(1) == complex_wrap(1), "");
  757. static_assert(complex_wrap(1) != complex(0, 1), "");
  758. constexpr complex_wrap makeComplexWrap(int re, int im) {
  759. return complex_wrap(re, im);
  760. }
  761. static_assert(makeComplexWrap(1,0) == complex(1), "");
  762. static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
  763. }
  764. namespace PR11595 {
  765. struct A { constexpr bool operator==(int x) { return true; } };
  766. struct B { B(); A& x; };
  767. static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
  768. constexpr bool f(int k) {
  769. return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
  770. }
  771. constexpr int n = f(1); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'f(1)'}}
  772. }
  773. namespace ExprWithCleanups {
  774. struct A { A(); ~A(); int get(); };
  775. constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
  776. constexpr int n = get(false);
  777. }
  778. namespace Volatile {
  779. volatile constexpr int n1 = 0; // expected-note {{here}}
  780. volatile const int n2 = 0; // expected-note {{here}}
  781. int n3 = 37; // expected-note {{declared here}}
  782. constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
  783. constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
  784. struct T { int n; };
  785. const T t = { 42 }; // expected-note {{declared here}}
  786. constexpr int f(volatile int &&r) {
  787. return r; // expected-note {{read of volatile temporary is not allowed in a constant expression}}
  788. }
  789. struct S {
  790. int k : f(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'f(0)'}}
  791. int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
  792. int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
  793. };
  794. }
  795. namespace ExternConstexpr {
  796. extern constexpr int n = 0;
  797. extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
  798. void f() {
  799. extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
  800. constexpr int j = 0;
  801. constexpr int k; // expected-error {{default initialization of an object of const type}}
  802. }
  803. }