Why must bitfields sum to a multiple of a byte?

Era Scarecrow rtcvb32 at yahoo.com
Tue Jul 31 10:44:09 PDT 2012


On Tuesday, 31 July 2012 at 17:34:33 UTC, monarch_dodra wrote:
> No, the bug shows itself if the first field is 32 bits, 
> regardless of (ulong included).
>
> I would add though that requesting a field in bits that is 
> bigger than the type of the field should not work (IMO). EG:
> --------
> struct A
> {
>     mixin(bitfields!(
>           ushort, "a", 24,
>           uint,    "",  8
>         )
>     );
> }
> --------
> I don't see any way how that could make sense...
> But it *is* legal in C and C++...
> But it does generates warnings...

  Maybe so ushort has extra padding for expansion at some later 
date when they change it to uint?? could put an assert in, but if 
it doesn't break code...

> I think it should static assert in D.

  Glancing over the issue, the [0..31] is a compiler error based 
on bit shifting (not bitfields itself); if the storage type is 
ulong then it shouldn't matter if the first one is a 32bit size 
or not. Unless... Nah, couldn't be... I'll look it over later to 
be sure.

> That's part of the standard: Statements that have no effect are 
> illegal. This is a good think, IMO. I've seen MANY C++ bugs 
> that could have been saved by that.
>
> Regarding the assignment in if. I think it is a good thing. D 
> sides with safety. If you *really* want to test assign, you can 
> always comma operator it.



More information about the Digitalmars-d-learn mailing list