initializer for struct with anonymous union.
Danni Coy
danni.coy at gmail.com
Sat Apr 18 02:25:42 UTC 2026
On Sun, Apr 12, 2026 at 9:50 PM Dennis via Digitalmars-d-learn <
digitalmars-d-learn at puremagic.com> wrote:
> 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.
>
ok - I tracked down the source of the error, any float is going to be
default initialised to NaN, I feel like this should be considered a bug (it
makes D look unprofessional) but intialising the floats to zero fixes the
issue (as does the other fixes you suggested).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20260418/0790932b/attachment.htm>
More information about the Digitalmars-d-learn
mailing list