Cannot use function as foreach aggregate.

BCS BCS at pathlink.com
Wed Apr 4 08:58:54 PDT 2007


Jarrett Billingsley wrote:
> "Daniel Keep" <daniel.keep.lists at gmail.com> wrote in message 
> news:ev07p5$17u9$1 at digitalmars.com...
> 
>>So, I've just discovered that you can't use function pointers as the
>>aggregate of a foreach.  To put it mildly: this sucks.
>>
>>Basically I have a bunch of module-level functions used to iterate over
>>some global state private to that module.  So, two questions:
>>
>>1. Is this a bug?  If it's not, why can we use delegates but not
>>function pointers?
>>
>>2. What's the cleanest way to wrap a function pointer in a delegate?
> 
> 
> I don't know if it's the cleanest but it works well.
> 

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



More information about the Digitalmars-d mailing list