www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Functions as parameters in templates.

reply J Anderson <REMOVEanderson badmama.com.au> writes:
Walter Wrote:

I wish D templates could accept operators/functions as parameters.



It can accept functions as parameters - both as function pointers, 

aliases.



Let me get this straight. What is the syntax? I tried this: template tempT(alias func) { void temp() { func(); } } void myfunc() { } alias tempT!(myfunc).temp() foo; int main( char[][] args ) { foo(); return 0; }


 Cool, now for operators....

You can use the equivalent name for the operator functions.

-- -Anderson: http://badmama.com.au/~anderson/
May 14 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c8308u$105q$4 digitaldaemon.com...
 Let me get this straight.  What is the syntax?  I tried this:

 template tempT(alias func)
 {     void temp()
    {         func();
    }
    }


 void myfunc()
 {     }

 alias tempT!(myfunc).temp() foo;

use: alias tempT!(myfunc).temp foo; Unfortunately, there's a bug preventing that from working, which will be fixed in the next update.
 int main( char[][] args ) {
    foo();

    return 0;
 }
 Cool, now for operators....



If class Foo has an opAdd() function, use an alias parameter for Foo.opAdd.
May 14 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Walter wrote:

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c8308u$105q$4 digitaldaemon.com...
  

Let me get this straight.  What is the syntax?  I tried this:

template tempT(alias func)
{     void temp()
   {         func();
   }
   }


void myfunc()
{     }

alias tempT!(myfunc).temp() foo;
    

use: alias tempT!(myfunc).temp foo;

Unfortunately, there's a bug preventing that from working, which will be
fixed in the next update.

-- -Anderson: http://badmama.com.au/~anderson/
May 14 2004