import std.stdio ;
import std.traits ;
template DelegateToFunction( DG, string ID1= __FILE__ , int ID2= __LINE__ ) {
alias ParameterTypeTuple! DG PT;
alias ReturnType! DG RT;
void * thisp;
RT function ( PT) funcp;
RT function ( PT) DelegateToFunction( DG dg) {
thisp = dg.ptr ;
funcp = dg.funcptr ;
return & WrapFunction;
}
RT WrapFunction( PT p) {
RT delegate ( PT) dg;
dg.funcptr = funcp;
dg.ptr = thisp;
return dg( p) ;
}
}
void main( )
{
int a = 1 ;
int delegate ( int , int ) d = ( int x, int y) { return a += x + y; } ;
int function ( int , int ) f = DelegateToFunction( d) ;
writeln( f( 20 , 300 ) ) ;
writeln( a) ;
}
aW1wb3J0IHN0ZC5zdGRpbzsKaW1wb3J0IHN0ZC50cmFpdHM7Cgp0ZW1wbGF0ZSBEZWxlZ2F0ZVRvRnVuY3Rpb24oREcsIHN0cmluZyBJRDE9X19GSUxFX18sIGludCBJRDI9X19MSU5FX18pIHsKICAgIGFsaWFzIFBhcmFtZXRlclR5cGVUdXBsZSFERyBQVDsKICAgIGFsaWFzIFJldHVyblR5cGUhREcgUlQ7CiAgICB2b2lkKiB0aGlzcDsKICAgIFJUIGZ1bmN0aW9uKFBUKSBmdW5jcDsKICAgIFJUIGZ1bmN0aW9uKFBUKSBEZWxlZ2F0ZVRvRnVuY3Rpb24oREcgZGcpIHsKICAgICAgICB0aGlzcCA9IGRnLnB0cjsKICAgICAgICBmdW5jcCA9IGRnLmZ1bmNwdHI7CiAgICAgICAgcmV0dXJuICZXcmFwRnVuY3Rpb247CiAgICB9CiAgICBSVCBXcmFwRnVuY3Rpb24oUFQgcCkgewogICAgICAgIFJUIGRlbGVnYXRlKFBUKSBkZzsKICAgICAgICBkZy5mdW5jcHRyID0gZnVuY3A7CiAgICAgICAgZGcucHRyID0gdGhpc3A7CiAgICAgICAgcmV0dXJuIGRnKHApOwogICAgfQp9CiAKdm9pZCBtYWluKCkKewogICAgaW50IGEgPSAxOwogICAgaW50IGRlbGVnYXRlKGludCwgaW50KSBkID0gKGludCB4LCBpbnQgeSl7IHJldHVybiBhICs9IHggKyB5OyB9OwogICAgaW50IGZ1bmN0aW9uKGludCwgaW50KSBmID0gRGVsZWdhdGVUb0Z1bmN0aW9uKGQpOwogICAgd3JpdGVsbihmKDIwLCAzMDApKTsKICAgIHdyaXRlbG4oYSk7Cn0=
compilation info
prog.d(4): Error: template prog.DelegateToFunction(DG,string ID1 = __FILE__,int ID2 = __LINE__) is not a function template
prog.d(26): Error: template prog.DelegateToFunction(DG,string ID1 = __FILE__,int ID2 = __LINE__) cannot deduce template function from argument types !()(int delegate(int, int))
prog.d(26): Error: cannot implicitly convert expression ((DelegateToFunction(DG,string ID1 = __FILE__,int ID2 = __LINE__))(d)) of type int to int function(int, int)
stdout