toDelegate() for D1

Stewart Gordon smjg_1998 at yahoo.com
Fri Jan 14 11:00:20 PST 2011


On 13/01/2011 17:14, %u wrote:
> == Quote from Simen kjaeraas (simen.kjaras at gmail.com)'s article
>> %u<e at ee.com>  wrote:
>>> I only need something to make a void deleg() from a void func().
>> This works for me:
>> ReturnType!( F ) delegate( ParameterTypeTuple!( F ) ) toDelegate( F )( F
>> fn ) {
>>       return ( ParameterTypeTuple!( F ) args ){ return fn( args ); };
>> }
>
> What am I doing wrong. I can't call the delegate twice with anything in between.

Every delegate needs a context pointer.  If the function is defined 
within a function, that context pointer points to the stack frame of the 
function that defined it.  And in this case it uses something from that 
very stack frame: the parameter fn.

ISTM it works straight after you've created it only because the stack 
frame of toDelegate has not yet been overwritten.  However, I'm still 
puzzled, because the process of calling the delegate ought to itself 
overwrite the stack frame.

You want this:

http://pr.stewartsplace.org.uk/d/sutil/

Stewart.


More information about the Digitalmars-d-learn mailing list