need help to work around float union non-zero init

monkyyy crazymonkyyy at gmail.com
Fri Sep 20 12:22:10 UTC 2024


On Friday, 20 September 2024 at 09:38:54 UTC, Dakota wrote:
> I need my struct defined as `isZeroInit`,  so can I can import 
> them as `di` file.  (this also reduce build size)
>
>
> But I find this problem with float inside union:
>
>
> ```d
> struct test_t {
>     union {
>         int i32;
>         float f32;
>     }
> }
>
> static assert(__traits(isZeroInit, test_t) );
> ```
>
> ```sh
> Error: static assert:  `__traits(isZeroInit, test_t)` is false
> ```
>
> I consider this is compiler bug (it may take years to get 
> fixed), so I am here to ask any way to workaround this problem?

```d
union somevalue{
     int i32;
     float f32;
}

struct test_t{
     somevalue a=void;
}

static assert(__traits(isZeroInit, test_t));
unittest{
   test_t foo;
   assert(foo.a.i32==0);
}
```

this version compiles, but you probably need to provide more 
details


More information about the Digitalmars-d-learn mailing list