Follow-on question about delegates

BCS none at anon.com
Tue Jun 9 09:43:01 PDT 2009


Hello Jerry,

> OK, having thought a bit more about delegates, I now have another
> question.
> 
> The ABI shows a delegate as consisting of a context ptr and a function
> ptr.  The context ptr can be a class reference, ptr to struct, ptr to
> closure or ptr to stack frame.
> 
> If you're passing one of these delegates into another function, how
> does the receiving function figure out what kind of context it's
> looking at?  Each of these things will look different in memory.
> Also, structs at least don't have a header that can be used to
> disambiguate the situation.
> 
> Thanks,
> Jerry

ok here is a brain twister: the context can be anything, it's just a size_t 
full of bits.

**** THE FOLLOWING IS NOT RECOMMENDED *****

http://codepad.org/8nnoIKNQ

import std.stdio;
struct S
{
    int go()
    {
        U u;
        u.s = this;
        return u.a + u.b;
    }
}

union U
{
    S* s;
    struct {  short a; short b; }
}

void main()
{
     U u;
     u.a= 54;
     u.b = 42;

     int delegate() dg = &u.s.go;


     writef("%s\n", dg()); 
}





More information about the Digitalmars-d mailing list