Hello,

I was wondering if anyone has suggestions on performing arbitrary initialization of static arrays, size of which is arbitrary at compile time.

Consider this:

template StaticArray(T,int N,T v) if (N > 0)
{
    static if (N == 1)
    {
        enum T[N] StaticArray = cast(T[N])[v];
    }
    else
    {
        enum T[N] StaticArray = cast(T[N])([v] ~ StaticArray!(T,N-1,v));
    }
}

T[] generateArray(T,int N,string G=q{a[i] = 0})()
{
    T[] a;
    foreach(i;0..N)
    {
        mixin(G ~ ";");
    }
    return a.dup;
}

template GenStaticArray(T,int N,string G=q{a[i] = 0})
{
    enum T[N] GenStaticArray = cast(T[N])generateArray!(T,N,G)();
}

struct Vector(int N,T)
{
    T data_[N];

    static immutable Vector Identity = { StaticArray!(T,N,1) };
    static immutable Vector Zero = { StaticArray!(T,N,0) };
static immutable Vector UnitX = { GenStaticArray!(T,N,q{a[i] = i ? 0 : 1}) }; static immutable Vector UnitY = { GenStaticArray!(T,N,q{a[i] = i != 1 ? 0 : 1}) }; static immutable Vector UnitZ = { GenStaticArray!(T,N,q{a[i] = i != 2 ? 0 : 1}) };
}

I don't like those casts in templates. In fact, I'm not even sure if such code is legal. Maybe there is some better way of doing this?

--

        

Reply via email to