@@ -7,8 +7,8 @@ $(HEADERNAV_TOC)
7
7
$(H2 $(LNAME2 intro, Introduction))
8
8
9
9
$(P Whereas $(DDLINK spec/class, Classes, classes) are reference types,
10
- structs are value types.
11
- Structs and unions are simple aggregations of data and their
10
+ structs and unions are value types.
11
+ Structs are simple aggregations of data and their
12
12
associated operations on that data.
13
13
)
14
14
@@ -21,7 +21,8 @@ $(GNAME StructDeclaration):
21
21
22
22
$(GNAME AnonStructDeclaration):
23
23
$(D struct) $(GLINK AggregateBody)
24
-
24
+ )
25
+ $(GRAMMAR
25
26
$(GNAME UnionDeclaration):
26
27
$(D union) $(GLINK_LEX Identifier) $(D ;)
27
28
$(D union) $(GLINK_LEX Identifier) $(GLINK AggregateBody)
@@ -30,7 +31,8 @@ $(GNAME UnionDeclaration):
30
31
31
32
$(GNAME AnonUnionDeclaration):
32
33
$(D union) $(GLINK AggregateBody)
33
-
34
+ )
35
+ $(GRAMMAR
34
36
$(GNAME AggregateBody):
35
37
$(D {) $(GLINK2 module, DeclDefs)$(OPT) $(D })
36
38
)
@@ -44,7 +46,8 @@ struct S
44
46
int i;
45
47
}
46
48
47
- void main() {
49
+ void main()
50
+ {
48
51
S a;
49
52
a.i = 3;
50
53
@@ -60,12 +63,40 @@ void main() {
60
63
by default. To allocate on the heap, use `new`:)
61
64
---
62
65
S* p = new S;
66
+ assert(p.i == 0);
63
67
---
64
68
69
+ $(P A struct can contain multiple fields which are stored sequentially.
70
+ Conversely, multiple fields in a union use overlapping storage.)
71
+
72
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
73
+ ---
74
+ union U
75
+ {
76
+ ubyte i;
77
+ char c;
78
+ }
79
+
80
+ void main()
81
+ {
82
+ U u;
83
+ u.i = 3;
84
+ assert(u.c == '\x03');
85
+ u.c++;
86
+ assert(u.i == 4);
87
+ }
88
+ ---
89
+ )
90
+
91
+ $(H2 $(LNAME2 members, Members))
92
+
93
+ $(H3 $(LNAME2 struct-members, Struct Members))
94
+
65
95
$(P A struct definition can contain:)
66
96
$(UL
67
- $(LI fields)
68
- $(LI $(DDSUBLINK spec/attribute, static, static) fields)
97
+ $(LI Fields)
98
+ $(LI $(DDSUBLINK spec/attribute, static, Static) fields)
99
+ $(LI $(RELATIVE_LINK2 anonymous, Anonymous Structs and Unions))
69
100
$(LI $(DDSUBLINK spec/class, member-functions, member functions)
70
101
$(UL
71
102
$(LI static member functions)
@@ -75,24 +106,59 @@ void main() {
75
106
$(LI $(DDLINK spec/operatoroverloading, Operator Overloading, Operator Overloading))
76
107
)
77
108
$(LI $(DDSUBLINK spec/class, alias-this, Alias This))
78
- $(LI other declarations (see $(GLINK2 module, DeclDef)))
109
+ $(LI Other declarations (see $(GLINK2 module, DeclDef)))
79
110
)
80
111
)
81
112
82
113
$(P A struct is defined to not have an identity; that is,
83
114
the implementation is free to make bit copies of the struct
84
115
as convenient.)
85
116
86
- $(P Structs and unions may not contain an instance of themselves,
87
- however, they may contain a pointer to the same type.
88
- )
89
-
90
117
$(BEST_PRACTICE
91
118
$(OL
92
119
$(LI Bit fields are supported with the
93
120
$(LINK2 https://dlang.org/phobos/std_bitmanip.html#bitfields, bitfields) template.)
94
121
))
95
122
123
+ $(H3 $(LNAME2 union-members, Union Members))
124
+
125
+ $(P A union definition can contain:)
126
+ $(UL
127
+ $(LI Fields)
128
+ $(LI $(DDSUBLINK spec/attribute, static, Static) fields)
129
+ $(LI $(RELATIVE_LINK2 anonymous, Anonymous Structs and Unions))
130
+ $(LI $(DDSUBLINK spec/class, member-functions, member functions)
131
+ $(UL
132
+ $(LI static member functions)
133
+ $(LI $(RELATIVE_LINK2 UnionConstructor, Constructors))
134
+ $(LI $(DDLINK spec/operatoroverloading, Operator Overloading, Operator Overloading))
135
+ )
136
+ $(LI $(DDSUBLINK spec/class, alias-this, Alias This))
137
+ $(LI Other declarations (see $(GLINK2 module, DeclDef)))
138
+ )
139
+ )
140
+
141
+ $(H3 $(LNAME2 recursive-types, Recursive Structs and Unions))
142
+
143
+ $(P Structs and unions may not contain a non-static instance of themselves,
144
+ however, they may contain a pointer to the same type.
145
+ )
146
+
147
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
148
+ ---
149
+ struct S
150
+ {
151
+ S* ptr; // OK
152
+ S[] slice; // OK
153
+
154
+ S s; // error
155
+ S[2] array; // error
156
+
157
+ static S global; // OK
158
+ }
159
+ ---
160
+ )
161
+
96
162
97
163
$(H2 $(LNAME2 struct_layout, Struct Layout))
98
164
0 commit comments