second draft: add Bitfields to D

Walter Bright newshound2 at digitalmars.com
Sat May 4 02:07:22 UTC 2024


On 4/30/2024 6:43 AM, Timon Gehr wrote:
> No, more than one bitfield is valid at a time even if they have the same 
> offsetof. This is definitely breaking expectations that used to be true.

Any expectations that there would be no two fields with the same offset were 
incorrect anyway, as that is what happens with anonymous unions.


>> std.bitmanip.bitfields also implements it as a union,
> No, this is not correct. It implements it as a field with accessors for 
> different groups of bits. The only reason why `union` appears in that file is to 
> support bitfields inside a union. This again highlights that those are not the 
> same thing.

They are the same thing, it is not substantive what label is painted on it. 
Anonymous unions can lay fields on top of each other without explicitly labeling 
it as a union.


> I understand the underlying reality of all of those concepts and I still 
> disagree that interpreting bitfields as a union is correct. There are bitfields 
> and there are unions.

There is no value to that distinction. As I replied to Jonathan in this thread, 
D can have fields laying over the top of each other right now without any unions 
or bitfields declared.


> But it will include bitfields, and not the underlying "physical" variables.

If the introspection code does not take into account anonymous unions, it's 
already broken anyway.

```
struct S
{
     union
     {
         int a;
         int b;
         int c:32;
     }
}
```




More information about the dip.development mailing list