Bitfield structs and suggestion

Janice Caron caron800 at googlemail.com
Fri Nov 23 09:50:43 PST 2007


On 11/23/07, Richard Bradley <darthalias at yahoo.com> wrote:
> I whole heartedly disagree that your workaround is sufficient.

I did not claim that the workaround was sufficient, I only claimed
that the workaround exists. You can't disagree with a claim I didn't
make.


> If you look at your example you are replacing the four parameters in the C++ version ( the four bitfield sizes) with twelve parameters in D ( 4 bitfield positions, and 8 masks.) not to mention the shifting and bitwise operations. And the need of the user to keep track of the "bits" members size. Plus, what if you wanted a structure 512 bytes long?

Agreed. That would make it tricky to write getter and setter functions
for each bitfield. I've certainly no objection to the compiler making
things easier for the programmer. The shifting and bitwise operations
will happen behind the scenes though, even if you're not aware of it.

I think the way to do it to have the compiler automagically write
getter and setter functions for you. That way, the following would be
allowed:

    n = s.a; // read a bitfield
    s.a = n; // write a bitfield

but things like s.a+=, &s.a, passing bitfields by reference, and so
on, would still be disallowed. That would force the programmer to do
all the calculations in "real" ints, using the bitfields only for
storage.



More information about the Digitalmars-d mailing list