@@ -136,7 +136,9 @@ S* p; // ok, knowledge of members is not necessary
136
136
$(LINK2 https://en.wikipedia.org/wiki/Opaque_pointer, PIMPL idiom).)
137
137
138
138
139
- $(H2 $(LNAME2 default_struct_init, Default Initialization of Structs))
139
+ $(H2 $(LNAME2 initialization, Initialization))
140
+
141
+ $(H3 $(LNAME2 default_struct_init, Default Initialization of Structs))
140
142
141
143
$(P Struct fields are by default initialized to whatever the
142
144
$(GLINK2 declaration, Initializer) for the field is, and if none is supplied, to
@@ -154,10 +156,24 @@ $(H2 $(LNAME2 default_struct_init, Default Initialization of Structs))
154
156
155
157
$(P The default initializers may not contain references to mutable data.)
156
158
157
- $(H2 $(LNAME2 static_struct_init, Static Initialization of Structs))
159
+ $(H3 $(LNAME2 static_struct_init, Static Initialization of Structs))
160
+
161
+ $(GRAMMAR
162
+ $(GNAME StructInitializer):
163
+ $(D {) $(I StructMemberInitializers)$(OPT) $(D })
158
164
159
- $(P If a $(GLINK2 declaration, StructInitializer) is supplied, the
160
- fields are initialized by the $(GLINK2 declaration, StructMemberInitializer) syntax.
165
+ $(GNAME StructMemberInitializers):
166
+ $(I StructMemberInitializer)
167
+ $(I StructMemberInitializer) $(D ,)
168
+ $(I StructMemberInitializer) $(D ,) $(GSELF StructMemberInitializers)
169
+
170
+ $(GNAME StructMemberInitializer):
171
+ $(GLINK2 declaration, NonVoidInitializer)
172
+ $(GLINK_LEX Identifier) $(D :) $(GLINK2 declaration, NonVoidInitializer)
173
+ )
174
+
175
+ $(P If a $(I StructInitializer) is supplied, the
176
+ fields are initialized by the $(I StructMemberInitializer) syntax.
161
177
$(I StructMemberInitializers) with the $(I Identifier : NonVoidInitializer) syntax
162
178
may be appear in any order, where $(I Identifier) is the field identifier.
163
179
$(I StructMemberInitializer)s with the $(GLINK2 declaration, NonVoidInitializer) syntax
@@ -186,14 +202,12 @@ $(H2 $(LNAME2 static_struct_init, Static Initialization of Structs))
186
202
---
187
203
)
188
204
189
- $(H2 $(LNAME2 default_union_init, Default Initialization of Unions))
205
+ $(H3 $(LNAME2 default_union_init, Default Initialization of Unions))
190
206
191
207
$(P Unions are by default initialized to whatever the
192
208
$(GLINK2 declaration, Initializer) for the first field is, and if none is supplied, to
193
209
the default initializer for the first field's type.
194
- )
195
-
196
- $(P If the union is larger than the first field, the remaining bits
210
+ If the union is larger than the first field, the remaining bits
197
211
are set to 0.)
198
212
199
213
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -203,6 +217,8 @@ $(H2 $(LNAME2 default_union_init, Default Initialization of Unions))
203
217
---
204
218
)
205
219
220
+ $(P It is an error to supply initializers for members other than the first one.)
221
+
206
222
$(SPEC_RUNNABLE_EXAMPLE_FAIL
207
223
---
208
224
union V { int a; long b = 4; } // error: union field `b` with default initialization `4` must be before field `a`
@@ -215,10 +231,10 @@ $(H2 $(LNAME2 default_union_init, Default Initialization of Unions))
215
231
$(IMPLEMENTATION_DEFINED The values the fields other than the
216
232
default initialized field are set to.)
217
233
218
- $(H2 $(LNAME2 static_union_init, Static Initialization of Unions))
234
+ $(H3 $(LNAME2 static_union_init, Static Initialization of Unions))
219
235
220
236
$(P Unions are initialized similarly to structs, except that only
221
- one initializer is allowed.)
237
+ one member initializer is allowed.)
222
238
223
239
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
224
240
---
@@ -240,7 +256,7 @@ $(H2 $(LNAME2 static_union_init, Static Initialization of Unions))
240
256
$(IMPLEMENTATION_DEFINED The values the fields other than the
241
257
initialized field are set to.)
242
258
243
- $(H2 $(LNAME2 dynamic_struct_init, Dynamic Initialization of Structs))
259
+ $(H3 $(LNAME2 dynamic_struct_init, Dynamic Initialization of Structs))
244
260
245
261
$(P The $(RELATIVE_LINK2 static_struct_init, static initializer syntax)
246
262
can also be used to initialize non-static variables.
@@ -269,7 +285,7 @@ S s = t; // s.a is set to 3
269
285
----
270
286
)
271
287
272
- $(P If the struct has a $(LINK2 # struct-constructor, constructor), and
288
+ $(P If the struct has a $(RELATIVE_LINK2 struct-constructor, constructor), and
273
289
the struct is initialized with a value that is of a different type,
274
290
then the constructor is called:)
275
291
@@ -289,7 +305,8 @@ S s = 3; // sets s.a to 3 using S's constructor
289
305
----
290
306
)
291
307
292
- $(P If the struct does not have a constructor but $(D opCall) is
308
+ $(P If the struct does not have a constructor but
309
+ $(DDSUBLINK spec/operatoroverloading, FunctionCall, `opCall`) is
293
310
overridden for the struct, and the struct is initialized with a value
294
311
that is of a different type, then the $(D opCall) operator is called:)
295
312
0 commit comments