[Issue 1648] Casting a function literal to void* swallows the function or segfaults

Jarrett Billingsley kb3ctd2 at yahoo.com
Fri Nov 9 05:23:15 PST 2007


"Derek Parnell" <derek at nomail.afraid.org> wrote in message 
news:1pvgn2j8ihvm$.1tg724zepcfnz.dlg at 40tude.net...

> import std.stdio;
>
> class Foo {
>    alias void delegate()  dg;
>    private dg* _funcptr;
>    private dg  _func;
>
>    void send (dg* func) {
>        _funcptr = func;
>    }
>
>    void send (dg func) {
>        _func = func;
>        _funcptr = &_func;
>    }
>
>    void execute () {
>        (*_funcptr)();
>    }
>
> }
>
> void main () {
>    Foo foo = new Foo();
>
>    auto func = { writefln("greetings"); };
>    foo.send(&func);
>    foo.execute();
>
>    foo.send({ writefln("this one is typed"); });
>    foo.execute();
> }

That works, but you still have to make sure that you don't return from the 
function that called foo.send before calling foo.execute (assuming that all 
this doesn't necessarily happen in main).  If you made 'func' static, it'd 
be fine. 




More information about the Digitalmars-d-bugs mailing list