PageRenderTime 90ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/CXX/expr/expr.const/p2-0x.cpp

https://bitbucket.org/larsivi/amber-clang
C++ | 609 lines | 464 code | 58 blank | 87 comment | 17 complexity | 80d3eb4c5132085a5785fc047383fc06 MD5 | raw file
  1. // RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu
  2. // A conditional-expression is a core constant expression unless it involves one
  3. // of the following as a potentially evaluated subexpression [...]:
  4. // - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
  5. // expression, function invocation substitution (7.1.5 [dcl.constexpr])
  6. // replaces each occurrence of this in a constexpr member function with a
  7. // pointer to the class object. -end note];
  8. struct This {
  9. int this1 : this1; // expected-error {{undeclared}}
  10. int this2 : this->this1; // expected-error {{invalid}}
  11. void this3() {
  12. int n1[this->this1]; // expected-warning {{variable length array}}
  13. int n2[this1]; // expected-warning {{variable length array}}
  14. (void)n1, (void)n2;
  15. }
  16. };
  17. // - an invocation of a function other than a constexpr constructor for a
  18. // literal class or a constexpr function [ Note: Overload resolution (13.3)
  19. // is applied as usual - end note ];
  20. struct NonConstexpr1 {
  21. static int f() { return 1; } // expected-note {{here}}
  22. int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
  23. };
  24. struct NonConstexpr2 {
  25. constexpr NonConstexpr2(); // expected-note {{here}}
  26. int n;
  27. };
  28. struct NonConstexpr3 {
  29. NonConstexpr3();
  30. int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
  31. };
  32. struct NonConstexpr4 {
  33. NonConstexpr4(); // expected-note {{declared here}}
  34. int n;
  35. };
  36. struct NonConstexpr5 {
  37. int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}}
  38. };
  39. // - an invocation of an undefined constexpr function or an undefined
  40. // constexpr constructor;
  41. struct UndefinedConstexpr {
  42. constexpr UndefinedConstexpr();
  43. static constexpr int undefinedConstexpr1(); // expected-note {{here}}
  44. int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
  45. };
  46. // - an invocation of a constexpr function with arguments that, when substituted
  47. // by function invocation substitution (7.1.5), do not produce a core constant
  48. // expression;
  49. namespace NonConstExprReturn {
  50. static constexpr const int &id_ref(const int &n) {
  51. return n;
  52. }
  53. struct NonConstExprFunction {
  54. int n : id_ref(16); // ok
  55. };
  56. constexpr const int *address_of(const int &a) {
  57. return &a;
  58. }
  59. constexpr const int *return_param(int n) { // expected-note {{declared here}}
  60. return address_of(n);
  61. }
  62. struct S {
  63. int n : *return_param(0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
  64. };
  65. }
  66. // - an invocation of a constexpr constructor with arguments that, when
  67. // substituted by function invocation substitution (7.1.5), do not produce all
  68. // constant expressions for the constructor calls and full-expressions in the
  69. // mem-initializers (including conversions);
  70. namespace NonConstExprCtor {
  71. struct T {
  72. constexpr T(const int &r) :
  73. r(r) {
  74. }
  75. const int &r;
  76. };
  77. constexpr int n = 0;
  78. constexpr T t1(n); // ok
  79. constexpr T t2(0); // expected-error {{must be initialized by a constant expression}} expected-note {{temporary created here}} expected-note {{reference to temporary is not a constant expression}}
  80. struct S {
  81. int n : T(4).r; // ok
  82. };
  83. }
  84. // - an invocation of a constexpr function or a constexpr constructor that would
  85. // exceed the implementation-defined recursion limits (see Annex B);
  86. namespace RecursionLimits {
  87. constexpr int RecurseForever(int n) {
  88. return n + RecurseForever(n+1); // expected-note {{constexpr evaluation exceeded maximum depth of 128 calls}} expected-note 9{{in call to 'RecurseForever(}} expected-note {{skipping 118 calls}}
  89. }
  90. struct AlsoRecurseForever {
  91. constexpr AlsoRecurseForever(int n) :
  92. n(AlsoRecurseForever(n+1).n) // expected-note {{constexpr evaluation exceeded maximum depth of 128 calls}} expected-note 9{{in call to 'AlsoRecurseForever(}} expected-note {{skipping 118 calls}}
  93. {}
  94. int n;
  95. };
  96. struct S {
  97. int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
  98. int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
  99. };
  100. }
  101. // DR1458: taking the address of an object of incomplete class type
  102. namespace IncompleteClassTypeAddr {
  103. struct S;
  104. extern S s;
  105. constexpr S *p = &s; // ok
  106. static_assert(p, "");
  107. extern S sArr[];
  108. constexpr S (*p2)[] = &sArr; // ok
  109. struct S {
  110. constexpr S *operator&() { return nullptr; }
  111. };
  112. constexpr S *q = &s; // ok
  113. static_assert(!q, "");
  114. }
  115. // - an operation that would have undefined behavior [Note: including, for
  116. // example, signed integer overflow (Clause 5 [expr]), certain pointer
  117. // arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
  118. // shift operations (5.8 [expr.shift]) -end note];
  119. namespace UndefinedBehavior {
  120. void f(int n) {
  121. switch (n) {
  122. case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}} expected-note {{previous case defined here}}
  123. case (int)0x80000000u: // ok
  124. case (int)10000000000ll: // expected-note {{here}}
  125. case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
  126. case (int)(unsigned)(long long)4.4e9: // ok
  127. case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}} expected-error {{duplicate case value '2147483647'}} expected-note {{previous case defined here}}
  128. case (int)((float)1e37 / 1e30): // ok
  129. case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}} expected-error {{duplicate case value '2147483647'}}
  130. break;
  131. }
  132. }
  133. constexpr int int_min = ~0x7fffffff;
  134. constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
  135. constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
  136. constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
  137. constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
  138. constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
  139. constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
  140. constexpr int shl_0 = 0 << 0; // ok
  141. constexpr int shl_31 = 0 << 31; // ok
  142. constexpr int shl_32 = 0 << 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type 'int' (32}} expected-warning {{>= width of type}}
  143. constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
  144. constexpr int shl_unsigned_into_sign = 1u << 31; // ok
  145. constexpr int shl_unsigned_overflow = 1024u << 31; // ok
  146. constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
  147. constexpr int shl_signed_ok = 1 << 30; // ok
  148. constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457)
  149. constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457)
  150. constexpr int shl_signed_off_end = 2 << 31; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{signed shift result (0x100000000) requires 34 bits to represent, but 'int' only has 32 bits}}
  151. constexpr int shl_signed_off_end_2 = 0x7fffffff << 2; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{signed shift result (0x1FFFFFFFC) requires 34 bits to represent, but 'int' only has 32 bits}}
  152. constexpr int shl_signed_overflow = 1024 << 31; // expected-error {{constant expression}} expected-note {{signed left shift discards bits}} expected-warning {{requires 43 bits to represent}}
  153. constexpr int shl_signed_ok2 = 1024 << 20; // ok
  154. constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
  155. constexpr int shr_0 = 0 >> 0; // ok
  156. constexpr int shr_31 = 0 >> 31; // ok
  157. constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
  158. struct S {
  159. int m;
  160. };
  161. constexpr S s = { 5 };
  162. constexpr const int *p = &s.m + 1;
  163. constexpr const int &f(const int *q) {
  164. return q[0];
  165. }
  166. constexpr int n = (f(p), 0); // ok
  167. struct T {
  168. int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
  169. };
  170. namespace Ptr {
  171. struct A {};
  172. struct B : A { int n; };
  173. B a[3][3];
  174. constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
  175. B b = {};
  176. constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
  177. constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
  178. constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
  179. constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
  180. constexpr A *na = nullptr;
  181. constexpr B *nb = nullptr;
  182. constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
  183. constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
  184. static_assert((A*)nb == 0, "");
  185. static_assert((B*)na == 0, "");
  186. constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
  187. constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
  188. struct C {
  189. constexpr int f() { return 0; }
  190. } constexpr c = C();
  191. constexpr int k1 = c.f(); // ok
  192. constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}}
  193. constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}}
  194. C c2;
  195. constexpr int k4 = c2.f(); // ok!
  196. constexpr int diff1 = &a[2] - &a[0];
  197. constexpr int diff2 = &a[1][3] - &a[1][0];
  198. constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
  199. static_assert(&a[2][0] == &a[1][3], "");
  200. constexpr int diff4 = (&b + 1) - &b;
  201. constexpr int diff5 = &a[1][2].n - &a[1][0].n; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
  202. constexpr int diff6 = &a[1][2].n - &a[1][2].n;
  203. constexpr int diff7 = (A*)&a[0][1] - (A*)&a[0][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
  204. }
  205. namespace Overflow {
  206. // Signed int overflow.
  207. constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
  208. constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
  209. constexpr int n3 = n1 + 1; // ok
  210. constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
  211. constexpr int n5 = -65536 * 32768; // ok
  212. constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
  213. constexpr int n7 = -n3 + -1; // ok
  214. constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
  215. constexpr int n9 = n3 - 0; // ok
  216. constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
  217. constexpr int n11 = -1 - n3; // ok
  218. constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
  219. constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
  220. constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
  221. constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
  222. constexpr signed char c1 = 100 * 2; // ok
  223. constexpr signed char c2 = '\x64' * '\2'; // also ok
  224. constexpr long long ll1 = 0x7fffffffffffffff; // ok
  225. constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
  226. constexpr long long ll3 = -ll1 - 1; // ok
  227. constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
  228. constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
  229. // Yikes.
  230. char melchizedek[2200000000];
  231. typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
  232. constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
  233. constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
  234. constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
  235. constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
  236. // Unsigned int overflow.
  237. static_assert(65536u * 65536u == 0u, ""); // ok
  238. static_assert(4294967295u + 1u == 0u, ""); // ok
  239. static_assert(0u - 1u == 4294967295u, ""); // ok
  240. static_assert(~0u * ~0u == 1u, ""); // ok
  241. // Floating-point overflow and NaN.
  242. constexpr float f1 = 1e38f * 3.4028f; // ok
  243. constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
  244. constexpr float f3 = 1e38f / -.2939f; // ok
  245. constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
  246. constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
  247. constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
  248. constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}}
  249. }
  250. }
  251. // - a lambda-expression (5.1.2);
  252. struct Lambda {
  253. // FIXME: clang crashes when trying to parse this! Revisit this check once
  254. // lambdas are fully implemented.
  255. //int n : []{ return 1; }();
  256. };
  257. // - an lvalue-to-rvalue conversion (4.1) unless it is applied to
  258. namespace LValueToRValue {
  259. // - a non-volatile glvalue of integral or enumeration type that refers to a
  260. // non-volatile const object with a preceding initialization, initialized
  261. // with a constant expression [Note: a string literal (2.14.5 [lex.string])
  262. // corresponds to an array of such objects. -end note], or
  263. volatile const int vi = 1; // expected-note 2{{here}}
  264. const int ci = 1;
  265. volatile const int &vrci = ci;
  266. static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
  267. static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
  268. static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
  269. // - a non-volatile glvalue of literal type that refers to a non-volatile
  270. // object defined with constexpr, or that refers to a sub-object of such an
  271. // object, or
  272. struct V {
  273. constexpr V() : v(1) {}
  274. volatile int v; // expected-note {{not literal because}}
  275. };
  276. constexpr V v; // expected-error {{non-literal type}}
  277. struct S {
  278. constexpr S(int=0) : i(1), v(const_cast<volatile int&>(vi)) {}
  279. constexpr S(const S &s) : i(2), v(const_cast<volatile int&>(vi)) {}
  280. int i;
  281. volatile int &v;
  282. };
  283. constexpr S s; // ok
  284. constexpr volatile S vs; // expected-note {{here}}
  285. constexpr const volatile S &vrs = s; // ok
  286. static_assert(s.i, "");
  287. static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
  288. static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
  289. static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
  290. static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
  291. static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
  292. // - a non-volatile glvalue of literal type that refers to a non-volatile
  293. // temporary object whose lifetime has not ended, initialized with a
  294. // constant expression;
  295. constexpr volatile S f() { return S(); }
  296. static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
  297. static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}}
  298. }
  299. // DR1312: The proposed wording for this defect has issues, so we ignore this
  300. // bullet and instead prohibit casts from pointers to cv void (see core-20842
  301. // and core-20845).
  302. //
  303. // - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
  304. // glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
  305. // are neither the same type nor similar types (4.4 [conv.qual]);
  306. // - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
  307. // refers to a non-active member of a union or a subobject thereof;
  308. namespace LValueToRValueUnion {
  309. // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
  310. // of this.
  311. union U { int a, b; } constexpr u = U();
  312. static_assert(u.a == 0, "");
  313. constexpr const int *bp = &u.b;
  314. constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
  315. extern const U pu;
  316. constexpr const int *pua = &pu.a;
  317. constexpr const int *pub = &pu.b;
  318. constexpr U pu = { .b = 1 }; // expected-warning {{C99 feature}}
  319. constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
  320. constexpr const int b2 = *pub; // ok
  321. }
  322. // - an id-expression that refers to a variable or data member of reference type
  323. // unless the reference has a preceding initialization, initialized with a
  324. // constant expression;
  325. namespace References {
  326. const int a = 2;
  327. int &b = *const_cast<int*>(&a);
  328. int c = 10; // expected-note 2 {{here}}
  329. int &d = c;
  330. constexpr int e = 42;
  331. int &f = const_cast<int&>(e);
  332. extern int &g;
  333. constexpr int &h(); // expected-note {{here}}
  334. int &i = h(); // expected-note {{here}}
  335. constexpr int &j() { return b; }
  336. int &k = j();
  337. struct S {
  338. int A : a;
  339. int B : b;
  340. int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
  341. int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
  342. int D2 : &d - &c + 1;
  343. int E : e / 2;
  344. int F : f - 11;
  345. int G : g; // expected-error {{constant expression}}
  346. int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
  347. int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
  348. int J : j();
  349. int K : k;
  350. };
  351. }
  352. // - a dynamic_cast (5.2.7);
  353. namespace DynamicCast {
  354. struct S { int n; };
  355. constexpr S s { 16 };
  356. struct T {
  357. int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
  358. };
  359. }
  360. // - a reinterpret_cast (5.2.10);
  361. namespace ReinterpretCast {
  362. struct S { int n; };
  363. constexpr S s { 16 };
  364. struct T {
  365. int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
  366. };
  367. struct U {
  368. int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
  369. };
  370. }
  371. // - a pseudo-destructor call (5.2.4);
  372. namespace PseudoDtor {
  373. int k;
  374. typedef int I;
  375. struct T {
  376. int n : (k.~I(), 0); // expected-error {{constant expression}}
  377. };
  378. }
  379. // - increment or decrement operations (5.2.6, 5.3.2);
  380. namespace IncDec {
  381. int k = 2;
  382. struct T {
  383. int n : ++k; // expected-error {{constant expression}}
  384. int m : --k; // expected-error {{constant expression}}
  385. };
  386. }
  387. // - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
  388. namespace std {
  389. struct type_info {
  390. virtual ~type_info();
  391. const char *name;
  392. };
  393. }
  394. namespace TypeId {
  395. struct S { virtual void f(); };
  396. constexpr S *p = 0;
  397. constexpr const std::type_info &ti1 = typeid(*p); // expected-error {{must be initialized by a constant expression}} expected-note {{typeid applied to expression of polymorphic type 'TypeId::S'}}
  398. struct T {} t;
  399. constexpr const std::type_info &ti2 = typeid(t);
  400. }
  401. // - a new-expression (5.3.4);
  402. // - a delete-expression (5.3.5);
  403. namespace NewDelete {
  404. int *p = 0;
  405. struct T {
  406. int n : *new int(4); // expected-error {{constant expression}}
  407. int m : (delete p, 2); // expected-error {{constant expression}}
  408. };
  409. }
  410. // - a relational (5.9) or equality (5.10) operator where the result is
  411. // unspecified;
  412. namespace UnspecifiedRelations {
  413. int a, b;
  414. constexpr int *p = &a, *q = &b;
  415. // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
  416. // different objects that are not members of the same array or to different
  417. // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
  418. // and p>=q are unspecified.
  419. constexpr bool u1 = p < q; // expected-error {{constant expression}}
  420. constexpr bool u2 = p > q; // expected-error {{constant expression}}
  421. constexpr bool u3 = p <= q; // expected-error {{constant expression}}
  422. constexpr bool u4 = p >= q; // expected-error {{constant expression}}
  423. constexpr bool u5 = p < 0; // expected-error {{constant expression}}
  424. constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
  425. constexpr bool u7 = p > 0; // expected-error {{constant expression}}
  426. constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
  427. constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
  428. constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
  429. constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
  430. constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
  431. void f(), g();
  432. constexpr void (*pf)() = &f, (*pg)() = &g;
  433. constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
  434. constexpr bool u14 = pf == pg;
  435. // If two pointers point to non-static data members of the same object with
  436. // different access control, the result is unspecified.
  437. struct A {
  438. public:
  439. constexpr A() : a(0), b(0) {}
  440. int a;
  441. constexpr bool cmp() { return &a < &b; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{comparison of address of fields 'a' and 'b' of 'A' with differing access specifiers (public vs private) has unspecified value}}
  442. private:
  443. int b;
  444. };
  445. class B {
  446. public:
  447. A a;
  448. constexpr bool cmp() { return &a.a < &b.a; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{comparison of address of fields 'a' and 'b' of 'B' with differing access specifiers (public vs protected) has unspecified value}}
  449. protected:
  450. A b;
  451. };
  452. // If two pointers point to different base sub-objects of the same object, or
  453. // one points to a base subobject and the other points to a member, the result
  454. // of the comparison is unspecified. This is not explicitly called out by
  455. // [expr.rel]p2, but is covered by 'Other pointer comparisons are
  456. // unspecified'.
  457. struct C {
  458. int c[2];
  459. };
  460. struct D {
  461. int d;
  462. };
  463. struct E : C, D {
  464. struct Inner {
  465. int f;
  466. } e;
  467. } e;
  468. constexpr bool base1 = &e.c[0] < &e.d; // expected-error {{constant expression}} expected-note {{comparison of addresses of subobjects of different base classes has unspecified value}}
  469. constexpr bool base2 = &e.c[1] < &e.e.f; // expected-error {{constant expression}} expected-note {{comparison of address of base class subobject 'C' of class 'E' to field 'e' has unspecified value}}
  470. constexpr bool base3 = &e.e.f < &e.d; // expected-error {{constant expression}} expected-note {{comparison of address of base class subobject 'D' of class 'E' to field 'e' has unspecified value}}
  471. // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
  472. // represent the same address or are both the null pointer [...]; otherwise
  473. // the result is unspecified.
  474. struct S { int a, b; } s;
  475. constexpr void *null = 0;
  476. constexpr void *pv = (void*)&s.a;
  477. constexpr void *qv = (void*)&s.b;
  478. constexpr bool v1 = null < 0;
  479. constexpr bool v2 = null < pv; // expected-error {{constant expression}}
  480. constexpr bool v3 = null == pv; // ok
  481. constexpr bool v4 = qv == pv; // ok
  482. constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
  483. constexpr bool v6 = qv > null; // expected-error {{constant expression}}
  484. constexpr bool v7 = qv <= (void*)&s.b; // ok
  485. constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
  486. }
  487. // - an assignment or a compound assignment (5.17); or
  488. namespace Assignment {
  489. int k;
  490. struct T {
  491. int n : (k = 9); // expected-error {{constant expression}}
  492. int m : (k *= 2); // expected-error {{constant expression}}
  493. };
  494. struct Literal {
  495. constexpr Literal(const char *name) : name(name) {}
  496. const char *name;
  497. };
  498. struct Expr {
  499. constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
  500. bool IsLiteral;
  501. union {
  502. Literal l;
  503. // ...
  504. };
  505. };
  506. struct MulEq {
  507. constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
  508. Expr LHS;
  509. Expr RHS;
  510. };
  511. constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
  512. Literal a("a");
  513. Literal b("b");
  514. MulEq c = a *= b; // ok
  515. }
  516. // - a throw-expression (15.1)
  517. namespace Throw {
  518. struct S {
  519. int n : (throw "hello", 10); // expected-error {{constant expression}}
  520. };
  521. }
  522. // PR9999
  523. template<unsigned int v>
  524. class bitWidthHolding {
  525. public:
  526. static const
  527. unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
  528. };
  529. static const int width=bitWidthHolding<255>::width;
  530. template<bool b>
  531. struct always_false {
  532. static const bool value = false;
  533. };
  534. template<bool b>
  535. struct and_or {
  536. static const bool and_value = b && and_or<always_false<b>::value>::and_value;
  537. static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
  538. };
  539. static const bool and_value = and_or<true>::and_value;
  540. static const bool or_value = and_or<true>::or_value;
  541. static_assert(and_value == false, "");
  542. static_assert(or_value == true, "");
  543. namespace rdar13090123 {
  544. typedef __INTPTR_TYPE__ intptr_t;
  545. constexpr intptr_t f(intptr_t x) {
  546. return (((x) >> 21) * 8); // expected-note{{subexpression not valid in a constant expression}}
  547. }
  548. extern "C" int foo;
  549. constexpr intptr_t i = f((intptr_t)&foo - 10); // expected-error{{constexpr variable 'i' must be initialized by a constant expression}} \
  550. // expected-note{{in call to 'f((char*)&foo + -10)'}}
  551. }