Skip to content

Commit 876f560

Browse files
ntrelthewilsonator
authored andcommitted
[spec] Tweak docs for field offsets
Make example runnable. Use IMPLEMENTATION_DEFINED macro for class field offsets and link to this from `.offsetof` section. Add links to `.offsetof` example.
1 parent c75187d commit 876f560

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

spec/class.dd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ $(H2 $(LNAME2 fields, Fields))
129129
$(P Members of a base class can be accessed by prepending the name of
130130
the base class followed by a dot:)
131131

132+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
132133
---
133134
class A { int a; int a2;}
134135
class B : A { int a; }
@@ -140,9 +141,11 @@ void foo(B b)
140141
b.A.a = 5; // accesses field A.a
141142
}
142143
---
144+
)
143145

144-
$(P The D compiler is free to rearrange the order of fields in a class to
145-
optimally pack them in an implementation-defined manner.
146+
$(IMPLEMENTATION_DEFINED
147+
The D compiler is free to rearrange the order of fields in a class to
148+
optimally pack them.
146149
Consider the fields much like the local
147150
variables in a function -
148151
the compiler assigns some to registers and shuffles others around all to
@@ -234,6 +237,9 @@ $(H3 $(LNAME2 field_properties, Field Properties))
234237

235238
$(P The $(D .offsetof) property gives the offset in bytes of the field
236239
from the beginning of the class instantiation.
240+
Note that the compiler can $(RELATIVE_LINK2 fields, rearrange class field offsets).
241+
See $(DDSUBLINK spec/attribute, align, the `align` attribute) for a struct-based
242+
example.
237243
)
238244
$(NOTE `.offsetof` is not available for fields of `extern(Objective-C)` classes
239245
due to their fields having a dynamic offset.

spec/struct.dd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,9 @@ $(H3 $(LNAME2 struct_field_properties, Struct Field Properties))
631631

632632
$(TABLE2 Struct Field Properties,
633633
$(THEAD Name, Description)
634-
$(TROW $(D .offsetof), Offset in bytes of field from beginning of struct)
634+
$(TROW $(D .offsetof), Offset in bytes of field from beginning of struct.
635+
See $(DDSUBLINK spec/attribute, align, the `align` attribute) for an example.
636+
)
635637
)
636638

637639
$(H2 $(LEGACY_LNAME2 ConstStruct, const-struct, Const, Immutable and Shared Structs))

0 commit comments

Comments
 (0)