Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/constexpr-union3.C

raw
1 // { dg-do compile { target c++20 } }
2
3 struct S
4 {
5 union {
6 char buf[8];
7 char* ptr;
8 };
9 unsigned len;
10
11 constexpr S(const char* s, unsigned n)
12 {
13 char* p;
14 if (n > 7)
15 p = ptr = new char[n+1];
16 else
17 p = buf;
18 for (len = 0; len < n; ++len)
19 p[len] = s[len]; // { dg-error "accessing uninitialized member" }
20 p[len] = '\0';
21 }
22
23 constexpr ~S()
24 {
25 if (len > 7)
26 delete[] ptr;
27 }
28 };
29
30 constexpr bool test1()
31 {
32 S s("test", 4); // { dg-message "in .constexpr. expansion" }
33 return true;
34 }
35
36 constexpr bool a = test1(); // { dg-message "in .constexpr. expansion" }
37
38
39 constexpr bool test2()
40 {
41 S s("hello world", 11);
42 return true;
43 }
44
45 constexpr bool b = test2();