Member function passed through template alias only requiring `this` in certain conditions?

Emma VuLXn6DBW at PPtUm7TvV6nsw.com
Thu Jul 19 15:12:24 UTC 2018


Hello,

I’ve got this piece of code:

---
import std.stdio;

void test(alias fn1, alias fn2)()
{
     fn1();
     fn2();
}

struct Foo
{
     void foo()
     {
         test!(bar, baz);
     }

     void bar()
     {}

     void baz()
     {}
}
---

If I try to compile it, dmd complains, which I guess makes sense:

---
Error: need this for bar of type void()
Error: need this for baz of type void()
---

However, if I change line 13 from `test!(bar, baz);` to `test!(() 
=> bar, baz);`, it compiles and executes fine, calling both the 
`bar` and `baz` member functions.

Why is this? Shouldn’t it complain about `baz` needing `this`? 
Does the lambda in the first template argument somehow “pull in” 
the `this` reference for `baz`, too?

Thanks!


More information about the Digitalmars-d-learn mailing list