DIP: add Bit Fields

Walter Bright newshound2 at digitalmars.com
Mon Mar 11 22:59:59 UTC 2024


On 3/11/2024 11:49 AM, Dennis wrote:
> That's great, but it's not what the DIP says currently, which is that the layout 
> (for non-int) is implementation defined matching a C compiler.

It's the same story for longs. I had neglected to mention that in the DIP.


> It makes the comparison with C's varying `int` size, but the difference is that 
> in that case, D sticks to its guns and keeps `int` 32-bits even when C would 
> make it 16-bit. Perhaps the DIP could say: the layout in D is defined as X, 
> which follows what C compilers do in practice.

D doesn't say what the alignment algorithm is, either, just that it matches the 
associated C compiler. It's always been that way, although it is not an 
accident. It was deliberate on my part. Nobody seems to notice this, it "just 
works", as it should.

As for 16 bit int D, that is a variant of D, not D. (It should perhaps be called 
"D16".) This is perfectly reasonable, because you will never be able to port 
non-trivial D code to D16 without major alterations (DMD itself will never be 
portable to 16 bit code). This frees the 99.99% of D programmers to not feel a 
need to contort their code to be "Standard-conformant" to 16 bit targets. I see 
the pointless self-torture of C programmers doing this all the time. (And in 
spite of these efforts, the C code is still not portable to those other unusual 
platforms.)

C would be better off if they took the same approach.


> Then programmers can be comfortable relying on the layout.

They already can rely on it, in practice.


>> The D compiler itself would, so using it in the D front end will effortlessly 
>> work with the C++ gdc and ldc backends.
> Those are extern(C++) classes, and they already have working bitfields.

There are currently no bit fields in the DMD front end source code, although 
there are some emulations of bit fields.


> They wouldn't be impaired by making bit fields in D consistent and predictable.

They are consistent and predictable now. Just like the D and C++ code reliance 
on ints being 32 bits, the endianess, and the alignment layout of the fields in 
the structs.



More information about the dip.development mailing list