initializedArray

Philippe Sigaud philippe.sigaud at gmail.com
Tue Dec 20 12:17:44 PST 2011


On Tue, Dec 20, 2011 at 14:22, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:
> Ok here's an initial implementation (I've had to put the initializer
> first, otherwise I can't use variadic arguments):

Why? That works for me:

auto initializedArray(T, I...)(I args)
    if (allSatisfy!(isIntegral, I[0 .. $-1])
    && isImplicitlyConvertible!(I[$-1], BaseElementType!T) // Least
constraining that your original test
    && (I[0..$-1].length == rank!T)) // Verify the number of arguments
{
    auto arr = uninitializedArray!(T)(args[0 .. $-1]);
    initArr(arr, args[$-1]);
    return arr;
}

void main()
{
    auto arr = initializedArray!(float[][])(3, 4, 3.0f);
    writeln(arr);
}

If you want to separate the dimensions and the intializer, another
solution would be to return a intermediate callable struct:

auto arr = initializedArray!(float[][])(3,4)(3.0f);



> http://www.ideone.com/2rqFb


More information about the Digitalmars-d mailing list