Calling template member function?
Steven Schveighoffer
schveiguy at gmail.com
Tue Apr 19 15:09:36 UTC 2022
On 4/19/22 9:36 AM, Andrey Zherikov wrote:
> 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?
Something similar just recently came up on discord. Maybe related:
https://issues.dlang.org/show_bug.cgi?id=17063
-Steve
More information about the Digitalmars-d-learn
mailing list