[Issue 2101] Please may I use mutable arrays at compile time?

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon May 12 09:41:11 PDT 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2101





------- Comment #1 from samukha at voliacable.com  2008-05-12 11:41 -------
You could use something like this (quickly hacked, dmd 1.029): 

T[] newCtfeArray(T)(size_t size)
{
    T[] array;
    if (!size)
        return array;

    array ~= T.init;

    if (array.length < size)
        return newCtfeArray(array, size);

    return array;
}

private T[] newCtfeArray(T, U = void)(T[] array, size_t size)
{
    while (array.length * 2 <= size)
        array ~= array;

    if (array.length < size)
        return array ~= newCtfeArray!(T)(size - array.length);

    return array;
}

int[] test()
{
    // Allocate an array of 123 ints. The memory will not be freed, sigh
    auto a = newCtfeArray!(int)(123);
    foreach (i, e; a)
    {
        a[i] = e;        
    }

    return a;
}

void main()
{
    static const a = test();
} 


-- 



More information about the Digitalmars-d-bugs mailing list