Forwarding calls to objects of another type

Johan Fjeldtvedt via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 10 14:04:10 PDT 2017


I have a couple of questions related to the following code: 
https://gist.github.com/Jaffe-/b027287a884fc2e173a65601ec242676

1) This is a very simplified example, but what I'm trying to do 
here is to call `foo` on each object in `Container.ss` contains 
when `foo` is called, and likewise for `bar`. To do this without 
having to repeat the foreach loop in every member function, I 
made the `call_for_each` template. However, I found no other way 
to pull this off (without using string mixins) than to also add 
the `call` template in the S struct. At first I thought it would 
be possible to write `s.func(args)` in `call_for_each`, but that 
will try to look up func as a member in the struct.

Is there a more common / idiomatic way of doing this? In C++ this 
would be solved by using a member function pointer as a template 
argument, but as far as I understand D uses delegates (which are 
already bound to an instance) instead?

2) This is about the reduce templates. As I've commented, I can't 
use a template lambda with reduce, but I can use a lambda taking 
ints as arguments. Why is this? The error message I get when 
using the template lambda is:

"template instance reduce!((a, b) => a + b) cannot use local 
'__lambda1' as parameter to non-global template reduce(alias 
fun)()"



More information about the Digitalmars-d-learn mailing list