C bitfields guarantees

Steven Schveighoffer schveiguy at gmail.com
Wed Jul 10 01:41:54 UTC 2024


On Wednesday, 10 July 2024 at 00:32:53 UTC, Timon Gehr wrote:
> On 7/9/24 01:52, Walter Bright wrote:
>> I agree I sometimes have trouble writing exact specifications, 
>> but I'm also confident that you understand this.
>> ...
>
> Sure, but I really think we should just enforce this kind of 
> rule for `extern(D)` bitfields. If a programmer does not follow 
> the rule, just error out and present options to the programmer 
> for how to make the code compile:
>
> error: bitfield layout is ambiguous
>
> - add extern(C) to match the layout of the associated C compiler
> - add padding and/or 0-width bitfields to unambiguously start 
> bitfields on a T alignment boundary without straddling
>
> A priori you just don't know which of those was intended. It's 
> good to require explicit input here, as it is subtle.

Yes, this is the correct answer. I stayed away from `extern(C)` 
specification because I *kinda* see the point that we have no 
precedent for `extern(C)` to adjust field layout. But this seems 
so obvious to me, I challenge anyone to fault this as a bad 
experience. For those who want C Compatibility, just say so. The 
D compiler has you covered. For those who want exact bitfield 
layout, you can use D, because D ensures you have not shot 
yourself in the foot by making an ambiguous layout request.

-Steve


More information about the Digitalmars-d mailing list