Initialising arrays at compile time

Peter Alexander peter.alexander.au at gmail.com
Sun Jan 2 07:03:17 PST 2011


On 2/01/11 2:16 PM, bearophile wrote:
> Is this good enough?
>
> struct Vec {
>      double[2] e;
>
>      static enum Vec zero = Vec([0.0, 0.0]);
>
>      this(real x, real y) {
>          e[0] = x;
>          e[1] = y;
>      }
> }

Well it works, so yes :-)

That's quite irritating. Why does the automatically generated one work, 
but not the hand written one. Grrr...

I also discovered that this works:

enum Vec zero = Vec([0.0, 0.0]);

this(double[2] v)
{
   foreach (size_t i, ref r; e)
     r = v[i];
}

Which works in the general case of an arbitrary sized vector.


More information about the Digitalmars-d-learn mailing list