Bug 3051 – Passing alias to member function does not work (1/2)
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2009-06-04T11:43:38Z
Last change time
2019-06-26T12:57:00Z
Assigned to
No Owner
Creator
Andrei Alexandrescu
Comments
Comment #0 by andrei — 2009-06-04T11:43:38Z
This is the first of two related bug reports.
class A
{
A next;
void fun(alias fun)()
{
assert(0);
}
void gun()
{
void hun(A a)
{
}
next.fun!(hun)();
}
}
Error: template instance cannot use local 'hun' as parameter to non-global template fun(alias fun)
This might not work due to an implementation limitation in the current dmd (the "this" pointer and the stack frame pointer compete for the same register), but as the next bug will show, the code doesn't work even if said limitation is worked around.
Comment #1 by dfj1esp02 — 2009-06-05T05:20:57Z
how on earth templates can be parameterized with values unevaluatable at compile time?
Comment #2 by schveiguy — 2009-06-05T06:17:44Z
I thought the same as you at first, but I tried this code, and it works:
void fun(alias fx)()
{
fx();
}
void main()
{
int x = 0;
void hun()
{
x++;
}
fun!(hun)();
writefln("%s", x); // outputs 1
}
So I think Andrei is right, it is probably a conflict of 2 this pointers required. The second bug is legit, as it is equivalent to what I wrote here, but I think this one is invalid (need 2 this pointers).
Comment #3 by dfj1esp02 — 2009-06-05T07:36:54Z
huh... &fun is a delegate! Does it take stack pointer with all needed information? Quite hackish, I would say... foreign function messing my stack... ugh...
So it behaves as nested function, but they are said to access this pointer through stack, not register.
Comment #4 by schveiguy — 2009-06-05T08:00:23Z
Since the template is instantiated differently for each function that it is called with, it's entirely possible to "know" the stack frame pointer since it will be a constant offset from the current stack pointer. I think that is why it can work.
In fact, I'm now unsure why that can't work in the other bug too...
Comment #5 by schveiguy — 2009-06-05T08:10:38Z
(In reply to comment #4)
> In fact, I'm now unsure why that can't work for both these bugs too...
One other thing about having two "this" pointers, how come this can work (tested with dmd 1.043)?
void foo(delegate void() dg)
{
dg();
}
class C
{
int y;
void fun()
{
int x = 0;
void gun()
{ x++; y++; } // 2 this pointers needed here!
foo(&gun);
}
}
So probably there should be no conflict. And the "this" pointer of the alias'd function in the bug's example should be statically calculatable, so it doesn't need to occupy a register.
Comment #6 by dfj1esp02 — 2009-06-10T03:30:25Z
void foo(delegate void() dg)
{
dg();
}
class C
{
int y;
void fun()
{
int x = 0;
auto me=this;
void gun()
{ stack.x++; stack.me.y++; }
foo(&gun); //closure
}
}
Comment #7 by public — 2013-08-02T06:09:54Z
So old and so annoying ;)
Comment #8 by dkorpel — 2019-06-26T12:57:00Z
It is a bit weird to mark this as a duplicate of an issue filed later, but since 5710 has been marked as fixed this works too now.
*** This issue has been marked as a duplicate of issue 5710 ***