Skip to content

Commit 95db2ad

Browse files
authored
Merge pull request #2321 from WalterBright/struct-init
rewrite struct initialization text
2 parents 4f9ac81 + a38fdfe commit 95db2ad

File tree

1 file changed

+67
-41
lines changed

1 file changed

+67
-41
lines changed

spec/struct.dd

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -132,68 +132,94 @@ S s; // error, cannot initialize unknown contents
132132
S* p; // ok, knowledge of members is not necessary
133133
---
134134

135-
$(P They can be used to implement the
135+
$(BEST_PRACTICE They can be used to implement the
136136
$(LINK2 https://en.wikipedia.org/wiki/Opaque_pointer, PIMPL idiom).)
137137

138138

139-
$(H2 $(LNAME2 static_struct_init, Static Initialization of Structs))
139+
$(H2 $(LNAME2 default_struct_init, Default Initialization of Structs))
140140

141-
$(P Static struct members are by default initialized to whatever the
142-
default initializer for the member is, and if none is supplied, to
143-
the default initializer for the member's type.
141+
$(P Struct fields are by default initialized to whatever the
142+
$(GLINK2 declaration, Initializer) for the field is, and if none is supplied, to
143+
the default initializer for the field's type.
144144
)
145145

146-
------
147-
struct S { int a = 4; int b; }
148-
static S x; // a is set to 4, b to 0
149-
------
146+
---
147+
struct S { int a = 4; int b; }
148+
S x; // x.a is set to 4, x.b to 0
149+
---
150150

151-
$(P If a static initializer is supplied, the
152-
members are initialized by the `memberName:expression` syntax.
153-
The members may be initialized in any order.
154-
Initializers for statics must be evaluatable at
155-
compile time. Members not specified in the initializer list are default
156-
initialized.
151+
$(P The default initializers are evaluated at compile time.)
152+
153+
$(H2 $(LNAME2 static_struct_init, Static Initialization of Structs))
154+
155+
$(P If a $(GLINK2 declaration, StructInitializer) is supplied, the
156+
fields are initialized by the $(GLINK2 declaration, StructMemberInitializer) syntax.
157+
$(I StructMemberInitializers) with the $(I Identifier : NonVoidInitializer) syntax
158+
may be appear in any order, where $(I Identifier) is the field identifer.
159+
$(I StructMemberInitializer)s with the $(GLINK2 declaration, NonVoidInitializer) syntax
160+
appear in the lexical order of the fields in the $(GLINK StructDeclaration).
157161
)
158162

159-
------
160-
struct S { int a, b, c, d = 7; }
161-
static S x = { a:1, b:2 }; // c is set to 0, d to 7
162-
static S z = { c:4, b:5, a:2, d:5 }; // z.a = 2, z.b = 5, z.c = 4, z.d = 5
163-
------
163+
$(P Fields not specified in the $(I StructInitializer) are default initialized.)
164164

165-
$(P C-style initialization, based on the order of the members in the struct
166-
declaration, is also supported:)
165+
---
166+
struct S { int a, b, c, d = 7; }
167+
S r; // r.a = 0, r.b = 0, r.c = 0, r.d = 7
168+
S s = { a:1, b:2 }; // s.a = 1, s.b = 2, s.c = 0, s.d = 7
169+
S t = { c:4, b:5, a:2, d:5 }; // t.a = 2, t.b = 5, t.c = 4, t.d = 5
170+
S u = { 1, 2 }; // u.a = 1, u.b = 2, u.c = 0, u.d = 7
171+
S v = { 1, d:3 }; // v.a = 1, v.b = 0, v.c = 0, v.d = 3
172+
S w = { b:1, 3 }; // v.a = 0, v.b = 1, v.c = 3, v.d = 7
173+
---
167174

168-
------
169-
static S q = { 1, 2 }; // q.a = 1, q.b = 2, q.c = 0, q.d = 7
170-
------
175+
$(P Initializing a field more than once is an error:)
171176

172-
$(P The two styles can be combined:)
177+
---
178+
S x = { 1, a:2 }; // error: duplicate initializer for field `a`
179+
---
173180

174-
------
175-
static S q = { 1, d:3 }; // q.a = 1, q.b = 0, q.c = 0, q.d = 3
176-
------
181+
$(H2 $(LNAME2 default_union_init, Default Initialization of Unions))
182+
183+
$(P Unions are by default initialized to whatever the
184+
$(GLINK2 declaration, Initializer) for the first field is, and if none is supplied, to
185+
the default initializer for the first field's type.
186+
)
187+
188+
$(P If the union is larger than the first field, the remaining bits
189+
are set to 0.)
190+
191+
---
192+
union U { int a = 4; long b; }
193+
U x; // x.a is set to 4, x.b to an implementation-defined value
194+
195+
union V { int a; long b = 4; }
196+
U y; // y.a is set to 0, y.b to an implementation-defined value
177197

178-
$(P Struct literals can also be used to initialize statics, but
179-
they must be evaluable at compile time.)
198+
union W { int a = 4; long b = 5; } // error: overlapping default initialization for `a` and `b`
199+
---
180200

181-
-----
182-
static S q = S( 1, 2+3 ); // q.a = 1, q.b = 5, q.c = 0, q.d = 7
183-
-----
201+
$(P The default initializer is evaluated at compile time.)
184202

203+
$(IMPLEMENTATION_DEFINED The values the fields other than the
204+
default initialized field are set to.)
185205

186206
$(H2 $(LNAME2 static_union_init, Static Initialization of Unions))
187207

188-
$(P Unions are initialized explicitly.)
208+
$(P Unions are initialized similarly to structs, except that only
209+
one initializer is allowed.)
189210

190-
------
191-
union U { int a; double b; }
192-
static U u = { b : 5.0 }; // u.b = 5.0
193-
------
211+
---
212+
union U { int a; double b; }
213+
U u = { 2 }; // u.a = 2
214+
U v = { b : 5.0 }; // v.b = 5.0
215+
U w = { 2, 3 }; // error: overlapping initialization for field `a` and `b`
216+
---
217+
218+
$(P If the union is larger than the initialized field, the remaining bits
219+
are set to 0.)
194220

195-
$(P Other members of the union that overlay the initializer, but occupy more
196-
storage, have the extra storage initialized to zero.)
221+
$(IMPLEMENTATION_DEFINED The values the fields other than the
222+
initialized field are set to.)
197223

198224
$(H2 $(LNAME2 dynamic_struct_init, Dynamic Initialization of Structs))
199225

0 commit comments

Comments
 (0)