Taking the address of an eponymous template

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 31 03:14:11 PDT 2017


On 07/31/2017 11:44 AM, Arafel wrote:
> ```
> class C {
[...]
>      template baz(string S) {
>          void baz()() {
>          }
>      }
> }
> 
> void main() {
[...]
>      void delegate() aBaz = &c.baz!"a"; // This doesn't compile.
> }
> ```
> 
> If I try &c.baz!"a".baz it doesn't work either (I get a different error 
> message.
> 
> Do you know if this works (and if so, what should I do), or if it's 
> supposed to?

You'd have to instantiate the inner template, too. Something like 
`&c.baz!"a".baz!()`, but that doesn't work. I don't know how you could 
make it work.

> Of course in this case I don't need to use an eponymous template at all, 
> bit it's just a simplification to try to get everything else out of the 
> way...
> 
> In case anyone is interested, the real case is something more like this:
> 
> ```
> class C {
>      template baz(args...) if (someCondition!args) {
>          void baz(this T) {
>          }
>      }
> }
> ```

(Assuming the inner baz is supposed to be `void baz(this T)() {}`.)

You'd still have to instantiate the inner baz in order to get a delegate 
of it. But even if we figured out how to do that, my guess is you don't 
want to specify `this T` explicitly.

So how about a function literal:

     void delegate() aBaz = () => c.baz!(int, float)();

> As far as I know, that's the only way to combine a "this" template 
> parameter with variadic template parameters.

That's right if you want to pass `args` explicitly, but `this` 
implicitly. If specifying `args` via IFTI is an option, then this works, 
too:

----
class C {
     void baz(this T, args...)(args) {}
}

void main() {
     C c = new C();
     void delegate() aBaz = () => c.baz(1, 2.3, "four");
}
----

A function literal again, because you have to call baz in order to 
instantiate it (or you have specify `this T` and `args` explicitly). But 
you can't get a delegate from a call.


More information about the Digitalmars-d-learn mailing list