Foreach on a template?

Robin Allen r.a3 at ntlworld.com
Sun Mar 11 09:56:34 PDT 2007


>>
>> Because "foreach(v ; mytemplate!(int))" expects "mytemplate!(int)" to be
>> either an array or an object which has an opApply overload[*], so
>> technically they *DON'T* mean the same thing.
>>

Okay, I should have said they *should* mean the same thing. Or that the 
programmer using foreach would *mean* the same thing. Anyway, acording 
to the spec, foreach expects an 'expression' which can be pretty much 
anything, so there's no reason why a template shouldn't work.

>>     -- Daniel
>>
>> [*] Incidentally, have you tried specifying a function pointer or
>> delegate to foreach instead?  Offhand, I think that was implemented a
>> while back...

I haven't, but it's not what I'm trying to do here.

> 
> Another possibility is to have the template evaluate to a struct, and to 
> implement opApply in that struct:
>   template foo(args) {
>     struct foo {
>       int opApply(...) {...}
>     }
>   }

I can't do that with my template, it's got lots of other stuff in it. 
And it wouldn't be much neater than what I'm having to do now anyway.

-Rob



More information about the Digitalmars-d mailing list