fork download
  1. import std.stdio;
  2. import std.traits;
  3.  
  4. template DelegateToFunction(DG, string ID1=__FILE__, int ID2=__LINE__) {
  5. alias ParameterTypeTuple!DG PT;
  6. alias ReturnType!DG RT;
  7. void* thisp;
  8. RT function(PT) funcp;
  9. RT function(PT) DelegateToFunction(DG dg) {
  10. thisp = dg.ptr;
  11. funcp = dg.funcptr;
  12. return &WrapFunction;
  13. }
  14. RT WrapFunction(PT p) {
  15. RT delegate(PT) dg;
  16. dg.funcptr = funcp;
  17. dg.ptr = thisp;
  18. return dg(p);
  19. }
  20. }
  21.  
  22. void main()
  23. {
  24. int a = 1;
  25. int delegate(int, int) d = (int x, int y){ return a += x + y; };
  26. int function(int, int) f = DelegateToFunction(d);
  27. writeln(f(20, 300));
  28. writeln(a);
  29. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
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
Standard output is empty