Thread overview
Why are std.bitmanip.bitfields so big ?
Jul 27, 2020
wjoe
Jul 28, 2020
wjoe
Jul 28, 2020
MoonlightSentinel
Jul 28, 2020
wjoe
Jul 28, 2020
wjoe
July 27, 2020
From the API documentation:

> Create a bitfield pack of eight bits, which fit in one ubyte.
> [...]
>
> struct A
> {
>     mixin(bitfields!(
>        bool, "flag1",    1,
>        bool, "flag2",    1,
>        uint, "",         6));
> }
> 
> A a;
> writeln(a.flag1); // 0
> a.flag1 = 1;
> writeln(a.flag1); // 1
> a.flag1 = 0;
> writeln(a.flag1); // 0
> writeln(a.sizeof);


> Application output
>
> false
> true
> false
> 16

I would expect a sizeof 1. Why is its size 16 ?

Source: https://dlang.org/library/std/bitmanip/bitfields.html
July 27, 2020
On 7/27/20 5:49 AM, wjoe wrote:
> struct A
> {
>      mixin(bitfields!(
>         bool, "flag1",    1,
>         bool, "flag2",    1,
>         uint, "",         6));
> }

Is this inside a function? If so, put `static` on it.

What you are seeing is the 8-byte frame pointer that comes from inner structs so you can access stack variables inside the struct.

-Steve
July 28, 2020
On Monday, 27 July 2020 at 12:52:53 UTC, Steven Schveighoffer wrote:
> On 7/27/20 5:49 AM, wjoe wrote:
>> struct A
>> {
>>      mixin(bitfields!(
>>         bool, "flag1",    1,
>>         bool, "flag2",    1,
>>         uint, "",         6));
>> }
>
> Is this inside a function? If so, put `static` on it.
>
> What you are seeing is the 8-byte frame pointer that comes from inner structs so you can access stack variables inside the struct.
>
> -Steve

It was run on the doc page. I suppose the examples are wrapped in a unittest block?

Anyways, I appreciate your explanation.
July 28, 2020
On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote:
> It was run on the doc page. I suppose the examples are wrapped in a unittest block?

Indeed, see https://github.com/dlang/phobos/blob/cd2ba0d2c378a893ec0eaefc57b87d0770a1990c/std/bitmanip.d#L293-L314
July 28, 2020
On Tuesday, 28 July 2020 at 09:46:01 UTC, MoonlightSentinel wrote:
> On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote:
>> It was run on the doc page. I suppose the examples are wrapped in a unittest block?
>
> Indeed, see https://github.com/dlang/phobos/blob/cd2ba0d2c378a893ec0eaefc57b87d0770a1990c/std/bitmanip.d#L293-L314

Thank you.
July 28, 2020
On 7/28/20 5:46 AM, MoonlightSentinel wrote:
> On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote:
>> It was run on the doc page. I suppose the examples are wrapped in a unittest block?
> 
> Indeed, see https://github.com/dlang/phobos/blob/cd2ba0d2c378a893ec0eaefc57b87d0770a1990c/std/bitmanip.d#L293-L314 
> 

It doesn't necessarily need to be in a unittest block (AFAIK, the example executer isn't actually running the unittests of phobos), but it does need to be inside a function, because you have executable code there. Either way, yes, it's run inside a function, so you need to apply static to the struct to avoid the frame pointer. I tested that, and it prints 1 instead of 16.

-Steve
July 28, 2020
On Tuesday, 28 July 2020 at 13:00:12 UTC, Steven Schveighoffer wrote:
> On 7/28/20 5:46 AM, MoonlightSentinel wrote:
>> On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote:
>>> It was run on the doc page. I suppose the examples are wrapped in a unittest block?
>> 
>> Indeed, see https://github.com/dlang/phobos/blob/cd2ba0d2c378a893ec0eaefc57b87d0770a1990c/std/bitmanip.d#L293-L314
>> 
>
> It doesn't necessarily need to be in a unittest block (AFAIK, the example executer isn't actually running the unittests of phobos), but it does need to be inside a function, because you have executable code there. Either way, yes, it's run inside a function, so you need to apply static to the struct to avoid the frame pointer. I tested that, and it prints 1 instead of 16.
>
> -Steve

My reasoning was that a unittest is essentially a function and it's incredibly convenient for such things but I hadn't really checked prior to MoonlightSentinel's reply.

Thanks again :)