Generating struct .init at run time?

kinke noone at
Thu Jul 2 17:51:15 UTC 2020

On Thursday, 2 July 2020 at 16:51:52 UTC, kinke wrote:
> `= void` for members doesn't work and, I dare say, not work 
> anytime soon if ever.

I've quickly checked; `= void` for members has 
initialize-with-zeros semantics too, so with LDC, it's equivalent 
to `= 0` but applicable to user-defined types as well.
For DMD, `= void` for non-default-zero-initialized members can be 
used for the same effect. If all members are effectively 
zero-initialized, the init symbol isn't emitted, and the compiler 
initializes the whole struct with zeros. With `= 0`, DMD still 
emits the init symbol into the object file, but doesn't use it 
(at least not for stack allocations).

TLDR: Seems like initializing (all non-default-zero-initialized) 
members with `= void` is the portable solution to elide the init 
symbols *and* have the compiler initialize the whole struct with 
zeros, so a manual memset isn't required.

More information about the Digitalmars-d-learn mailing list