Cannot use function as foreach aggregate.

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Apr 5 15:07:33 PDT 2007


"BCS" <BCS at pathlink.com> wrote in message 
news:ev379f$1ccm$1 at digitalmars.com...
>
> I don't think that will work. It will only allow one delegate per function 
> type.

Yeah, you're right; I forgot that I didn't use an alias template parameter 
so it would be based on signature, not name.

> also it occurs to me that there is no reason that the addressOf is needed. 
> If functions as args works (I think it does) then this should work:
>
> template ToDelegate(RetType, RetType function(Args) func, Args...)
> {
>     struct S
>     {
>         static S s
>         RetType callMe(Args args)
>         {
>             return func(args);
>         }
>     }
>     alias S.s.callMe ToDelegate;
> }

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;
    }
}

...

int foo(int i){return 1;}
int bar(int i){return 2;}

...

auto a = ToDelegate!(foo);
auto b = ToDelegate!(bar);

writefln(a(1));
writefln(a(2)); 





More information about the Digitalmars-d mailing list