initializer for struct with anonymous union.
Dennis
dkorpel at gmail.com
Sun Apr 12 11:49:15 UTC 2026
On Saturday, 11 April 2026 at 06:56:36 UTC, Danni Coy wrote:
> ok gets weirder - adding an int to the beginning of the
> anonymous union removes the linker problem.
>
> union
> {
> int _;
> EventA a;
> EventB b;
> }
>
> Is this a bug, or is there something I am missing?
Most likely `EventA` has an .init value that isn't all 0. When it
becomes part of a struct or the first member of a union, that
also makes the enclosing struct have a non-zero .init symbol
which you must link when you want to use it. Either make `EventA`
a zero initialized type (you can check with `__traits(isZeroInit,
EventA)`) or override the default init value, like `EventA a =
0;` or `EventA a = void;`.
Or just ensure that every module you import also gets compiled
into the final executable. Sometimes you can get away with
'header only' imports, but it's bound to bring up subtle issues
like this so it's best to just include everything either using
the `-i` switch or by listing every file yourself.
More information about the Digitalmars-d-learn
mailing list