@@ -99,6 +99,56 @@ $(H2 $(LNAME2 struct_layout, Struct Layout))
99
99
$(LI Avoid using empty structs as parameters or arguments to variadic functions.)
100
100
))
101
101
102
+ $(H2 $(LNAME2 bitfields, Bit Field Declarations))
103
+
104
+ $(P A bit field is declared with a $(GLINK2 declaration, BitfieldDeclarator). The bit field width
105
+ is specified by the $(GLINK2 expression, AssignExpression). It is evaluated at compile time, and
106
+ must result in an integer expression that ranges from 0 to the number of bits in type of the
107
+ bit field.)
108
+
109
+ $(P Anonymous bit fields do not have a $(GLINK_LEX Identifier) for them.)
110
+
111
+ $(P A zero width bit field must be anonymous. A zero width bit field causes the next bit field
112
+ to be placed into the next unit.)
113
+
114
+ $(P The type of the bit field must be an integral type, signed or unsigned. The values assigned to
115
+ bit fields must fit into the specified width.)
116
+
117
+ $(P The address of a bit field cannot be taken.
118
+ $(D ref) declarations cannot be initialized from bit fields.
119
+ Arrays of bit fields are not allowed.
120
+ Bit fields can only be fields in structs, unions and classes.
121
+ Bit fields must be non-static.
122
+ )
123
+
124
+ ---
125
+ struct B
126
+ {
127
+ int x:3, y:2;
128
+ }
129
+
130
+ static assert(B.sizeof == 4);
131
+
132
+ int vaporator(B b)
133
+ {
134
+ b.x = 4;
135
+ b.y = 2;
136
+ return b.x + b.y; // returns 6
137
+ }
138
+ ---
139
+
140
+ $(IMPLEMENTATION_DEFINED Reading and writing a bit field may cause reads and writes of other bit
141
+ fields in the same unit. Any use of bit fields where asynchronous access is possible will produce
142
+ unreliable results.)
143
+
144
+ $(IMPLEMENTATION_DEFINED the layout of the bit fields is implementation defined. In practice, it is
145
+ expected to be identical to that of ImportC.)
146
+
147
+ $(BEST_PRACTICE
148
+ Because the layout of the bit fields is implementation defined, using them to match an externally
149
+ defined layout is not a good idea. Use shifts and masks instead for such portability.
150
+ )
151
+
102
152
$(H2 $(LNAME2 POD, Plain Old Data))
103
153
104
154
$(P A struct or union is $(I Plain Old Data) (POD) if it meets the following criteria:)
0 commit comments