Question of delegate literals and return from function(GDC,DMD,LDC)

amaury pouly amaury.pouly at gmail.com
Sat Jan 31 14:36:03 PST 2009


Hello,
I have a little question about D: I want to have a function returning a delegate and more precisely a delegate literal that uses one of the arguments of the function. Here is an example:

int delegate(int) test(int[] tab)
{
    return (int d)
    {
        int sum;
        foreach(i;tab)
            sum+=i*d;// stupid computation
        return sum;
    };
}

int main()
{
    auto res=test([1,2,3,4,5,6])(4);
    return res;
}
Of course this example is stupid but the idea is here. The point is that if I compile and run this code with DMD it works fine whereas it segfaults with GDC and LDC(LLVM D). I also know a fix for the segfault with both GDC and LDC:

int delegate(int) test(int[] tab)
{
    struct Foo
    {
        int[] tab;
        
        int fn(int d)
        {
            int sum;
            foreach(i;tab)
                sum+=i*d;// stupid computation
            return sum;
        }
    }
    
    Foo *f=new Foo;
    f.tab=tab;
    return &f.fn;
}

int main()
{
    auto res=test([1,2,3,4,5,6])(4);
    return res;
}

My question is the following: is this a bug of GDC/LDC or is this the expected behaviour ? The second solution must always work but I'm unsure about the first one. Indeed, the specification says that a delegate shall not reference a stack parameter which is logical but it says nothing about function arguments.



More information about the Digitalmars-d mailing list