digitalmars.D.learn - Why are std.bitmanip.bitfields so big ?
- wjoe (3/25) Jul 27 2020 I would expect a sizeof 1. Why is its size 16 ?
- Steven Schveighoffer (5/12) Jul 27 2020 Is this inside a function? If so, put `static` on it.
- wjoe (5/18) Jul 28 2020 It was run on the doc page. I suppose the examples are wrapped in
- MoonlightSentinel (3/5) Jul 28 2020 Indeed, see
- wjoe (2/7) Jul 28 2020 Thank you.
- Steven Schveighoffer (8/15) Jul 28 2020 It doesn't necessarily need to be in a unittest block (AFAIK, the
- wjoe (6/21) Jul 28 2020 My reasoning was that a unittest is essentially a function and
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 16I would expect a sizeof 1. Why is its size 16 ? Source: https://dlang.org/library/std/bitmanip/bitfields.html
Jul 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
Jul 27 2020
On Monday, 27 July 2020 at 12:52:53 UTC, Steven Schveighoffer wrote:On 7/27/20 5:49 AM, wjoe wrote:It was run on the doc page. I suppose the examples are wrapped in a unittest block? Anyways, I appreciate your explanation.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
Jul 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
Jul 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:Thank you.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
Jul 28 2020
On 7/28/20 5:46 AM, MoonlightSentinel wrote:On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote: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. -SteveIt 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/ itmanip.d#L293-L314
Jul 28 2020
On Tuesday, 28 July 2020 at 13:00:12 UTC, Steven Schveighoffer wrote:On 7/28/20 5:46 AM, MoonlightSentinel wrote: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 :)On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote: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. -SteveIt 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
Jul 28 2020