Cannot use function as foreach aggregate.

BCS BCS at pathlink.com
Thu Apr 5 17:49:03 PDT 2007


Jarrett Billingsley wrote:
> 
> You can't alias expressions.  It has to be a function:
> 
> template ToDelegate(alias func)
> {
>     ReturnType!(func) delegate(ParameterTypeTuple!(func)) ToDelegate()
>     {
>         struct S
>         {
>             static S s;
>             ReturnType!(func) callMe(ParameterTypeTuple!(func) args)
>             {
>                 return func(args);
>             }
>         }
> 
>         return &S.s.callMe;
>     }
> }

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.



More information about the Digitalmars-d mailing list