Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/bit-cast12.C

raw
1 // P1272R4
2 // { dg-do compile { target c++14 } }
3
4 namespace std
5 {
6 enum class byte : unsigned char {};
7 }
8
9 struct S { unsigned char a[2]; alignas(sizeof 0) int b; };
10 struct T { char a; alignas(sizeof 0) int b; };
11 struct U { char a : 1; char : 6; char b : 1; };
12 struct V { int a; S b; };
13 struct W { unsigned a; T b; };
14
15 constexpr bool
16 f1 ()
17 {
18 T t = { 1, 2 };
19 S s = __builtin_bit_cast (S, t);
20 return s.a[0] == 1;
21 }
22
23 constexpr bool
24 f2 ()
25 {
26 U u = { 0, 0 };
27 unsigned char a = __builtin_bit_cast (unsigned char, u);
28 return true;
29 }
30
31 constexpr bool
32 f3 ()
33 {
34 T t = { 1, 2 };
35 S s = __builtin_bit_cast (S, t);
36 return s.a[1] == 0; // { dg-error "accessing uninitialized array element" }
37 }
38
39 constexpr bool
40 f4 ()
41 {
42 U u = { 0, 0 };
43 unsigned char a = __builtin_bit_cast (unsigned char, u);
44 return a == 0; // { dg-error "is not a constant expression" }
45 }
46
47 constexpr bool
48 f5 ()
49 {
50 W t = { 1, 2 };
51 V s = __builtin_bit_cast (V, t);
52 return s.b.a[0] == 1;
53 }
54
55 constexpr bool
56 f6 ()
57 {
58 W t = { 1, 2 };
59 V s = __builtin_bit_cast (V, t);
60 return s.b.a[1] == 1; // { dg-error "accessing uninitialized array element" }
61 }
62
63 constexpr bool a = f1 ();
64 constexpr bool b = f2 ();
65 constexpr bool c = f3 (); // { dg-message "in .constexpr. expansion" }
66 constexpr bool d = f4 (); // { dg-message "in .constexpr. expansion" }
67 constexpr bool e = f5 ();
68 constexpr bool f = f6 (); // { dg-message "in .constexpr. expansion" }