@@ -62,7 +62,9 @@ $(H2 $(LNAME2 struct_layout, Struct Layout))
62
62
the first field and the start of the object.
63
63
)
64
64
65
- $(P Structs with no fields of non-zero size (aka $(I Empty Structs)) have a size of one byte.)
65
+ $(P A struct with no fields of non-zero size is an $(I Empty Struct).
66
+ An Empty Struct with a $(DDSUBLINK spec/attribute, linkage, Linkage Attribute) of `extern (C)` has a size of zero.
67
+ Otherwise, its size is one byte.)
66
68
67
69
$(P Non-static $(RELATIVE_LINK2 nested, function-nested D structs), which access the context of
68
70
their enclosing scope, have an extra field.
@@ -72,20 +74,22 @@ $(H2 $(LNAME2 struct_layout, Struct Layout))
72
74
$(OL
73
75
$(LI The default layout of the fields of a struct is an exact
74
76
match with the $(I associated C compiler).)
75
- $(LI g++ and clang++ differ in how empty structs are handled. Both return `1` from `sizeof`,
77
+ $(LI g++ and clang++ differ in how Empty Structs are handled. Both return `1` from `sizeof`,
76
78
however, clang++ does not push them onto the parameter stack while g++ does. This is a
77
79
binary incompatibility between g++ and clang++.
78
80
dmd follows clang++ behavior for OSX and FreeBSD, and g++ behavior for Linux and other
79
81
Posix platforms.
80
82
)
81
- $(LI clang and gcc both return `0` from `sizeof` for empty structs. Using `extern "C++"`
82
- in clang++ and g++ does not cause them to conform to the behavior of their respective C compilers.)
83
+ $(LI C compilers normally evaluate `sizeof` applied to Empty Structs as `0`, although it
84
+ is undefined behavior according to the C11 Standard.
85
+ C++ compilers normally evaluate the size as `1`, even if `extern "C"` is applied.
86
+ )
83
87
))
84
88
85
89
$(UNDEFINED_BEHAVIOR
86
90
$(OL
87
91
$(LI The padding data can be accessed, but its contents are undefined.)
88
- $(LI Do not pass or return structs with no fields of non-zero size to `extern (C)` functions.
92
+ $(LI Do not pass or return Empty Structs to `extern (C)` functions.
89
93
According to C11 6.7.2.1p8 this is undefined behavior.)
90
94
))
91
95
@@ -95,8 +99,9 @@ $(H2 $(LNAME2 struct_layout, Struct Layout))
95
99
attributes to describe an exact match. Using a $(DDSUBLINK spec/version, static-assert, Static Assert)
96
100
to ensure the result is as expected.)
97
101
$(LI Although the contents of the padding are often zero, do not rely on that.)
98
- $(LI Avoid using empty structs when interfacing with C and C++ code.)
99
- $(LI Avoid using empty structs as parameters or arguments to variadic functions.)
102
+ $(LI Avoid using Empty Structs when interfacing with C++ code that declares them with `extern "C"`.
103
+ Add a one byte dummy field to those structs.)
104
+ $(LI Avoid using Empty Structs as parameters or arguments to variadic functions.)
100
105
))
101
106
102
107
$(H2 $(LNAME2 POD, Plain Old Data))
0 commit comments