Cannot use function as foreach aggregate.
Jarrett Billingsley
kb3ctd2 at yahoo.com
Wed Apr 4 17:44:56 PDT 2007
"BCS" <BCS at pathlink.com> wrote in message
news:ev0i44$2io$5 at digitalmars.com...
> Jarrett Billingsley wrote:
>
> I can't help but wonder if this works.
>
> 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;
> }
Not quite.. you can't alias addresses, and I don't think you can pass
function pointers as template arguments (though I might be wrong).
Though using your idea of a single static instance of the struct, my
function can then become:
RetType delegate(Args) ToDelegate(RetType, Args...)(RetType function(Args)
func)
{
struct S
{
static S s;
static RetType function(Args) func;
RetType callMe(Args args)
{
return func(args);
}
}
S.func = func;
return &S.s.callMe;
}
Which is pretty sweet, as it no longer requires a heap allocation with each
call.
More information about the Digitalmars-d
mailing list