DIP: add Bit Fields

Timon Gehr timon.gehr at gmx.ch
Wed May 1 15:46:44 UTC 2024


On 4/22/24 18:16, Walter Bright wrote:
> 
> In the very rare case of actually needing an exact layout, with 5 minutes of effort any layout can be duplicated with C bit fields.

Well, if someone is not aware that a gun can shoot one's own foot, they 
might do it even if they very well know it is not good to hurt one's foot.

> If that's too much effort, one can write explicit shift/mask code, and encapsulate it into a function. (Once the function is inlined, there is zero extra overhead for it.) Or one can use std.bitmanip to do that automatically. 

I suppose you could match C bitfields with a string mixin too. Or the 
compiler could automatically generate a portable specification 
corresponding to the C bitfield layout.

> The preceding is true if one wants a specific layout.
> 
> But to algorithmically match what the associated C compiler does, that 
> is more of a significant problem. It took me a while (with the help of 
> Iain) to reverse-engineer it. This is a problem worth solving, and our 
> implementation does solve it. For the user, it effortlessly resolves the 
> issue of binary interoperability with C code.

Yes, I agree with this. However, there is a difference between having 
this feature and making it the _default representation_ of bit fields.

It's just about how to best expose that feature for the best overall 
ergonomics, including introspection.


More information about the dip.development mailing list