[Issue 1869] Semantically returning an array from a funciton is difficult

Janice Caron caron800 at googlemail.com
Mon Feb 25 23:40:07 PST 2008


On 26/02/2008, d-bugmail at puremagic.com <d-bugmail at puremagic.com> wrote:
>  but that still doesn't solve the base issue: I want a function to return a
>  pointer to a chunk of memory that is 4 ints in a row (or it is that as best the
>  compiler can tell).

    int[] f()
    {
        return [1,2,3,4].dup;
    }

or

    int[] f()
    {
        auto r = new int[4];
        /* assign r's elements */
        return r;
    }

If you need to prove that the array is exactly four bytes long, do

    int[] f()
    out(r)
    {
        assert(r.length == 4);
    }
    body
    {
        ...
    }

If you really, really need to return by value, do

    struct A(T, int N)
    {
        T[N] a;
    }

    A!(int,4) f()
    {
        A!(int,4) r;
        /* assign r.a */
        return r;
    }

Hopefully, one of these will suit your needs


More information about the Digitalmars-d-bugs mailing list