Movement against float.init being nan

Nick Treleaven nick at geany.org
Sat Aug 20 17:51:58 UTC 2022


On Friday, 19 August 2022 at 17:14:35 UTC, Steven Schveighoffer 
wrote:
> ```c
> ReallyLargeStruct foo = { 0 };
> ```
>
> There just is no equivalent in D. In order to set all fields to 
> 0, I would have to specify a complete layout of the entire 
> thing. This could probably be done via introspection. But I 
> just don't like it even in that case.

Maybe something like this:
```d
template ZeroInit(T)
{
	union U
	{
		byte[T.sizeof] a;
		T zeroed;
	}
	enum ZeroInit = U().zeroed;
}

struct S
{
	float f;
	float[2] a;
}

void main()
{
	S s = ZeroInit!S;
	assert(s.f == 0);
	assert(s.a == [0,0]);
}
```


More information about the Digitalmars-d mailing list