initializing a static array

ag0aep6g anonymous at example.com
Tue Oct 10 16:19:59 UTC 2017


On 10/10/2017 03:36 PM, Simon Bürger wrote:
> I have a static array inside a struct which I would like to be 
> initialized to all-zero like so
> 
>    struct Foo(size_t n)
>    {
>      double[n] bar = ... all zeroes ...
>    }
> 
> (note that the default-initializer of double is nan, and not zero)
> 
> I tried
> 
>    double[n] bar = 0;  // does not compile

Works for me:

----
struct Foo(size_t n)
{
     double[n] bar = 0;
}
void main()
{
     import std.stdio;
     Foo!5 foo;
     writeln(foo.bar); /* prints "[0, 0, 0, 0, 0]" */
}
----


More information about the Digitalmars-d-learn mailing list