| 1 |
// PR c++/91363 - P0960R3: Parenthesized initialization of aggregates. |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
|
| 4 |
// Test ill-formed code. |
| 5 |
|
| 6 |
// If k is greater than the size of the array, the program is ill-formed. |
| 7 |
int a1[2](1, 2, 3); // { dg-error "too many initializers" } |
| 8 |
int a2[](); // { dg-error "array of functions" } |
| 9 |
int a3[](()); // { dg-error "expected primary-expression" } |
| 10 |
int a4[]("raccoon"); // { dg-error "invalid conversion" } |
| 11 |
|
| 12 |
struct S { |
| 13 |
int i; |
| 14 |
int j; |
| 15 |
}; |
| 16 |
|
| 17 |
S s1(1, 1, 1); // { dg-error "too many initializers" } |
| 18 |
|
| 19 |
union U2 { |
| 20 |
int a; |
| 21 |
float b; |
| 22 |
}; |
| 23 |
|
| 24 |
// [dcl.init.aggr]/19: |
| 25 |
// When a union is initialized with an initializer list, there shall not be |
| 26 |
// more than one explicitly initialized element. |
| 27 |
U2 u4 = U2(1, 2); // { dg-error "too many initializers" } |
| 28 |
|
| 29 |
// Test there is no brace elision. |
| 30 |
|
| 31 |
int a[2][2](1, 2, 3, 4); // { dg-error "too many initializers|array must be initialized with a brace-enclosed initializer" } |
| 32 |
|
| 33 |
// private/protected/virtual base class -> not an aggregate. |
| 34 |
struct B { }; |
| 35 |
struct D : private B { |
| 36 |
int i; |
| 37 |
int j; |
| 38 |
}; |
| 39 |
|
| 40 |
D d({}, 1, 2); // { dg-error "no matching function" } |
| 41 |
|
| 42 |
// Private non-static data member -> not an aggregate. |
| 43 |
struct P { |
| 44 |
int i; |
| 45 |
private: |
| 46 |
int j; |
| 47 |
}; |
| 48 |
|
| 49 |
P p(1, 2); // { dg-error "no matching function" } |
| 50 |
|
| 51 |
// User-declared constructor -> not an aggregate. |
| 52 |
struct U { |
| 53 |
U() {} |
| 54 |
int i; |
| 55 |
int j; |
| 56 |
}; |
| 57 |
|
| 58 |
U u(1, 2); // { dg-error "no matching function" } |
| 59 |
|
| 60 |
// virtual member function -> not an aggregate. |
| 61 |
struct V { |
| 62 |
int i; |
| 63 |
int j; |
| 64 |
virtual int foo(int); |
| 65 |
}; |
| 66 |
|
| 67 |
V v(1, 2); // { dg-error "no matching function" } |
| 68 |
|
| 69 |
struct nonaggr { |
| 70 |
int i; |
| 71 |
int j; |
| 72 |
private: |
| 73 |
int x; |
| 74 |
}; |
| 75 |
|
| 76 |
struct F { |
| 77 |
nonaggr n; |
| 78 |
F() : n(1) { } // { dg-error "no matching function" } |
| 79 |
}; |
| 80 |
|
| 81 |
struct G { |
| 82 |
char a[4]; |
| 83 |
}; |
| 84 |
|
| 85 |
struct H { |
| 86 |
G g; |
| 87 |
H() : g("oaks") { } // { dg-error "initializer-string" } |
| 88 |
}; |