Skip to content

Commit 7401104

Browse files
authored
Merge pull request #2143 from WalterBright/struct-spec
struct.dd: improve wording
2 parents 0e37685 + d66eff1 commit 7401104

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

spec/struct.dd

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,10 @@ $(SPEC_S Structs and Unions,
44

55
$(HEADERNAV_TOC)
66

7-
$(P Whereas classes are reference types, structs are value types.
8-
Any C struct can be exactly represented as a D struct, except non-static
9-
$(RELATIVE_LINK2 nested, function-nested D structs) which access the context of
10-
their enclosing scope.
11-
Structs and unions are simple aggregations of data, or as a way
12-
to paint a data structure over hardware or an external type. External
13-
types can be defined by the operating system API, or by a file format.
14-
Object oriented features are provided with the class data type.
15-
)
16-
17-
$(P A struct is defined to not have an identity; that is,
18-
the implementation is free to make bit copies of the struct
19-
as convenient.)
7+
$(P Whereas classes are reference types, structs are value types.
8+
Structs and unions are simple aggregations of data and their
9+
associated operations on that data.
10+
)
2011

2112
$(GRAMMAR
2213
$(GNAME AggregateDeclaration):
@@ -47,27 +38,64 @@ $(GNAME AggregateBody):
4738
$(D {) $(GLINK2 module, DeclDefs)$(OPT) $(D })
4839
)
4940

50-
$(P They work like they do in C, with the following exceptions:)
41+
$(P A struct is defined to not have an identity; that is,
42+
the implementation is free to make bit copies of the struct
43+
as convenient.)
5144

52-
$(UL
53-
$(LI no bit fields)
54-
$(LI alignment can be explicitly specified)
55-
$(LI no separate tag name space - tag names go into the current scope)
56-
$(LI declarations like:
45+
$(P Structs and unions may not contain an instance of themselves,
46+
however, they may contain a pointer to the same type.
47+
)
5748

58-
------
59-
struct ABC x;
60-
------
61-
are not allowed, replace with:
49+
$(BEST_PRACTICE
50+
$(OL
51+
$(LI Bit fields are supported with the
52+
$(LINK2 https://dlang.org/phobos/std_bitmanip.html#bitfields, bitfields) template.)
53+
))
6254

63-
------
64-
ABC x;
65-
------
66-
)
67-
$(LI anonymous structs/unions are allowed as members of other structs/unions)
68-
$(LI Default initializers for members can be supplied.)
69-
$(LI Member functions and static members are allowed.)
70-
)
55+
56+
$(H2 $(LNAME2 struct_layout, Struct Layout))
57+
58+
$(P The non-static data members of a struct are called $(I fields). Fields are laid
59+
out in lexical order. Fields are aligned according to the $(DDSUBLINK spec/attribute, align, Align Attribute)
60+
in effect.
61+
Unnamed padding is inserted between fields to align fields. There is no padding between
62+
the first field and the start of the object.
63+
)
64+
65+
$(P Non-static $(RELATIVE_LINK2 nested, function-nested D structs), which access the context of
66+
their enclosing scope, have an extra field.
67+
)
68+
69+
$(IMPLEMENTATION_DEFINED
70+
$(OL
71+
$(LI The default layout of the fields of a struct is an exact
72+
match with the $(I associated C compiler).)
73+
))
74+
75+
$(UNDEFINED_BEHAVIOR
76+
$(OL
77+
$(LI The padding data can be accessed, but its contents are undefined.)
78+
))
79+
80+
$(BEST_PRACTICE
81+
$(OL
82+
$(LI When laying out a struct to match an externally defined layout, use align
83+
attributes to describe an exact match. Using a $(DDSUBLINK spec/version, static-assert, Static Assert)
84+
to ensure the result is as expected.)
85+
$(LI Although the contents of the padding are often zero, do not rely on that.)
86+
))
87+
88+
$(H2 $(LNAME2 POD, Plain Old Data))
89+
90+
$(P A struct or union is $(I Plain Old Data) (POD) if it meets the following criteria:)
91+
92+
$(OL
93+
$(LI it is not nested)
94+
$(LI it has no postblits, destructors, or assignment operators)
95+
$(LI it has no `ref` fields or fields that are themselves non-POD)
96+
)
97+
98+
$(BEST_PRACTICE Structs or unions that interface with C code should be POD.)
7199

72100

73101
$(H2 $(LNAME2 opaque_struct_unions, Opaque Structs and Unions))

0 commit comments

Comments
 (0)