@@ -114,9 +114,10 @@ $(H3 $(LNAME2 struct-members, Struct Members))
114
114
the implementation is free to make bit copies of the struct
115
115
as convenient.)
116
116
117
- $(BEST_PRACTICE
117
+ $(NOTE
118
118
$(OL
119
- $(LI Bit fields are supported with the
119
+ $(LI Native bit fields are supported with $(RELATIVE_LINK2 bitfields, Bit Field Declarations))
120
+ $(LI A library implemenation of bit fields are available with the
120
121
$(LINK2 https://dlang.org/phobos/std_bitmanip.html#bitfields, bitfields) template.)
121
122
))
122
123
@@ -206,6 +207,62 @@ $(H2 $(LNAME2 struct_layout, Struct Layout))
206
207
$(LI Avoid using empty structs as parameters or arguments to variadic functions.)
207
208
))
208
209
210
+ $(H2 $(LNAME2 bitfields, Bit Field Declarations))
211
+
212
+ $(P Bit fields are available under the preview flag $(TT -preview=bitfields))
213
+
214
+ $(P A bit field is declared with a $(GLINK2 declaration, BitfieldDeclarator). The bit field width
215
+ is specified by the $(GLINK2 expression, AssignExpression). It is evaluated at compile time, and
216
+ must result in an integer expression that ranges from 0 to the number of bits in the type of the
217
+ bit field.)
218
+
219
+ $(P The type of the bit field must be an integral type, signed or unsigned. The values assigned to
220
+ bit fields must fit into the specified width. The bit field is placed into a unit of memory at least
221
+ as large as needed to hold a value of the type of the bit field.)
222
+
223
+ $(P Anonymous bit fields do not have an $(GLINK_LEX Identifier) for them.)
224
+
225
+ $(P A zero width bit field must be anonymous. A zero width bit field causes the next bit field
226
+ to be placed into the next unit.)
227
+
228
+ $(P The address of a bit field cannot be taken.
229
+ $(D ref) declarations cannot be initialized from bit fields.
230
+ Arrays of bit fields are not allowed.
231
+ Bit fields can only be fields in structs, unions and classes.
232
+ Bit fields must be non-static.
233
+ )
234
+
235
+ ---
236
+ struct B
237
+ {
238
+ int x:3, y:2;
239
+ }
240
+
241
+ static assert(B.sizeof == 4);
242
+
243
+ int vaporator(B b)
244
+ {
245
+ b.x = 4;
246
+ b.y = 2;
247
+ return b.x + b.y; // returns 6
248
+ }
249
+ ---
250
+
251
+ $(IMPLEMENTATION_DEFINED Reading and writing a bit field may cause reads and writes of other bit
252
+ fields in the same unit. Any use of bit fields where asynchronous access is possible will produce
253
+ unreliable results.)
254
+
255
+ $(IMPLEMENTATION_DEFINED the layout of the bit fields is implementation defined. In practice, it is
256
+ expected to be identical to that of the associated C compiler, and ImportC.)
257
+
258
+ $(BEST_PRACTICE
259
+ If data portability is desired, use
260
+ $(LINK2 https://dlang.org/phobos/std_bitmanip.html#bitfields, std.bitmanip.bitfields).
261
+ If data compatibility with the associated C compiler is desired, use these bit fields.
262
+ Although not guaranteed, sticking with `int` and `uint` bit fields will yield the
263
+ most binary compatibility.
264
+ )
265
+
209
266
$(H2 $(LNAME2 POD, Plain Old Data))
210
267
211
268
$(P A struct or union is $(I Plain Old Data) (POD) if it meets the following criteria:)
0 commit comments