@@ -206,6 +206,59 @@ $(H2 $(LNAME2 struct_layout, Struct Layout))
206
206
$(LI Avoid using empty structs as parameters or arguments to variadic functions.)
207
207
))
208
208
209
+ $(H2 $(LNAME2 bitfields, Bit Field Declarations))
210
+
211
+ $(P Bit fields are available under the preview flag $(TT -preview=bitfields))
212
+
213
+ $(P A bit field is declared with a $(GLINK2 declaration, BitfieldDeclarator). The bit field width
214
+ is specified by the $(GLINK2 expression, AssignExpression). It is evaluated at compile time, and
215
+ must result in an integer expression that ranges from 0 to the number of bits in the type of the
216
+ bit field.)
217
+
218
+ $(P The type of the bit field must be an integral type, signed or unsigned. The values assigned to
219
+ bit fields must fit into the specified width. The bit field is placed into a unit of memory at least
220
+ as large as needed to hold a value of the type of the bit field.)
221
+
222
+ $(P Anonymous bit fields do not have an $(GLINK_LEX Identifier) for them.)
223
+
224
+ $(P A zero width bit field must be anonymous. A zero width bit field causes the next bit field
225
+ to be placed into the next unit.)
226
+
227
+ $(P The address of a bit field cannot be taken.
228
+ $(D ref) declarations cannot be initialized from bit fields.
229
+ Arrays of bit fields are not allowed.
230
+ Bit fields can only be fields in structs, unions and classes.
231
+ Bit fields must be non-static.
232
+ )
233
+
234
+ ---
235
+ struct B
236
+ {
237
+ int x:3, y:2;
238
+ }
239
+
240
+ static assert(B.sizeof == 4);
241
+
242
+ int vaporator(B b)
243
+ {
244
+ b.x = 4;
245
+ b.y = 2;
246
+ return b.x + b.y; // returns 6
247
+ }
248
+ ---
249
+
250
+ $(IMPLEMENTATION_DEFINED Reading and writing a bit field may cause reads and writes of other bit
251
+ fields in the same unit. Any use of bit fields where asynchronous access is possible will produce
252
+ unreliable results.)
253
+
254
+ $(IMPLEMENTATION_DEFINED the layout of the bit fields is implementation defined. In practice, it is
255
+ expected to be identical to that of ImportC.)
256
+
257
+ $(BEST_PRACTICE
258
+ Because the layout of the bit fields is implementation defined, using them to match an externally
259
+ defined layout is not a good idea. Use shifts and masks instead for such portability.
260
+ )
261
+
209
262
$(H2 $(LNAME2 POD, Plain Old Data))
210
263
211
264
$(P A struct or union is $(I Plain Old Data) (POD) if it meets the following criteria:)
0 commit comments