Debian Codegraph @

gcc/testsuite/g++.dg/cpp26/aggr-init2.C

raw
1 // P3106R1 - Clarifying rules for brace elision in aggregate initialization
2 // Examples from C++26 [dcl.init.aggr]
3 // { dg-do compile }
4
5 namespace N1 {
6 #if __cpp_designated_initializers >= 201707L
7 struct C {
8 union {
9 int a;
10 const char* p;
11 };
12 int x;
13 };
14 constexpr C c2 = { .a = 42.0, .x = 3 }; // { dg-error "narrowing conversion of '4.2e\\\+1' from 'double' to 'int'" "" { target c++20 } }
15 #endif
16 }
17
18 namespace N6 {
19 #if __cplusplus >= 201103L
20 struct S {
21 int y[] = { 0 }; // { dg-error "ISO C\\\+\\\+ forbids flexible array member 'y'" "" { target c++11 } }
22 // { dg-error "flexible array member 'N6::S::y' in an otherwise empty 'struct N6::S' is a GCC extension" "" { target c++11 } .-1 }
23 // { dg-error "initializer for flexible array member 'int N6::S::y \\\[\\\]'" "" { target c++11 } .-2 }
24 };
25 #endif
26 }
27
28 namespace N8 {
29 #if __cplusplus >= 201402L
30 struct A;
31 extern A a;
32 struct A {
33 const A &a1 { A { a, a } };
34 const A &a2 { A { } }; // { dg-error "default member initializer for 'N8::A::a2' required before the end of its enclosing class" "" { target c++14 } }
35 }; // { dg-error "invalid initialization of reference of type 'const N8::A\\\&' from expression of type '<brace-enclosed initializer list>'" "" { target c++14 } .-1 }
36 A a { a, a };
37
38 struct B {
39 int n = B {}.n; // { dg-error "default member initializer for 'N8::B::n' required before the end of its enclosing class" "" { target c++14 } }
40 };
41
42 struct C;
43 extern C c;
44 struct C {
45 const C &c1 { C { c, c } };
46 const C &c2 { C { c, c } };
47 };
48 C c { c, c };
49 #endif
50 }
51
52 namespace N11 {
53 char cv[4] = { 'a', 's', 'd', 'f', 0 }; // { dg-error "too many initializers for 'char \\\[4\\\]'" }
54 }
55
56 namespace N15 {
57 union u { int a; const char* b; };
58 u a = { 1 };
59 u b = a;
60 u c = 1; // { dg-error "conversion from 'int' to non-scalar type 'N15::u' requested" }
61 u d = { 0, "asdf" }; // { dg-error "too many initializers for 'N15::u'" }
62 u e = { "asdf" }; // { dg-error "invalid conversion from 'const char\\\*' to 'int'" }
63 #if __cpp_designated_initializers >= 201707L
64 u f = { .b = "asdf" };
65 u g = { .a = 1, .b = "asdf" }; // { dg-error "too many initializers for 'N15::u'" "" { target c++20 } }
66 #endif
67 }