constant-expression-cxx1y.cpp revision 692eafd2052fb6ca581530d6f3569eea9520a508
1// RUN: %clang_cc1 -std=c++1y -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu 2 3struct S { 4 // dummy ctor to make this a literal type 5 constexpr S(int); 6 7 S(); 8 9 int arr[10]; 10 11 constexpr int &get(int n) { return arr[n]; } 12 constexpr const int &get(int n) const { return arr[n]; } 13}; 14 15S s = S(); 16const S &sr = s; 17static_assert(&s.get(4) - &sr.get(2) == 2, ""); 18 19// Compound-statements can be used in constexpr functions. 20constexpr int e() {{{{}} return 5; }} 21static_assert(e() == 5, ""); 22 23// Types can be defined in constexpr functions. 24constexpr int f() { 25 enum E { e1, e2, e3 }; 26 27 struct S { 28 constexpr S(E e) : e(e) {} 29 constexpr int get() { return e; } 30 E e; 31 }; 32 33 return S(e2).get(); 34} 35static_assert(f() == 1, ""); 36 37// Variables can be declared in constexpr functions. 38constexpr int g(int k) { 39 const int n = 9; 40 int k2 = k * k; 41 int k3 = k2 * k; 42 return 3 * k3 + 5 * k2 + n * k - 20; 43} 44static_assert(g(2) == 42, ""); 45constexpr int h(int n) { 46 static const int m = n; // expected-error {{static variable not permitted in a constexpr function}} 47 return m; 48} 49constexpr int i(int n) { 50 thread_local const int m = n; // expected-error {{thread_local variable not permitted in a constexpr function}} 51 return m; 52} 53 54// if-statements can be used in constexpr functions. 55constexpr int j(int k) { 56 if (k == 5) 57 return 1; 58 if (k == 1) 59 return 5; 60 else { 61 if (int n = 2 * k - 4) { 62 return n + 1; 63 return 2; 64 } 65 } 66} // expected-note 2{{control reached end of constexpr function}} 67static_assert(j(0) == -3, ""); 68static_assert(j(1) == 5, ""); 69static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}} 70static_assert(j(3) == 3, ""); 71static_assert(j(4) == 5, ""); 72static_assert(j(5) == 1, ""); 73 74// There can be 0 return-statements. 75constexpr void k() { 76} 77 78// If the return type is not 'void', no return statements => never a constant 79// expression, so still diagnose that case. 80[[noreturn]] constexpr int fn() { // expected-error {{no return statement in constexpr function}} 81 fn(); 82} 83 84// We evaluate the body of a constexpr constructor, to check for side-effects. 85struct U { 86 constexpr U(int n) { 87 if (j(n)) {} // expected-note {{in call to 'j(2)'}} 88 } 89}; 90constexpr U u1{1}; 91constexpr U u2{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}} 92 93// We allow expression-statements. 94constexpr int l(bool b) { 95 if (b) 96 throw "invalid value for b!"; // expected-note {{subexpression not valid}} 97 return 5; 98} 99static_assert(l(false) == 5, ""); 100static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}} 101 102// Potential constant expression checking is still applied where possible. 103constexpr int htonl(int x) { // expected-error {{never produces a constant expression}} 104 typedef unsigned char uchar; 105 uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; 106 return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} 107} 108 109constexpr int maybe_htonl(bool isBigEndian, int x) { 110 if (isBigEndian) 111 return x; 112 113 typedef unsigned char uchar; 114 uchar arr[4] = { uchar(x >> 24), uchar(x >> 16), uchar(x >> 8), uchar(x) }; 115 return *reinterpret_cast<int*>(arr); // expected-note {{reinterpret_cast is not allowed in a constant expression}} 116} 117 118constexpr int swapped = maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}} 119 120namespace NS { 121 constexpr int n = 0; 122} 123constexpr int namespace_alias() { 124 namespace N = NS; 125 return N::n; 126} 127 128namespace assign { 129 constexpr int a = 0; 130 const int b = 0; 131 int c = 0; // expected-note 2{{here}} 132 133 constexpr void set(const int &a, int b) { 134 const_cast<int&>(a) = b; // expected-note 2{{constant expression cannot modify an object that is visible outside that expression}} 135 } 136 constexpr int wrap(int a, int b) { 137 set(a, b); 138 return a; 139 } 140 141 static_assert((set(a, 1), a) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(a, 1)'}} 142 static_assert((set(b, 1), b) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(b, 1)'}} 143 static_assert((set(c, 1), c) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 144 145 static_assert(wrap(a, 1) == 1, ""); 146 static_assert(wrap(b, 1) == 1, ""); 147 static_assert(wrap(c, 1) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 148} 149 150namespace string_assign { 151 template<typename T> 152 constexpr void swap(T &a, T &b) { 153 T tmp = a; 154 a = b; 155 b = tmp; 156 } 157 template<typename Iterator> 158 constexpr void reverse(Iterator begin, Iterator end) { 159 while (begin != end && begin != --end) 160 swap(*begin++, *end); 161 } 162 template<typename Iterator1, typename Iterator2> 163 constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) { 164 while (a != ae && b != be) 165 if (*a++ != *b++) 166 return false; 167 return a == ae && b == be; 168 } 169 constexpr bool test1(int n) { 170 char stuff[100] = "foobarfoo"; 171 const char stuff2[100] = "oofraboof"; 172 reverse(stuff, stuff + n); // expected-note {{cannot refer to element 101 of array of 100 elements}} 173 return equal(stuff, stuff + n, stuff2, stuff2 + n); 174 } 175 static_assert(!test1(1), ""); 176 static_assert(test1(3), ""); 177 static_assert(!test1(6), ""); 178 static_assert(test1(9), ""); 179 static_assert(!test1(100), ""); 180 static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}} 181 182 // FIXME: We should be able to reject this before it's called 183 constexpr void f() { 184 char foo[10] = { "z" }; // expected-note {{here}} 185 foo[10] = 'x'; // expected-warning {{past the end}} expected-note {{assignment to dereferenced one-past-the-end pointer}} 186 } 187 constexpr int k = (f(), 0); // expected-error {{constant expression}} expected-note {{in call}} 188} 189 190namespace array_resize { 191 constexpr int do_stuff(int k1, int k2) { 192 int arr[1234] = { 1, 2, 3, 4 }; 193 arr[k1] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}} 194 return arr[k2]; 195 } 196 static_assert(do_stuff(1, 2) == 3, ""); 197 static_assert(do_stuff(0, 0) == 5, ""); 198 static_assert(do_stuff(1233, 1233) == 5, ""); 199 static_assert(do_stuff(1233, 0) == 1, ""); 200 static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} 201 static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} 202 static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} 203} 204 205namespace potential_const_expr { 206 constexpr void set(int &n) { n = 1; } 207 constexpr int div_zero_1() { int z = 0; set(z); return 100 / z; } // no error 208 constexpr int div_zero_2() { // expected-error {{never produces a constant expression}} 209 int z = 0; 210 return 100 / (set(z), 0); // expected-note {{division by zero}} 211 } 212 int n; // expected-note {{declared here}} 213 constexpr int ref() { // expected-error {{never produces a constant expression}} 214 int &r = n; 215 return r; // expected-note {{read of non-const variable 'n'}} 216 } 217} 218 219namespace subobject { 220 union A { constexpr A() : y(5) {} int x, y; }; 221 struct B { A a; }; 222 struct C : B {}; 223 union D { constexpr D() : c() {} constexpr D(int n) : n(n) {} C c; int n; }; 224 constexpr void f(D &d) { 225 d.c.a.y = 3; 226 // expected-note@-1 {{cannot modify an object that is visible outside}} 227 // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}} 228 } 229 constexpr bool check(D &d) { return d.c.a.y == 3; } 230 231 constexpr bool g() { D d; f(d); return d.c.a.y == 3; } 232 static_assert(g(), ""); 233 234 D d; 235 constexpr bool h() { f(d); return check(d); } // expected-note {{in call}} 236 static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}} 237 238 constexpr bool i() { D d(0); f(d); return check(d); } // expected-note {{in call}} 239 static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}} 240 241 constexpr bool j() { D d; d.c.a.x = 3; return check(d); } // expected-note {{assignment to member 'x' of union with active member 'y'}} 242 static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}} 243} 244 245namespace lifetime { 246 constexpr int &&id(int &&n) { return static_cast<int&&>(n); } 247 constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}} 248 constexpr int bad() { int &&n = dead(); n = 1; return n; } // expected-note {{assignment to temporary whose lifetime has ended}} 249 static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}} 250} 251 252namespace const_modify { 253 constexpr int modify(int &n) { return n = 1; } // expected-note {{modification of object of const-qualified type 'const int'}} 254 constexpr int test1() { int k = 0; return modify(k); } 255 constexpr int test2() { const int k = 0; return modify(const_cast<int&>(k)); } // expected-note {{in call}} 256 static_assert(test1() == 1, ""); 257 static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}} 258} 259 260namespace null { 261 constexpr int test(int *p) { 262 return *p = 123; // expected-note {{assignment to dereferenced null pointer}} 263 } 264 static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}} 265} 266 267namespace incdec { 268 template<typename T> constexpr T &ref(T &&r) { return r; } 269 template<typename T> constexpr T postinc(T &&r) { return (r++, r); } 270 template<typename T> constexpr T postdec(T &&r) { return (r--, r); } 271 272 static_assert(++ref(0) == 1, ""); 273 static_assert(ref(0)++ == 0, ""); 274 static_assert(postinc(0) == 1, ""); 275 static_assert(--ref(0) == -1, ""); 276 static_assert(ref(0)-- == 0, ""); 277 static_assert(postdec(0) == -1, ""); 278 279 constexpr int overflow_int_inc_1 = ref(0x7fffffff)++; // expected-error {{constant}} expected-note {{2147483648}} 280 constexpr int overflow_int_inc_1_ok = ref(0x7ffffffe)++; 281 constexpr int overflow_int_inc_2 = ++ref(0x7fffffff); // expected-error {{constant}} expected-note {{2147483648}} 282 constexpr int overflow_int_inc_2_ok = ++ref(0x7ffffffe); 283 284 // inc/dec on short can't overflow because we promote to int first 285 static_assert(++ref<short>(0x7fff) == (int)0xffff8000u, ""); 286 static_assert(--ref<short>(0x8000) == 0x7fff, ""); 287 288 // inc on bool sets to true 289 static_assert(++ref(false), ""); // expected-warning {{deprecated}} 290 static_assert(++ref(true), ""); // expected-warning {{deprecated}} 291 292 int arr[10]; 293 static_assert(++ref(&arr[0]) == &arr[1], ""); 294 static_assert(++ref(&arr[9]) == &arr[10], ""); 295 static_assert(++ref(&arr[10]) == &arr[11], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}} 296 static_assert(ref(&arr[0])++ == &arr[0], ""); 297 static_assert(ref(&arr[10])++ == &arr[10], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}} 298 static_assert(postinc(&arr[0]) == &arr[1], ""); 299 static_assert(--ref(&arr[10]) == &arr[9], ""); 300 static_assert(--ref(&arr[1]) == &arr[0], ""); 301 static_assert(--ref(&arr[0]) != &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}} 302 static_assert(ref(&arr[1])-- == &arr[1], ""); 303 static_assert(ref(&arr[0])-- == &arr[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}} 304 static_assert(postdec(&arr[1]) == &arr[0], ""); 305 306 int x; 307 static_assert(++ref(&x) == &x + 1, ""); 308 309 static_assert(++ref(0.0) == 1.0, ""); 310 static_assert(ref(0.0)++ == 0.0, ""); 311 static_assert(postinc(0.0) == 1.0, ""); 312 static_assert(--ref(0.0) == -1.0, ""); 313 static_assert(ref(0.0)-- == 0.0, ""); 314 static_assert(postdec(0.0) == -1.0, ""); 315 316 static_assert(++ref(1e100) == 1e100, ""); 317 static_assert(--ref(1e100) == 1e100, ""); 318 319 union U { 320 int a, b; 321 }; 322 constexpr int f(U u) { 323 return ++u.b; // expected-note {{increment of member 'b' of union with active member 'a'}} 324 } 325 constexpr int wrong_member = f({0}); // expected-error {{constant}} expected-note {{in call to 'f({.a = 0})'}} 326 constexpr int vol = --ref<volatile int>(0); // expected-error {{constant}} expected-note {{decrement of volatile-qualified}} 327 328 constexpr int incr(int k) { 329 int x = k; 330 if (x++ == 100) 331 return x; 332 return incr(x); 333 } 334 static_assert(incr(0) == 101, ""); 335} 336 337namespace loops { 338 constexpr int fib_loop(int a) { 339 int f_k = 0, f_k_plus_one = 1; 340 for (int k = 1; k != a; ++k) { 341 int f_k_plus_two = f_k + f_k_plus_one; 342 f_k = f_k_plus_one; 343 f_k_plus_one = f_k_plus_two; 344 } 345 return f_k_plus_one; 346 } 347 static_assert(fib_loop(46) == 1836311903, ""); 348 349 constexpr bool breaks_work() { 350 int a = 0; 351 for (int n = 0; n != 100; ++n) { 352 ++a; 353 if (a == 5) continue; 354 if ((a % 5) == 0) break; 355 } 356 357 int b = 0; 358 while (b != 17) { 359 ++b; 360 if (b == 6) continue; 361 if ((b % 6) == 0) break; 362 } 363 364 int c = 0; 365 do { 366 ++c; 367 if (c == 7) continue; 368 if ((c % 7) == 0) break; 369 } while (c != 21); 370 371 return a == 10 && b == 12 & c == 14; 372 } 373 static_assert(breaks_work(), ""); 374 375 void not_constexpr(); 376 constexpr bool no_cont_after_break() { 377 for (;;) { 378 break; 379 not_constexpr(); 380 } 381 while (true) { 382 break; 383 not_constexpr(); 384 } 385 do { 386 break; 387 not_constexpr(); 388 } while (true); 389 return true; 390 } 391 static_assert(no_cont_after_break(), ""); 392 393 constexpr bool cond() { 394 for (int a = 1; bool b = a != 3; ++a) { 395 if (!b) 396 return false; 397 } 398 while (bool b = true) { 399 b = false; 400 break; 401 } 402 return true; 403 } 404 static_assert(cond(), ""); 405 406 constexpr int range_for() { 407 int arr[] = { 1, 2, 3, 4, 5 }; 408 int sum = 0; 409 for (int x : arr) 410 sum = sum + x; 411 return sum; 412 } 413 static_assert(range_for() == 15, ""); 414 415 template<int...N> struct ints {}; 416 template<typename A, typename B> struct join_ints; 417 template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> { 418 using type = ints<As..., sizeof...(As) + Bs...>; 419 }; 420 template<unsigned N> struct make_ints { 421 using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type; 422 }; 423 template<> struct make_ints<0> { using type = ints<>; }; 424 template<> struct make_ints<1> { using type = ints<0>; }; 425 426 struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} }; 427 428 template<typename T, unsigned N> struct array { 429 constexpr array() : arr{} {} 430 template<typename ...X> 431 constexpr array(X ...x) : arr{} { 432 init(typename make_ints<sizeof...(X)>::type{}, x...); 433 } 434 template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) { 435 ignore{arr[I] = x ...}; 436 } 437 T arr[N]; 438 struct iterator { 439 T *p; 440 constexpr explicit iterator(T *p) : p(p) {} 441 constexpr bool operator!=(iterator o) { return p != o.p; } 442 constexpr iterator &operator++() { ++p; return *this; } 443 constexpr T &operator*() { return *p; } 444 }; 445 constexpr iterator begin() { return iterator(arr); } 446 constexpr iterator end() { return iterator(arr + N); } 447 }; 448 449 constexpr int range_for_2() { 450 array<int, 5> arr { 1, 2, 3, 4, 5 }; 451 int sum = 0; 452 for (int k : arr) { 453 sum = sum + k; 454 if (sum > 8) break; 455 } 456 return sum; 457 } 458 static_assert(range_for_2() == 10, ""); 459} 460