Accepting delegates/functions in templates

"Luís "Luís
Mon May 27 07:05:14 PDT 2013


I'm trying to accept delegates or functions in a templated method.

My first try using "isDelegate!dg || isFunctionPointer!dg" did 
not work because isDelegate fails, although I'm not sure I 
understand exactly why:

     void main()
     {
         int x;
         static assert(isDelegate!(() { x = 4; }));
         // Error: static assert ... is false
     }

If I changed that example to use isSomeFunction it works 
correctly. But when I tried to use isSomeFunction in the proper 
context it fails to compile:

     class A
     {
         void foo(alias dg)()
             if(isSomeFunction!dg)
         {
         }
     }

     void main()
     {
         int x;
         A a = new A;
         a.foo!((int param) { x = 4;});

         // error: cannot use local '__lambda1' as parameter to 
non-global
                       template foo(alias dg)() if 
(isSomeFunction!(dg))
     }

Can anyone help me understand this?


More information about the Digitalmars-d-learn mailing list