Bug 3055 – & operator doesn't get correct func to construct the delegate
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2009-06-06T19:36:00Z
Last change time
2015-06-09T01:27:45Z
Assigned to
nobody
Creator
davidl
Comments
Comment #0 by davidl — 2009-06-06T19:36:15Z
import std.stdio;
class t
{
void func(){writefln("t");}
void delegate() getdelegate(){return &t.func; } // this doesn't return the delegate of t.func
}
class v:t
{
void func(){writefln("v");}
}
class r:v
{
void func(){writefln("r");}
void delegate() getdelegate(){return t.getdelegate(); }
}
void main()
{
r p= new r;
p.getdelegate()();
}
h3 gave me the solution which can work correctly:
import std.stdio;
class t
{
void func(){writefln("t");}
void delegate() getdelegate(){ static void function() getfuncptr() { return &func; } auto res = &func; res.funcptr = getfuncptr; return res; }
}
class v:t
{
void func(){writefln("v");}
}
class r:v
{
void func(){writefln("r");}
void delegate() getdelegate(){return t.getdelegate(); }
}
void main()
{
r p= new r;
p.getdelegate()();
}
Comment #1 by maxim — 2013-05-22T09:15:28Z
Taking address of member function in the form of &A.foo yields as expected a function pointer, not delegate. To get delegate one need to omit class name.