| 1 |
// PR c++/92812 |
| 2 |
// { dg-do run { target c++20 } } |
| 3 |
// { dg-options "-Wall -Wextra" } |
| 4 |
// Initializing arrays in a member init list using ()-init, valid cases. |
| 5 |
|
| 6 |
#define assert(X) do { if (!(X)) __builtin_abort(); } while(0) |
| 7 |
|
| 8 |
struct S { int x, y; }; |
| 9 |
struct N { int x, y; N(int, int); }; |
| 10 |
|
| 11 |
static S s{10, 11}; |
| 12 |
|
| 13 |
struct A { |
| 14 |
S s1; |
| 15 |
S s2; |
| 16 |
S a1[2]; |
| 17 |
S a2[2]; |
| 18 |
S a3[2]; |
| 19 |
S a4[2]; |
| 20 |
S a5[2]; |
| 21 |
S a6[2]; |
| 22 |
A() : s1(1, 2), |
| 23 |
s2(1), // { dg-warning "missing initializer for member" } |
| 24 |
a1({1}), // { dg-warning "missing initializer for member" } |
| 25 |
a2({1, 2}), |
| 26 |
a3({}, {}), |
| 27 |
a4(), |
| 28 |
a5(s), |
| 29 |
a6(s, s) |
| 30 |
{ } |
| 31 |
}; |
| 32 |
|
| 33 |
struct C { |
| 34 |
int a1[2]; |
| 35 |
int a2[2]; |
| 36 |
int a3[2]; |
| 37 |
int a4[2]; |
| 38 |
int a5[2]; |
| 39 |
C() : a1(1), |
| 40 |
a2(1, 2), |
| 41 |
a3({1}), |
| 42 |
a4({}, {}), |
| 43 |
a5() |
| 44 |
{ } |
| 45 |
}; |
| 46 |
|
| 47 |
struct D { |
| 48 |
N n; |
| 49 |
// Not an aggregate, should work pre-C++20 too. |
| 50 |
D() : n(1, 2) { } |
| 51 |
}; |
| 52 |
|
| 53 |
struct E { |
| 54 |
char a1[4]; |
| 55 |
char a2[4]; |
| 56 |
E() : a1("ab"), |
| 57 |
a2("abc") |
| 58 |
{ } |
| 59 |
}; |
| 60 |
|
| 61 |
// Compound literal. |
| 62 |
struct F { |
| 63 |
F (); |
| 64 |
S m[1]; |
| 65 |
}; |
| 66 |
|
| 67 |
F::F () : m(__extension__(S[1]) { 1, 2 }) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
struct B { int i; }; |
| 72 |
struct Der : B { }; |
| 73 |
Der d; |
| 74 |
struct G { |
| 75 |
B b1[1]; |
| 76 |
B b2[2]; |
| 77 |
G(): b1(d), |
| 78 |
b2(d, d) |
| 79 |
{ } |
| 80 |
}; |
| 81 |
|
| 82 |
// Variation of c++/93790. |
| 83 |
struct Empty { }; |
| 84 |
struct Empty_refwrap { |
| 85 |
Empty& r; |
| 86 |
Empty_refwrap(Empty &e) : r(e) { } |
| 87 |
operator Empty&() { return r; } |
| 88 |
}; |
| 89 |
|
| 90 |
Empty empty; |
| 91 |
Empty_refwrap empty_refwrap(empty); |
| 92 |
|
| 93 |
struct H { |
| 94 |
Empty &e; |
| 95 |
// Turning this into {empty_refwrap} would break things. |
| 96 |
H() : e(empty_refwrap) { } |
| 97 |
}; |
| 98 |
|
| 99 |
int |
| 100 |
main () |
| 101 |
{ |
| 102 |
A a; |
| 103 |
assert (a.s1.x == 1 && a.s1.y == 2); |
| 104 |
assert (a.s2.x == 1 && a.s2.y == 0); |
| 105 |
assert (a.a1[0].x == 1 && a.a1[0].y == 0 |
| 106 |
&& a.a1[1].x == 0 && a.a1[1].y == 0); |
| 107 |
assert (a.a2[0].x == 1 && a.a2[0].y == 2 |
| 108 |
&& a.a2[1].x == 0 && a.a2[1].y == 0); |
| 109 |
assert (a.a3[0].x == 0 && a.a3[0].y == 0 |
| 110 |
&& a.a3[1].x == 0 && a.a3[1].y == 0); |
| 111 |
assert (a.a4[0].x == 0 && a.a4[0].y == 0 |
| 112 |
&& a.a4[1].x == 0 && a.a4[1].y == 0); |
| 113 |
assert (a.a5[0].x == 10 && a.a5[0].y == 11 |
| 114 |
&& a.a5[1].x == 0 && a.a5[1].y == 0); |
| 115 |
assert (a.a6[0].x == 10 && a.a6[0].y == 11 |
| 116 |
&& a.a6[1].x == 10 && a.a6[1].y == 11); |
| 117 |
|
| 118 |
C c; |
| 119 |
assert (c.a1[0] == 1 && c.a1[1] == 0); |
| 120 |
assert (c.a2[0] == 1 && c.a2[1] == 2); |
| 121 |
assert (c.a3[0] == 1 && c.a3[1] == 0); |
| 122 |
assert (c.a4[0] == 0 && c.a4[1] == 0); |
| 123 |
assert (c.a5[0] == 0 && c.a5[1] == 0); |
| 124 |
|
| 125 |
E e; |
| 126 |
assert (__builtin_strcmp (e.a1, "ab") == 0 |
| 127 |
&& __builtin_strcmp (e.a2, "abc") == 0); |
| 128 |
} |