second draft: add Bitfields to D

Walter Bright newshound2 at digitalmars.com
Sat May 4 20:17:32 UTC 2024


On 5/4/2024 10:05 AM, Timon Gehr wrote:
> This is explicitly called a `union`, so I really do not see what you are trying 
> to say.

An anonymous union is simply a way to specify layout. No actual union is 
created, as there is no point to it. How would one refer to an anonymous union? 
The same goes for anonymous structs.


> Clearly bitfields are a new and distinct way fields can share the same 
> `.offsetof` in D. Before bitfields, such fields would overlap given they both 
> were of a type with a positive size. With bitfields there is no such overlap.

Bitfields do overlap, which is why they are accessed with shift and mask.

Besides, the context here is with existing introspection. Existing introspection 
will treat them as overlapping fields.


> BTW: what about `sizeof`? I think in C++ this is disallowed on a bitfield.

```
struct S { int b:3; }
pragma(msg, S.b.sizeof);
```
prints 4LU, as it applies to the type. To get the field width, use .max, as 
already discussed.


More information about the dip.development mailing list