| 1 |
// P3106R1 - Clarifying rules for brace elision in aggregate initialization |
| 2 |
// Examples from C++26 [dcl.init.aggr] |
| 3 |
// { dg-do run } |
| 4 |
|
| 5 |
extern "C" void abort (); |
| 6 |
|
| 7 |
namespace N1 { |
| 8 |
#if __cpp_designated_initializers >= 201707L |
| 9 |
struct C { |
| 10 |
union { |
| 11 |
int a; |
| 12 |
const char* p; |
| 13 |
}; |
| 14 |
int x; |
| 15 |
} c = { .a = 1, .x = 3 }; |
| 16 |
constexpr C c2 = { .a = 1, .x = 3 }; |
| 17 |
static_assert (c2.a == 1 && c2.x == 3, ""); |
| 18 |
#endif |
| 19 |
|
| 20 |
bool |
| 21 |
test () |
| 22 |
{ |
| 23 |
#if __cpp_designated_initializers >= 201707L |
| 24 |
return c.a == 1 && c.x == 3; |
| 25 |
#else |
| 26 |
return true; |
| 27 |
#endif |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
namespace N2 { |
| 32 |
struct A { |
| 33 |
int x; |
| 34 |
struct B { |
| 35 |
int i; |
| 36 |
int j; |
| 37 |
} b; |
| 38 |
} a = { 1, { 2, 3 } }; |
| 39 |
|
| 40 |
#if __cplusplus >= 201703L |
| 41 |
struct base1 { int b1, b2 = 42; }; |
| 42 |
struct base2 { |
| 43 |
base2 () { b3 = 42; } |
| 44 |
int b3; |
| 45 |
}; |
| 46 |
struct derived : base1, base2 { |
| 47 |
int d; |
| 48 |
}; |
| 49 |
|
| 50 |
derived d1 { { 1, 2 }, {}, 4 }; |
| 51 |
derived d2 { {}, {}, 4 }; |
| 52 |
#endif |
| 53 |
|
| 54 |
bool |
| 55 |
test () |
| 56 |
{ |
| 57 |
return a.x == 1 && a.b.i == 2 && a.b.j == 3 |
| 58 |
#if __cplusplus >= 201703L |
| 59 |
&& d1.b1 == 1 && d1.b2 == 2 && d1.b3 == 42 && d1.d == 4 |
| 60 |
&& d2.b1 == 0 && d2.b2 == 42 && d2.b3 == 42 && d2.d == 4 |
| 61 |
#endif |
| 62 |
; |
| 63 |
} |
| 64 |
|
| 65 |
#if __cplusplus >= 201703L |
| 66 |
constexpr A a2 = { 1, { 2, 3 } }; |
| 67 |
static_assert (a2.x == 1 && a2.b.i == 2 && a2.b.j == 3, ""); |
| 68 |
|
| 69 |
struct base3 { |
| 70 |
#if __cplusplus >= 202002L |
| 71 |
constexpr base3 () { b3 = 42; } |
| 72 |
#else |
| 73 |
constexpr base3 () : b3 (42) {} |
| 74 |
#endif |
| 75 |
int b3; |
| 76 |
}; |
| 77 |
struct derived2 : base1, base3 { |
| 78 |
int d; |
| 79 |
}; |
| 80 |
constexpr derived2 d3 { { 1, 2}, {}, 4}; |
| 81 |
constexpr derived2 d4 { {}, {}, 4 }; |
| 82 |
static_assert (d3.b1 == 1 && d3.b2 == 2 && d3.b3 == 42 && d3.d == 4, ""); |
| 83 |
static_assert (d4.b1 == 0 && d4.b2 == 42 && d4.b3 == 42 && d4.d == 4, ""); |
| 84 |
#endif |
| 85 |
} |
| 86 |
|
| 87 |
namespace N3 { |
| 88 |
#if __cplusplus >= 201402L |
| 89 |
struct S { int a; const char *b; int c; int d = b[a]; }; |
| 90 |
S ss = { 1, "asdf" }; |
| 91 |
constexpr S ss2 = { 1, "asdf" }; |
| 92 |
static_assert (ss2.a == 1 && ss2.b[0] == 'a' && ss2.b[3] == 'f' && ss2.c == 0 && ss2.d == 's', ""); |
| 93 |
|
| 94 |
#if __cpp_designated_initializers >= 201707L |
| 95 |
struct string { int s = -42; }; |
| 96 |
struct A { |
| 97 |
string a; |
| 98 |
int b = 42; |
| 99 |
int c = -1; |
| 100 |
}; |
| 101 |
static_assert (A { .c = 21 }.a.s == -42 && A { .c = 21 }.b == 42 && A { .c = 21 }.c == 21, ""); |
| 102 |
#endif |
| 103 |
#endif |
| 104 |
|
| 105 |
bool |
| 106 |
test () |
| 107 |
{ |
| 108 |
#if __cplusplus >= 201402L |
| 109 |
return ss.a == 1 && __builtin_strcmp (ss.b, "asdf") == 0 && ss.c == 0 && ss.d == 's'; |
| 110 |
#else |
| 111 |
return true; |
| 112 |
#endif |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
namespace N4 { |
| 117 |
int x[] = { 1, 3, 5 }; |
| 118 |
|
| 119 |
bool |
| 120 |
test () |
| 121 |
{ |
| 122 |
return sizeof (x) == 3 * sizeof (int) && x[0] == 1 && x[1] == 3 && x[2] == 5; |
| 123 |
} |
| 124 |
|
| 125 |
#if __cplusplus >= 201103L |
| 126 |
constexpr int x2[] = { 1, 3, 5 }; |
| 127 |
static_assert (sizeof (x2) == 3 * sizeof (int) |
| 128 |
&& x2[0] == 1 && x2[1] == 3 && x2[2] == 5, ""); |
| 129 |
#endif |
| 130 |
} |
| 131 |
|
| 132 |
namespace N5 { |
| 133 |
struct X { int i, j, k; }; |
| 134 |
X a[] = { 1, 2, 3, 4, 5, 6 }; |
| 135 |
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 136 |
|
| 137 |
bool |
| 138 |
test () |
| 139 |
{ |
| 140 |
return sizeof (a) == sizeof (b) && __builtin_memcmp (a, b, sizeof (a)) == 0; |
| 141 |
} |
| 142 |
|
| 143 |
#if __cplusplus >= 201103L |
| 144 |
constexpr X a2[] = { 1, 2, 3, 4, 5, 6 }; |
| 145 |
constexpr X b2[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 146 |
static_assert (sizeof (a2) == 2 * sizeof (X) |
| 147 |
&& a2[0].i == 1 && a2[0].j == 2 && a2[0].k == 3 |
| 148 |
&& a2[1].i == 4 && a2[1].j == 5 && a2[1].k == 6, ""); |
| 149 |
static_assert (sizeof (b2) == 2 * sizeof (X) |
| 150 |
&& b2[0].i == 1 && b2[0].j == 2 && b2[0].k == 3 |
| 151 |
&& b2[1].i == 4 && b2[1].j == 5 && b2[1].k == 6, ""); |
| 152 |
#endif |
| 153 |
} |
| 154 |
|
| 155 |
namespace N7 { |
| 156 |
struct A { |
| 157 |
int i; |
| 158 |
static int s; |
| 159 |
int j; |
| 160 |
int : 17; |
| 161 |
int k; |
| 162 |
} a = { 1, 2, 3 }; |
| 163 |
|
| 164 |
bool |
| 165 |
test () |
| 166 |
{ |
| 167 |
return a.i == 1 && a.j == 2 && a.k == 3; |
| 168 |
} |
| 169 |
|
| 170 |
#if __cplusplus >= 201103L |
| 171 |
constexpr A a2 = { 1, 2, 3 }; |
| 172 |
static_assert (a2.i == 1 && a2.j == 2 && a2.k == 3, ""); |
| 173 |
#endif |
| 174 |
} |
| 175 |
|
| 176 |
namespace N9 { |
| 177 |
int x[2][2] = { 3, 1, 4, 2 }; |
| 178 |
float y[4][3] = { |
| 179 |
{ 1 }, { 2 }, { 3 }, { 4 } |
| 180 |
}; |
| 181 |
|
| 182 |
bool |
| 183 |
test () |
| 184 |
{ |
| 185 |
return x[0][0] == 3 && x[0][1] == 1 && x[1][0] == 4 && x[1][1] == 2 |
| 186 |
&& y[0][0] == 1.f && y[0][1] == 0.f && y[0][2] == 0.f |
| 187 |
&& y[1][0] == 2.f && y[1][1] == 0.f && y[1][2] == 0.f |
| 188 |
&& y[2][0] == 3.f && y[2][1] == 0.f && y[2][2] == 0.f |
| 189 |
&& y[3][0] == 4.f && y[3][1] == 0.f && y[3][2] == 0.f; |
| 190 |
} |
| 191 |
|
| 192 |
#if __cplusplus >= 201103L |
| 193 |
constexpr int x2[2][2] = { 3, 1, 4, 2 }; |
| 194 |
constexpr float y2[4][3] = { |
| 195 |
{ 1 }, { 2 }, { 3 }, { 4 } |
| 196 |
}; |
| 197 |
static_assert (x2[0][0] == 3 && x2[0][1] == 1 && x2[1][0] == 4 && x2[1][1] == 2, ""); |
| 198 |
static_assert (y2[0][0] == 1.f && y2[0][1] == 0.f && y2[0][2] == 0.f, ""); |
| 199 |
static_assert (y2[1][0] == 2.f && y2[1][1] == 0.f && y2[1][2] == 0.f, ""); |
| 200 |
static_assert (y2[2][0] == 3.f && y2[2][1] == 0.f && y2[2][2] == 0.f, ""); |
| 201 |
static_assert (y2[3][0] == 4.f && y2[3][1] == 0.f && y2[3][2] == 0.f, ""); |
| 202 |
#endif |
| 203 |
} |
| 204 |
|
| 205 |
namespace N10 { |
| 206 |
struct S1 { int a, b; }; |
| 207 |
struct S2 { S1 s, t; }; |
| 208 |
|
| 209 |
S2 x[2] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 210 |
S2 y[2] = { |
| 211 |
{ |
| 212 |
{ 1, 2 }, |
| 213 |
{ 3, 4 } |
| 214 |
}, |
| 215 |
{ |
| 216 |
{ 5, 6 }, |
| 217 |
{ 7, 8 } |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
bool |
| 222 |
test () |
| 223 |
{ |
| 224 |
return sizeof (x) == sizeof (y) && __builtin_memcmp (x, y, sizeof (x)) == 0; |
| 225 |
} |
| 226 |
|
| 227 |
#if __cplusplus >= 201103L |
| 228 |
constexpr S2 x2[2] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 229 |
constexpr S2 y2[2] = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }; |
| 230 |
static_assert (x2[0].s.a == 1 && x2[0].s.b == 2 && x2[0].t.a == 3 && x2[0].t.b == 4, ""); |
| 231 |
static_assert (x2[1].s.a == 5 && x2[1].s.b == 6 && x2[1].t.a == 7 && x2[1].t.b == 8, ""); |
| 232 |
static_assert (y2[0].s.a == 1 && y2[0].s.b == 2 && y2[0].t.a == 3 && y2[0].t.b == 4, ""); |
| 233 |
static_assert (y2[1].s.a == 5 && y2[1].s.b == 6 && y2[1].t.a == 7 && y2[1].t.b == 8, ""); |
| 234 |
#endif |
| 235 |
} |
| 236 |
|
| 237 |
namespace N12 { |
| 238 |
float y[4][3] = { |
| 239 |
{ 1, 3, 5 }, |
| 240 |
{ 2, 4, 6 }, |
| 241 |
{ 3, 5, 7 }, |
| 242 |
}; |
| 243 |
float y2[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 }; |
| 244 |
|
| 245 |
bool |
| 246 |
test () |
| 247 |
{ |
| 248 |
for (int i = 0; i < 4; ++i) |
| 249 |
for (int j = 0; j < 3; ++j) |
| 250 |
if (y[i][j] != (i == 3 ? 0.f : (float) (i + 1 + j * 2)) |
| 251 |
|| y[i][j] != y2[i][j]) |
| 252 |
return false; |
| 253 |
return true; |
| 254 |
} |
| 255 |
|
| 256 |
#if __cplusplus >= 201103L |
| 257 |
constexpr float y3[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 }, }; |
| 258 |
constexpr float y4[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 }; |
| 259 |
static_assert (y3[0][0] == 1.f && y3[0][1] == 3.f && y3[0][2] == 5.f, ""); |
| 260 |
static_assert (y3[1][0] == 2.f && y3[1][1] == 4.f && y3[1][2] == 6.f, ""); |
| 261 |
static_assert (y3[2][0] == 3.f && y3[2][1] == 5.f && y3[2][2] == 7.f, ""); |
| 262 |
static_assert (y3[3][0] == 0.f && y3[3][1] == 0.f && y3[3][2] == 0.f, ""); |
| 263 |
static_assert (y4[0][0] == 1.f && y4[0][1] == 3.f && y4[0][2] == 5.f, ""); |
| 264 |
static_assert (y4[1][0] == 2.f && y4[1][1] == 4.f && y4[1][2] == 6.f, ""); |
| 265 |
static_assert (y4[2][0] == 3.f && y4[2][1] == 5.f && y4[2][2] == 7.f, ""); |
| 266 |
static_assert (y4[3][0] == 0.f && y4[3][1] == 0.f && y4[3][2] == 0.f, ""); |
| 267 |
#endif |
| 268 |
} |
| 269 |
|
| 270 |
namespace N13 { |
| 271 |
bool |
| 272 |
test () |
| 273 |
{ |
| 274 |
struct S { } s; |
| 275 |
struct A { |
| 276 |
S s1; |
| 277 |
int i1; |
| 278 |
S s2; |
| 279 |
int i2; |
| 280 |
S s3; |
| 281 |
int i3; |
| 282 |
} a = { |
| 283 |
{ }, |
| 284 |
0, |
| 285 |
s, |
| 286 |
0 |
| 287 |
}; |
| 288 |
return a.i1 == 0 && a.i2 == 0; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
namespace N14 { |
| 293 |
struct A { |
| 294 |
int i; |
| 295 |
operator int (); |
| 296 |
}; |
| 297 |
struct B { |
| 298 |
A a1, a2; |
| 299 |
int z; |
| 300 |
}; |
| 301 |
A a; |
| 302 |
B b = { 4, a, a }; |
| 303 |
A::operator int () { return 42; } |
| 304 |
|
| 305 |
bool |
| 306 |
test () |
| 307 |
{ |
| 308 |
return b.a1.i == 4 && b.a2.i == 0 && b.z == 42; |
| 309 |
} |
| 310 |
|
| 311 |
#if __cplusplus >= 201103L |
| 312 |
struct A2 { |
| 313 |
int i; |
| 314 |
constexpr operator int () const { return 42; } |
| 315 |
}; |
| 316 |
struct B2 { |
| 317 |
A2 a1, a2; |
| 318 |
int z; |
| 319 |
}; |
| 320 |
constexpr A2 a2 = { 26 }; |
| 321 |
constexpr B2 b2 = { 4, a2, a2 }; |
| 322 |
static_assert (b2.a1.i == 4 && b2.a2.i == 26 && b2.z == 42, ""); |
| 323 |
#endif |
| 324 |
} |
| 325 |
|
| 326 |
int |
| 327 |
main () |
| 328 |
{ |
| 329 |
if (!N1::test () |
| 330 |
|| !N2::test () |
| 331 |
|| !N3::test () |
| 332 |
|| !N4::test () |
| 333 |
|| !N5::test () |
| 334 |
|| !N7::test () |
| 335 |
|| !N9::test () |
| 336 |
|| !N10::test () |
| 337 |
|| !N12::test () |
| 338 |
|| !N13::test () |
| 339 |
|| !N14::test ()) |
| 340 |
abort (); |
| 341 |
} |