blob: 824a0e8b91b7111c7e5ade9a5ce219fe1cf596b6 [file] [log] [blame]
Richard Smithd9f663b2013-04-22 15:31:51 +00001// 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}
Richard Smith3da88fa2013-04-26 14:36:30 +0000127
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) {
Richard Smith3da88fa2013-04-26 14:36:30 +0000159 while (begin != end && begin != --end)
160 swap(*begin++, *end);
Richard Smith3da88fa2013-04-26 14:36:30 +0000161 }
162 template<typename Iterator1, typename Iterator2>
163 constexpr bool equal(Iterator1 a, Iterator1 ae, Iterator2 b, Iterator2 be) {
Richard Smith4e18ca52013-05-06 05:56:11 +0000164 while (a != ae && b != be)
Richard Smith243ef902013-05-05 23:31:59 +0000165 if (*a++ != *b++)
Richard Smith3da88fa2013-04-26 14:36:30 +0000166 return false;
Richard Smith3da88fa2013-04-26 14:36:30 +0000167 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 }
Richard Smith3229b742013-05-05 21:17:10 +0000212 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 }
Richard Smith3da88fa2013-04-26 14:36:30 +0000217}
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}
Richard Smith243ef902013-05-05 23:31:59 +0000266
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}
Richard Smith4e18ca52013-05-06 05:56:11 +0000336
Richard Smith43e77732013-05-07 04:50:00 +0000337namespace compound_assign {
338 constexpr bool test_int() {
339 int a = 3;
340 a += 6;
Richard Smith861b5b52013-05-07 23:34:45 +0000341 if (a != 9) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000342 a -= 2;
Richard Smith861b5b52013-05-07 23:34:45 +0000343 if (a != 7) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000344 a *= 3;
Richard Smith861b5b52013-05-07 23:34:45 +0000345 if (a != 21) return false;
346 if (&(a /= 10) != &a) return false;
347 if (a != 2) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000348 a <<= 3;
Richard Smith861b5b52013-05-07 23:34:45 +0000349 if (a != 16) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000350 a %= 6;
Richard Smith861b5b52013-05-07 23:34:45 +0000351 if (a != 4) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000352 a >>= 1;
Richard Smith861b5b52013-05-07 23:34:45 +0000353 if (a != 2) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000354 a ^= 10;
Richard Smith861b5b52013-05-07 23:34:45 +0000355 if (a != 8) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000356 a |= 5;
Richard Smith861b5b52013-05-07 23:34:45 +0000357 if (a != 13) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000358 a &= 14;
Richard Smith861b5b52013-05-07 23:34:45 +0000359 if (a != 12) return false;
Richard Smith43e77732013-05-07 04:50:00 +0000360 return true;
361 }
362 static_assert(test_int(), "");
363
Richard Smith861b5b52013-05-07 23:34:45 +0000364 constexpr bool test_float() {
365 float f = 123.;
366 f *= 2;
367 if (f != 246.) return false;
368 if ((f -= 0.5) != 245.5) return false;
369 if (f != 245.5) return false;
370 f /= 0.5;
371 if (f != 491.) return false;
372 f += -40;
373 if (f != 451.) return false;
374 return true;
375 }
376 static_assert(test_float(), "");
377
378 constexpr bool test_ptr() {
379 int arr[123] = {};
380 int *p = arr;
381 if ((p += 4) != &arr[4]) return false;
382 if (p != &arr[4]) return false;
383 p += -1;
384 if (p != &arr[3]) return false;
385 if ((p -= -10) != &arr[13]) return false;
386 if (p != &arr[13]) return false;
387 p -= 11;
388 if (p != &arr[2]) return false;
389 return true;
390 }
391 static_assert(test_ptr(), "");
392
Richard Smith43e77732013-05-07 04:50:00 +0000393 template<typename T>
394 constexpr bool test_overflow() {
395 T a = 1;
Richard Smith861b5b52013-05-07 23:34:45 +0000396 while (a != a / 2)
397 a *= 2; // expected-note {{value 2147483648 is outside the range}} expected-note {{ 9223372036854775808 }} expected-note {{floating point arithmetic produces an infinity}}
Richard Smith43e77732013-05-07 04:50:00 +0000398 return true;
399 }
400
401 static_assert(test_overflow<int>(), ""); // expected-error {{constant}} expected-note {{call}}
402 static_assert(test_overflow<unsigned>(), ""); // ok, unsigned overflow is defined
403 static_assert(test_overflow<short>(), ""); // ok, short is promoted to int before multiplication
404 static_assert(test_overflow<unsigned short>(), ""); // ok
405 static_assert(test_overflow<unsigned long long>(), ""); // ok
406 static_assert(test_overflow<long long>(), ""); // expected-error {{constant}} expected-note {{call}}
Richard Smith861b5b52013-05-07 23:34:45 +0000407 static_assert(test_overflow<float>(), ""); // expected-error {{constant}} expected-note {{call}}
Richard Smith43e77732013-05-07 04:50:00 +0000408
409 constexpr short test_promotion(short k) {
410 short s = k;
411 s *= s;
412 return s;
413 }
414 static_assert(test_promotion(100) == 10000, "");
415 static_assert(test_promotion(200) == -25536, "");
416 static_assert(test_promotion(256) == 0, "");
Richard Smith861b5b52013-05-07 23:34:45 +0000417
418 constexpr const char *test_bounds(const char *p, int o) {
419 return p += o; // expected-note {{element 5 of}} expected-note {{element -1 of}} expected-note {{element 1000 of}}
420 }
421 static_assert(test_bounds("foo", 0)[0] == 'f', "");
422 static_assert(test_bounds("foo", 3)[0] == 0, "");
423 static_assert(test_bounds("foo", 4)[-3] == 'o', "");
424 static_assert(test_bounds("foo" + 4, -4)[0] == 'f', "");
425 static_assert(test_bounds("foo", 5) != 0, ""); // expected-error {{constant}} expected-note {{call}}
426 static_assert(test_bounds("foo", -1) != 0, ""); // expected-error {{constant}} expected-note {{call}}
427 static_assert(test_bounds("foo", 1000) != 0, ""); // expected-error {{constant}} expected-note {{call}}
Richard Smith43e77732013-05-07 04:50:00 +0000428}
429
Richard Smith4e18ca52013-05-06 05:56:11 +0000430namespace loops {
431 constexpr int fib_loop(int a) {
432 int f_k = 0, f_k_plus_one = 1;
433 for (int k = 1; k != a; ++k) {
434 int f_k_plus_two = f_k + f_k_plus_one;
435 f_k = f_k_plus_one;
436 f_k_plus_one = f_k_plus_two;
437 }
438 return f_k_plus_one;
439 }
440 static_assert(fib_loop(46) == 1836311903, "");
441
442 constexpr bool breaks_work() {
443 int a = 0;
444 for (int n = 0; n != 100; ++n) {
445 ++a;
446 if (a == 5) continue;
447 if ((a % 5) == 0) break;
448 }
449
450 int b = 0;
451 while (b != 17) {
452 ++b;
453 if (b == 6) continue;
454 if ((b % 6) == 0) break;
455 }
456
457 int c = 0;
458 do {
459 ++c;
460 if (c == 7) continue;
461 if ((c % 7) == 0) break;
462 } while (c != 21);
463
464 return a == 10 && b == 12 & c == 14;
465 }
466 static_assert(breaks_work(), "");
467
468 void not_constexpr();
469 constexpr bool no_cont_after_break() {
470 for (;;) {
471 break;
472 not_constexpr();
473 }
474 while (true) {
475 break;
476 not_constexpr();
477 }
478 do {
479 break;
480 not_constexpr();
481 } while (true);
482 return true;
483 }
484 static_assert(no_cont_after_break(), "");
485
486 constexpr bool cond() {
487 for (int a = 1; bool b = a != 3; ++a) {
488 if (!b)
489 return false;
490 }
491 while (bool b = true) {
492 b = false;
493 break;
494 }
495 return true;
496 }
497 static_assert(cond(), "");
Richard Smith896e0d72013-05-06 06:51:17 +0000498
499 constexpr int range_for() {
500 int arr[] = { 1, 2, 3, 4, 5 };
501 int sum = 0;
502 for (int x : arr)
Richard Smith43e77732013-05-07 04:50:00 +0000503 sum += x;
Richard Smith896e0d72013-05-06 06:51:17 +0000504 return sum;
505 }
506 static_assert(range_for() == 15, "");
507
508 template<int...N> struct ints {};
509 template<typename A, typename B> struct join_ints;
510 template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> {
511 using type = ints<As..., sizeof...(As) + Bs...>;
512 };
513 template<unsigned N> struct make_ints {
514 using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type;
515 };
516 template<> struct make_ints<0> { using type = ints<>; };
517 template<> struct make_ints<1> { using type = ints<0>; };
518
519 struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} };
520
521 template<typename T, unsigned N> struct array {
522 constexpr array() : arr{} {}
523 template<typename ...X>
524 constexpr array(X ...x) : arr{} {
525 init(typename make_ints<sizeof...(X)>::type{}, x...);
526 }
527 template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) {
528 ignore{arr[I] = x ...};
529 }
530 T arr[N];
531 struct iterator {
532 T *p;
533 constexpr explicit iterator(T *p) : p(p) {}
534 constexpr bool operator!=(iterator o) { return p != o.p; }
535 constexpr iterator &operator++() { ++p; return *this; }
536 constexpr T &operator*() { return *p; }
537 };
538 constexpr iterator begin() { return iterator(arr); }
539 constexpr iterator end() { return iterator(arr + N); }
540 };
541
542 constexpr int range_for_2() {
543 array<int, 5> arr { 1, 2, 3, 4, 5 };
544 int sum = 0;
545 for (int k : arr) {
Richard Smith43e77732013-05-07 04:50:00 +0000546 sum += k;
Richard Smith896e0d72013-05-06 06:51:17 +0000547 if (sum > 8) break;
548 }
549 return sum;
550 }
551 static_assert(range_for_2() == 10, "");
Richard Smith4e18ca52013-05-06 05:56:11 +0000552}
Richard Smith99005e62013-05-07 03:19:20 +0000553
Richard Smith43e77732013-05-07 04:50:00 +0000554namespace assignment_op {
Richard Smith99005e62013-05-07 03:19:20 +0000555 struct A {
556 constexpr A() : n(5) {}
557 int n;
558 struct B {
559 int k = 1;
560 union U {
561 constexpr U() : y(4) {}
562 int x;
563 int y;
564 } u;
565 } b;
566 };
567 constexpr bool testA() {
568 A a, b;
569 a.n = 7;
570 a.b.u.y = 5;
571 b = a;
572 return b.n == 7 && b.b.u.y == 5 && b.b.k == 1;
573 }
574 static_assert(testA(), "");
575
576 struct B {
577 bool assigned = false;
578 constexpr B &operator=(const B&) {
579 assigned = true;
580 return *this;
581 }
582 };
583 struct C : B {
584 B b;
585 int n = 5;
586 };
587 constexpr bool testC() {
588 C c, d;
589 c.n = 7;
590 d = c;
591 c.n = 3;
592 return d.n == 7 && d.assigned && d.b.assigned;
593 }
594 static_assert(testC(), "");
595}