D as an extension language for C

max haughton maxhaton at gmail.com
Wed May 17 09:53:06 UTC 2023


On Wednesday, 17 May 2023 at 03:52:33 UTC, Walter Bright wrote:
> On 5/16/2023 6:36 AM, Adam D Ruppe wrote:
>> ImportC can do C bitfields, but since they're layout is 
>> undefined it is useless anyway.
>
> Pedantically, the C layout is not undefined, it is 
> implementation defined. D's bitfields, however, are defined to 
> match the layout of the associated C compiler.

This is what makes them a major footgun. Other than being 
underpowered for the complexity they add (I want the compiler to 
help me do discontinuous bitpacking not just easy stuff), you 
can't rely on the layout for basically anything other than 
space-efficiency, so you can't guarantee something will actually 
round-trip (say) over the network or even a filesystem/buffer on 
the same computer.

This, for example, is why one of the ELF specifications 
explicitly recommends not using bitfields to implement it.


More information about the Digitalmars-d mailing list