> Of course, all this is silly if nested functions carry around their
> lexical scope and can be returned. But I dont know that they do.

A simple test case that would not invoke UB with n1968 lambda functions:

#include <stdio.h>

typedef void (*fn_t)();

void doinvoke(fn_t f)
{
    f();
}

fn_t getit(int j)
{
    int i=j;
    void myfn()
    {
        printf("Doit. I dare you: %d\n",i);
    }
    doinvoke(myfn);
    return myfn;
    // under n1968, we'd
    // return <>()->void{printf("Doit. I dare you: %d\n",i);};
}

int main()
{
    fn_t five = getit(5);
    fn_t six = getit(6);
    fn_t seven = getit(7);
    five(); six(); seven();
}

Expected output:
Doit. I dare you: 5
Doit. I dare you: 6
Doit. I dare you: 7
Doit. I dare you: 5
Doit. I dare you: 6
Doit. I dare you: 7

Actual output:
Doit. I dare you: 5
Doit. I dare you: 6
Doit. I dare you: 7
Doit. I dare you: 7
Segmentation fault (core dumped)


Reply via email to