April 06, 2007 Re: Cannot use function as foreach aggregate. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | "BCS" <BCS@pathlink.com> wrote in message news:ev45id$94v$1@digitalmars.com... > Jarrett Billingsley wrote: > > Of course this all only works for statically known functions. If that isn't the case then the original solution is needed. > > OTOH this might work > > template ToDelegate(alias func) > { > ReturnType!(func) delegate(ParameterTypeTuple!(func)) ToDelegate() > { > struct S > { > ReturnType!(func) callMe(ParameterTypeTuple!(func) args) > { > return(cast(typeof(func))cast(void*)this)(args); > } > } > return &((cast(S*)cast(void*)&func).callMe); > } > } > > I'd have to play with it but the same thing could be done for a dynamic pointer just with a few things moved around. > > > Ohh, I'd so fire anyone who tried to get me to pay them for writing that. Error: can't convert from void* to a function pointer. Oh well. | |||
April 06, 2007 Re: Cannot use function as foreach aggregate. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote: > "BCS" <BCS@pathlink.com> wrote in message news:ev45id$94v$1@digitalmars.com... > >>Jarrett Billingsley wrote: >> >>Of course this all only works for statically known functions. If that isn't the case then the original solution is needed. >> >>OTOH this might work >> >>template ToDelegate(alias func) >>{ >> ReturnType!(func) delegate(ParameterTypeTuple!(func)) ToDelegate() >> { >> struct S >> { >> ReturnType!(func) callMe(ParameterTypeTuple!(func) args) >> { >> return(cast(typeof(func))cast(void*)this)(args); >> } >> } >> return &((cast(S*)cast(void*)&func).callMe); >> } >>} >> >>I'd have to play with it but the same thing could be done for a dynamic pointer just with a few things moved around. >> >> >>Ohh, I'd so fire anyone who tried to get me to pay them for writing that. > > > Error: can't convert from void* to a function pointer. Oh well. > > OK, I've tested this one on 0.177 (Yes, I need to update this system :-b) R delegate(Args) ToDelegate(R, Args...)(R function(Args) func) { struct S { void callMe(Args args) { return (cast(R function(Args))cast(void*)this)(args); } } return &((cast(S*)cast(void*)func).callMe); } | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply