Calling template member function?

Andrey Zherikov andrey.zherikov at gmail.com
Tue Apr 19 13:36:26 UTC 2022


I want to migrate my library API from standalone function that 
takes delegate as argument to a template member function that 
takes delegate as a template parameter but compiler errors out. 
Here is code example:
```d
import std.stdio;

template T(P)
{
     static void f_new(alias func)()
     {
         func();
     }
}

void f(FUNC)(FUNC func)
{
     T!int.f_new!(() => func());
}

void main()
{
     f(function () { __LINE__.writeln; });
}
```

Compiler error:
```
onlineapp.d(7): Error: `static` function `onlineapp.f!(void 
function() @safe).f.f_new!(delegate () @safe
{
(*func)();
return ;
}
).f_new` cannot access delegate `__lambda2` in frame of function 
`onlineapp.f!(void function() @safe).f`
onlineapp.d(13):        `__lambda2` declared here
onlineapp.d(13): Error: template instance `onlineapp.f!(void 
function() @safe).f.f_new!(delegate () @safe
{
(*func)();
return ;
}
)` error instantiating
onlineapp.d(18):        instantiated from here: `f!(void 
function() @safe)`
```

What confuses me a lot is that if I remove `template T` then 
everything works perfectly:
```d
import std.stdio;

void f_new(alias func)()
{
     func();
}

void f(FUNC)(FUNC func)
{
     f_new!(() => func());
}

void main()
{
     f(function () { __LINE__.writeln; });
}
```

Is there an issue in template processing in compiler?


More information about the Digitalmars-d-learn mailing list