1 // P0138R2 - direct-list-initialization of enums
2 // { dg-do compile { target c++11 } }
3
4 enum A { G = 26 };
5 enum B : short {};
6 enum class C {};
7 enum struct D : long {};
8 enum class E : unsigned char { e = 7 };
CS9 struct S { operator C () { return C (s); } int s; } s;
10 struct T { operator long () { return t; } long t; } t;
11 struct V { E v; };
12 long l;
13 long long ll;
14 short c;
15 void bar (E);
16
17 void
foo()18 foo ()
19 {
20 A a1 { 5 }; // { dg-error "invalid conversion from 'int' to 'A'" }
21 B b1 { 7 }; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
22 C c1 { s };
23 D d1 { D(t) }; // { dg-error "invalid cast from type 'T' to type 'D'" }
24 D d2 { t }; // { dg-error "cannot convert 'T' to 'D' in initialization" "" { target c++14_down } }
25 // { dg-error "invalid cast from type 'T' to type 'D'" "" { target c++17 } .-1 }
26 D d3 { 9 }; // { dg-error "cannot convert 'int' to 'D' in initialization" "" { target c++14_down } }
27 D d4 { l }; // { dg-error "cannot convert 'long int' to 'D' in initialization" "" { target c++14_down } }
28 D d5 { D(l) };
29 D d6 { G }; // { dg-error "cannot convert 'A' to 'D' in initialization" "" { target c++14_down } }
30 E e1 { 5 }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
31 E e2 { -1 }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
32 // { dg-error "narrowing conversion of '-1' from 'int' to 'unsigned char' inside" "" { target c++17 } .-1 }
33 E e3 { 5.0 }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
34 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
35 E e4 { 5.2 }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
36 // { dg-error "narrowing conversion of '5.\[0-9]*e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
37 B b2 = { 7 }; // { dg-error "invalid conversion from 'int' to 'B'" }
38 C c2 = { C { 8 } }; // { dg-error "cannot convert 'int' to 'C' in initialization" "" { target c++14_down } }
39
40 D *d7 = new D { 9 }; // { dg-error "cannot convert \[^\n\r]* to 'D' in initialization" "" { target c++14_down } }
41 E *e5 = new E { -4 }; // { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
42 // { dg-error "narrowing conversion of '-4' from 'int' to 'unsigned char' inside" "" { target c++17 } .-1 }
43 bar ({ 10 }); // { dg-error "cannot convert \[^\n\r]* to 'E'" }
44 bar (E { 9 }); // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
45 V v1 = { { 11 } }; // { dg-error "braces around scalar initializer for type 'E'" }
46 V v2 = { E { 12 } }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
47 V v3 = { E { 5.0 } }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
48 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
49 V v4 = { 13 }; // { dg-error "cannot convert 'int' to 'E' in initialization" }
50 if (B b3 { 5 }) // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
51 ;
52 if (B b4 { 4.0 }) // { dg-error "cannot convert 'double' to 'B' in initialization" "" { target c++14_down } }
53 ; // { dg-error "narrowing conversion of '4.0e.0' from 'double' to 'short int' inside" "" { target c++17 } .-1 }
54 C c3 { 8L }; // { dg-error "cannot convert 'long int' to 'C' in initialization" "" { target c++14_down } }
55 B b4 {short (c + 5)}; // { dg-error "invalid conversion from 'short int' to 'B'" "" { target c++14_down } }
56 B b5 {c + 5}; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
57 // { dg-error "narrowing conversion of \[^\n\r]* from 'int' to 'short int' inside" "" { target c++17 } .-1 }
58 C c4 { ll }; // { dg-error "cannot convert 'long long int' to 'C' in initialization" "" { target c++14_down } }
59 // { dg-error "narrowing conversion of 'll' from 'long long int' to 'int' inside" "" { target c++17 } .-1 }
60 C c5 {short (c + 5)}; // { dg-error "cannot convert 'short int' to 'C' in initialization" "" { target c++14_down } }
61 C c6 {c + 5}; // { dg-error "cannot convert 'int' to 'C' in initialization" "" { target c++14_down } }
62 }
63
64 struct U
65 {
UU66 U () : e { 5 } {} // { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
UU67 U (int) : e { 5.0 } {}// { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
68 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
UU69 U (float) : e({ 6 }) {}// { dg-error "list-initializer for non-class type must not be parenthesized" }
70 // { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target *-*-* } .-1 }
71 E e;
72 };
73
74 struct W
75 {
76 A a { 5 }; // { dg-error "invalid conversion from 'int' to 'A'" }
77 B b { 6 }; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
78 C c { 3.0f }; // { dg-error "cannot convert \[^\n\r]* to 'C' in initialization" "" { target c++14_down } }
79 // { dg-error "narrowing conversion of '3.0e.0f' from 'float' to 'int' inside" "" { target c++17 } .-1 }
80 D d = { 7 }; // { dg-error "cannot convert \[^\n\r]* to 'D' in initialization" }
81 };
82
83 template <int N>
84 void
foo2()85 foo2 ()
86 {
87 A a1 { 5 }; // { dg-error "invalid conversion from 'int' to 'A'" }
88 B b1 { 7 }; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
89 C c1 { s };
90 D d1 { D(t) }; // { dg-error "invalid cast from type 'T' to type 'D'" }
91 D d2 { t }; // { dg-error "cannot convert 'T' to 'D' in initialization" "" { target c++14_down } }
92 // { dg-error "invalid cast from type 'T' to type 'D'" "" { target c++17 } .-1 }
93 D d3 { 9 }; // { dg-error "cannot convert 'int' to 'D' in initialization" "" { target c++14_down } }
94 D d4 { l }; // { dg-error "cannot convert 'long int' to 'D' in initialization" "" { target c++14_down } }
95 D d5 { D(l) };
96 D d6 { G }; // { dg-error "cannot convert 'A' to 'D' in initialization" "" { target c++14_down } }
97 E e1 { 5 }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
98 E e2 { -1 }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
99 // { dg-error "narrowing conversion of '-1' from 'int' to 'unsigned char' inside" "" { target c++17 } .-1 }
100 E e3 { 5.0 }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
101 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
102 E e4 { 5.2 }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
103 // { dg-error "narrowing conversion of '5.\[0-9]*e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
104 B b2 = { 7 }; // { dg-error "invalid conversion from 'int' to 'B'" }
105 C c2 = { C { 8 } }; // { dg-error "cannot convert 'int' to 'C' in initialization" "" { target c++14_down } }
106 D *d7 = new D { 9 }; // { dg-error "cannot convert \[^\n\r]* to 'D' in initialization" "" { target c++14_down } }
107 E *e5 = new E { -4 }; // { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
108 // { dg-error "narrowing conversion of '-4' from 'int' to 'unsigned char' inside" "" { target c++17 } .-1 }
109 bar ({ 10 }); // { dg-error "cannot convert \[^\n\r]* to 'E'" }
110 bar (E { 9 }); // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
111 V v1 = { { 11 } }; // { dg-error "braces around scalar initializer for type 'E'" }
112 V v2 = { E { 12 } }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
113 V v3 = { E { 5.0 } }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
114 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
115 V v4 = { 13 }; // { dg-error "cannot convert 'int' to 'E' in initialization" }
116 if (B b3 { 5 }) // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
117 ;
118 if (B b4 { 4.0 }) // { dg-error "cannot convert 'double' to 'B' in initialization" "" { target c++14_down } }
119 ; // { dg-error "narrowing conversion of '4.0e.0' from 'double' to 'short int' inside" "" { target c++17 } .-1 }
120 C c3 { 8L }; // { dg-error "cannot convert 'long int' to 'C' in initialization" "" { target c++14_down } }
121 B b4 {short (c + 5)}; // { dg-error "invalid conversion from 'short int' to 'B'" "" { target c++14_down } }
122 B b5 {c + 5}; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
123 // { dg-error "narrowing conversion of \[^\n\r]* from 'int' to 'short int' inside" "" { target c++17 } .-1 }
124 C c4 { ll }; // { dg-error "cannot convert 'long long int' to 'C' in initialization" "" { target c++14_down } }
125 // { dg-error "narrowing conversion of 'll' from 'long long int' to 'int' inside" "" { target c++17 } .-1 }
126 C c5 {short (c + 5)}; // { dg-error "cannot convert 'short int' to 'C' in initialization" "" { target c++14_down } }
127 C c6 {c + 5}; // { dg-error "cannot convert 'int' to 'C' in initialization" "" { target c++14_down } }
128 }
129
130 template <int N>
131 struct U2
132 {
U2U2133 U2 () : e { 5 } {} // { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
U2U2134 U2 (int) : e { 5.0 } {}// { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
135 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
U2U2136 U2 (float) : e({ 6 }) {}
137 E e;
138 };
139
140 template <int N>
141 struct W2
142 {
143 A a { 5 }; // { dg-error "invalid conversion from 'int' to 'A'" "" { target *-*-* } }
144 B b { 6 }; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
145 C c { 3.0f }; // { dg-error "cannot convert \[^\n\r]* to 'C' in initialization" "" { target c++14_down } }
146 // { dg-error "narrowing conversion of '3.0e.0f' from 'float' to 'int' inside" "" { target c++17 } .-1 }
147 D d = { 7 }; // { dg-error "cannot convert \[^\n\r]* to 'D' in initialization" "" { target *-*-* } }
148 };
149
150 template <typename H, typename I, typename J, typename K, typename L, typename M>
151 void
foo3()152 foo3 ()
153 {
154 void bar3 (L);
155 H a1 { 5 }; // { dg-error "invalid conversion from 'int' to 'A'" }
156 I b1 { 7 }; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
157 J c1 { s };
158 K d1 { K(t) }; // { dg-error "invalid cast from type 'T' to type 'D'" }
159 K d2 { t }; // { dg-error "cannot convert 'T' to 'D' in initialization" "" { target c++14_down } }
160 // { dg-error "invalid cast from type 'T' to type 'D'" "" { target c++17 } .-1 }
161 K d3 { 9 }; // { dg-error "cannot convert 'int' to 'D' in initialization" "" { target c++14_down } }
162 K d4 { l }; // { dg-error "cannot convert 'long int' to 'D' in initialization" "" { target c++14_down } }
163 K d5 { K(l) };
164 K d6 { G }; // { dg-error "cannot convert 'A' to 'D' in initialization" "" { target c++14_down } }
165 L e1 { 5 }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
166 L e2 { -1 }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
167 // { dg-error "narrowing conversion of '-1' from 'int' to 'unsigned char' inside" "" { target c++17 } .-1 }
168 L e3 { 5.0 }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
169 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
170 L e4 { 5.2 }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
171 // { dg-error "narrowing conversion of '5.\[0-9]*e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
172 I b2 = { 7 }; // { dg-error "invalid conversion from 'int' to 'B'" }
173 J c2 = { J { 8 } }; // { dg-error "cannot convert 'int' to 'C' in initialization" "" { target c++14_down } }
174 K *d7 = new K { 9 }; // { dg-error "cannot convert \[^\n\r]* to 'D' in initialization" "" { target c++14_down } }
175 L *e5 = new L { -4 }; // { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
176 // { dg-error "narrowing conversion of '-4' from 'int' to 'unsigned char' inside" "" { target c++17 } .-1 }
177 bar3 ({ 10 }); // { dg-error "cannot convert \[^\n\r]* to 'E'" }
178 bar3 (E { 9 }); // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
179 M v1 = { { 11 } }; // { dg-error "braces around scalar initializer for type 'E'" }
180 M v2 = { L { 12 } }; // { dg-error "cannot convert 'int' to 'E' in initialization" "" { target c++14_down } }
181 M v3 = { L { 5.0 } }; // { dg-error "cannot convert 'double' to 'E' in initialization" "" { target c++14_down } }
182 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
183 M v4 = { 13 }; // { dg-error "cannot convert 'int' to 'E' in initialization" }
184 if (I b3 { 5 }) // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
185 ;
186 if (I b4 { 4.0 }) // { dg-error "cannot convert 'double' to 'B' in initialization" "" { target c++14_down } }
187 ; // { dg-error "narrowing conversion of '4.0e.0' from 'double' to 'short int' inside" "" { target c++17 } .-1 }
188 J c3 { 8L }; // { dg-error "cannot convert 'long int' to 'C' in initialization" "" { target c++14_down } }
189 I b4 {short (c + 5)}; // { dg-error "invalid conversion from 'short int' to 'B'" "" { target c++14_down } }
190 I b5 {c + 5}; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
191 // { dg-error "narrowing conversion of \[^\n\r]* from 'int' to 'short int' inside" "" { target c++17 } .-1 }
192 J c4 { ll }; // { dg-error "cannot convert 'long long int' to 'C' in initialization" "" { target c++14_down } }
193 // { dg-error "narrowing conversion of 'll' from 'long long int' to 'int' inside" "" { target c++17 } .-1 }
194 J c5 {short (c + 5)}; // { dg-error "cannot convert 'short int' to 'C' in initialization" "" { target c++14_down } }
195 J c6 {c + 5}; // { dg-error "cannot convert 'int' to 'C' in initialization" "" { target c++14_down } }
196 }
197
198 template <typename L>
199 struct U3
200 {
U3U3201 U3 () : e { 5 } {} // { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
U3U3202 U3 (int) : e { 5.0 } {}// { dg-error "cannot convert \[^\n\r]* to 'E' in initialization" "" { target c++14_down } }
203 // { dg-error "narrowing conversion of '5.0e.0' from 'double' to 'unsigned char' inside" "" { target c++17 } .-1 }
U3U3204 U3 (float) : e({ 6 }) {}
205 L e;
206 };
207
208 template <typename H, typename I, typename J, typename K>
209 struct W3
210 {
211 H a { 5 }; // { dg-error "invalid conversion from 'int' to 'A'" "" { target *-*-* } }
212 I b { 6 }; // { dg-error "invalid conversion from 'int' to 'B'" "" { target c++14_down } }
213 J c { 3.0f }; // { dg-error "cannot convert \[^\n\r]* to 'C' in initialization" "" { target c++14_down } }
214 // { dg-error "narrowing conversion of '3.0e.0f' from 'float' to 'int' inside" "" { target c++17 } .-1 }
215 K d = { 7 }; // { dg-error "cannot convert \[^\n\r]* to 'D' in initialization" "" { target *-*-* } }
216 };
217
218 void
test()219 test ()
220 {
221 foo2<0> ();
222 U2<0> u20;
223 U2<1> u21 (5);
224 W2<0> w2; // { dg-message "" }
225 foo3<A, B, C, D, E, V> ();
226 U3<E> u30;
227 U3<E> u31 (5);
228 W3<A, B, C, D> w3; // { dg-message "" }
229 }
230