How to initialize static array member variable?

bearophile bearophileHUGS at lycos.com
Thu Sep 30 13:29:36 PDT 2010


Sebastian Schuberth:

> struct Vector(alias N,T)
> {
>      static immutable Vector X=Vector(1,0,0);
> 
>      this(T[N] v ...) {
>          data=v;
>      }
> 
>      T data[N];
> };
> 
> alias Vector!(3,float) Vec3f;

This seems to work:

struct Vector(int N, T) {
    static immutable Vector x;

    static this() {
        x = Vector(1, 0, 0);
    }

    this(T[N] v...) {
        data = v;
    }

    T[N] data;
}

alias Vector!(3,float) Vec3f;

void main() {
    auto v = Vec3f(1,2,3);
    v = v.x;
    assert(v.data[0] == 1.0);
}


Your code doesn't work because type safe variadic functions can't be used at compile time yet :-) It's a limitation that I think Don will eventually remove.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list