| 1 |
// PR c++/91363 - P0960R3: Parenthesized initialization of aggregates. |
| 2 |
// { dg-do run { target c++20 } } |
| 3 |
|
| 4 |
// Test T[](). |
| 5 |
|
| 6 |
int i; |
| 7 |
int a1[](1, 2, 3); |
| 8 |
static_assert(sizeof(a1) == 3 * sizeof (int), ""); |
| 9 |
int a2[](1.0, 2, 3); |
| 10 |
static_assert(sizeof(a2) == 3 * sizeof (int), ""); |
| 11 |
int a3[3](1, 2, 3); |
| 12 |
int a4[3](1, 2); // a4[2] is value-initialized. |
| 13 |
int a5[](++i, ++i); |
| 14 |
static_assert(sizeof(a5) == 2 * sizeof (int), ""); |
| 15 |
int a6[](1); |
| 16 |
static_assert(sizeof(a6) == sizeof (int), ""); |
| 17 |
int a7[]({}); |
| 18 |
static_assert(sizeof(a7) == sizeof (int), ""); |
| 19 |
int a8[]({}, {}, {}, {}, {}, {}); |
| 20 |
static_assert(sizeof(a8) == 6 * sizeof (int), ""); |
| 21 |
int a9[]((1)); |
| 22 |
static_assert(sizeof(a9) == sizeof (int), ""); |
| 23 |
int a10[]((1), (2), (3)); |
| 24 |
static_assert(sizeof(a10) == 3 * sizeof (int), ""); |
| 25 |
int a11[][2]{1}; |
| 26 |
static_assert(sizeof(a11) == 2 * sizeof (int), ""); |
| 27 |
int a12[][2]({1, 2}, {3, 4}); |
| 28 |
static_assert(sizeof(a12) == 4 * sizeof (int), ""); |
| 29 |
|
| 30 |
const int (&ra1)[](1, 2); |
| 31 |
const int (&ra2)[](1.0, 2); |
| 32 |
const int (&ra3)[2](1.0, 2); |
| 33 |
int (&&rra1)[](1, 2); |
| 34 |
int (&&rra2)[](1.0, 2); |
| 35 |
int (&&rra3)[2](1.0, 2); |
| 36 |
|
| 37 |
struct S { int i; } s; |
| 38 |
S s1[]({1}); |
| 39 |
static_assert(sizeof(s1) == sizeof (S), ""); |
| 40 |
S s2[]({1}, {2}); |
| 41 |
static_assert(sizeof(s2) == 2 * sizeof (S), ""); |
| 42 |
S s3[]({1}, {2}, {3}); |
| 43 |
static_assert(sizeof(s3) == 3 * sizeof (S), ""); |
| 44 |
S s4[3]({1}, {2}); |
| 45 |
static_assert(sizeof(s4) == 3 * sizeof (S), ""); |
| 46 |
S s5[]({++i}, {++i}); |
| 47 |
static_assert(sizeof(s2) == 2 * sizeof (S), ""); |
| 48 |
S s6[](s, s); |
| 49 |
|
| 50 |
struct R { int i; int j; }; |
| 51 |
R r1[]({1, 2}); |
| 52 |
static_assert(sizeof(r1) == sizeof (R), ""); |
| 53 |
R r2[]({1, 2}, {3, 4}); |
| 54 |
static_assert(sizeof(r2) == 2 * sizeof (R), ""); |
| 55 |
R r3[]({1.0, 2}, {3.2, 4}); |
| 56 |
static_assert(sizeof(r3) == 2 * sizeof (R), ""); |
| 57 |
|
| 58 |
char c1[]('r'); |
| 59 |
char c2[]('r', 'a', 'c', 'c', 'o', 'o', 'n'); |
| 60 |
char c3[]("yarrow"); |
| 61 |
char c4[4]("oak"); |
| 62 |
char c5[10]("cat"); |
| 63 |
const char (&c6)[4]("eel"); |
| 64 |
|
| 65 |
int g; |
| 66 |
struct X { |
| 67 |
int i; |
| 68 |
X() { ++g; } |
| 69 |
X(int) { }; |
| 70 |
}; |
| 71 |
|
| 72 |
int |
| 73 |
main () |
| 74 |
{ |
| 75 |
// Here we'll value-initialize l[1] and l[2], which will zero-initialize it. |
| 76 |
int l[3](42); |
| 77 |
if (l[0] != 42 || l[1] != 0 || l[2] != 0) |
| 78 |
__builtin_abort (); |
| 79 |
|
| 80 |
// Here we'll value-initialize x[2] and x[3]. Since X is a class type |
| 81 |
// with a user-provided ctor, we'll default-initialize in both cases. |
| 82 |
X x[4]({ 1 }, { 2 }); |
| 83 |
if (g != 2) |
| 84 |
__builtin_abort (); |
| 85 |
|
| 86 |
if (a1[0] != 1 || a1[1] != 2 || a1[2] != 3) |
| 87 |
__builtin_abort (); |
| 88 |
if (a2[0] != 1 || a2[1] != 2 || a2[2] != 3) |
| 89 |
__builtin_abort (); |
| 90 |
if (a3[0] != 1 || a3[1] != 2 || a3[2] != 3) |
| 91 |
__builtin_abort (); |
| 92 |
if (a4[0] != 1 || a4[1] != 2 || a4[2] != 0) |
| 93 |
__builtin_abort (); |
| 94 |
if (a5[0] != 1 || a5[1] != 2) |
| 95 |
__builtin_abort (); |
| 96 |
if (a6[0] != 1) |
| 97 |
__builtin_abort (); |
| 98 |
if (a7[0] != 0) |
| 99 |
__builtin_abort (); |
| 100 |
if (a8[0] != 0 || a8[1] != 0 || a8[2] != 0 || a8[3] != 0 |
| 101 |
|| a8[4] != 0 || a8[5] != 0) |
| 102 |
__builtin_abort (); |
| 103 |
if (a9[0] != 1) |
| 104 |
__builtin_abort (); |
| 105 |
if (a10[0] != 1 || a10[1] != 2 || a10[2] != 3) |
| 106 |
__builtin_abort (); |
| 107 |
if (a11[0][0] != 1 || a11[0][1] != 0) |
| 108 |
__builtin_abort (); |
| 109 |
if (a12[0][0] != 1 || a12[0][1] != 2 || a12[1][0] != 3 || a12[1][1] != 4) |
| 110 |
__builtin_abort (); |
| 111 |
|
| 112 |
if (ra1[0] != 1 || ra1[1] != 2) |
| 113 |
__builtin_abort (); |
| 114 |
if (ra2[0] != 1 || ra2[1] != 2) |
| 115 |
__builtin_abort (); |
| 116 |
if (ra3[0] != 1 || ra3[1] != 2) |
| 117 |
__builtin_abort (); |
| 118 |
if (rra1[0] != 1 || rra1[1] != 2) |
| 119 |
__builtin_abort (); |
| 120 |
if (rra2[0] != 1 || rra2[1] != 2) |
| 121 |
__builtin_abort (); |
| 122 |
if (rra3[0] != 1 || rra3[1] != 2) |
| 123 |
__builtin_abort (); |
| 124 |
|
| 125 |
if (s1[0].i != 1) |
| 126 |
__builtin_abort (); |
| 127 |
if (s2[0].i != 1 || s2[1].i != 2) |
| 128 |
__builtin_abort (); |
| 129 |
if (s3[0].i != 1 || s3[1].i != 2 || s3[2].i != 3) |
| 130 |
__builtin_abort (); |
| 131 |
if (s4[0].i != 1 || s4[1].i != 2 || s4[2].i != 0) |
| 132 |
__builtin_abort (); |
| 133 |
if (s5[0].i != 3 || s5[1].i != 4) |
| 134 |
__builtin_abort (); |
| 135 |
|
| 136 |
if (r1[0].i != 1 || r1[0].j != 2) |
| 137 |
__builtin_abort (); |
| 138 |
if (r2[0].i != 1 || r2[0].j != 2 || r2[1].i != 3 || r2[1].j != 4) |
| 139 |
__builtin_abort (); |
| 140 |
if (r3[0].i != 1 || r3[0].j != 2 || r3[1].i != 3 || r3[1].j != 4) |
| 141 |
__builtin_abort (); |
| 142 |
} |