Cannot use function as foreach aggregate.

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Apr 4 06:25:47 PDT 2007


"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.

RetType delegate(Args) ToDelegate(RetType, Args...)(RetType function(Args) 
func)
{
    struct S
    {
        RetType function(Args) func;

        RetType callMe(Args args)
        {
            return func(args);
        }
    }

    S* s = new S;
    s.func = func;
    return &s.callMe;
}






More information about the Digitalmars-d mailing list