second draft: add Bitfields to D

Walter Bright newshound2 at digitalmars.com
Tue Apr 30 03:41:46 UTC 2024


On 4/29/2024 8:54 AM, Jonathan M Davis wrote:
> Such code does already need to take unions into account (and there is _some_
> similarity between those and bitfields), but it's going to have done that by
> checking things like is(T == union), which won't help with bitfields at all.

Using `is(T==union)` is incomplete because anonymous unions are not fields. The 
compiler doesn't do a union check internally for that reason. The correct check 
would be:

```
if (S.a.offsetof + typeof(S.a).sizeof <= S.b.offsetof ||
     S.b.offsetof + typeof(S.b).sizeof <= S.a.offsetof)
{
     // S.a and S.b do not overlap
}
else
{
     // S.a and S.b overlap
}
```
This will work without change for bitfields.



More information about the dip.development mailing list