toDelegate() for D1
Moritz Warning
moritzwarning at web.de
Thu Jan 13 18:33:19 PST 2011
On Thu, 13 Jan 2011 17:14:13 +0000, %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.
>
> ----
> void func(){ writefln("func"); }
>
> void main(){
> void delegate() dg;
> dg = toDelegate(&func);
> dg(); // np
> //writefln(dg.ptr," ", dg.funcptr,"."); // Error: Stack Overflow @
> second dg call //writefln(); // Error: Access Violation @ second dg
> call dg();
> }
> ----
My tangofied of this code works, maybe it's a lib bug?
On the other hand, is the delegate allocated on the stack?
Anyway, here is another way:
R delegate(T) toDg(R, T...)(R function(T) fp)
{
struct dg
{
R opCall(T t)
{
return (cast(R function(T)) this) (t);
}
}
R delegate(T) t;
t.ptr = fp;
t.funcptr = &dg.opCall;
return t;
}
More information about the Digitalmars-d-learn
mailing list