Question about Template

Michael pongad at gmail.com
Sat Jun 23 08:30:01 PDT 2012


Hello!
   I have a little question about templates. I am trying to write 
a template "timer" that takes a function and times how long the 
function takes to execute. Something like
   auto time = timer!sort(array);
is equivalent to
   auto start = Clock.currStdTime();
   sort(array);
   auto time = Clock.currStdTime() - time;

except of course that the variable "start" is not created in the 
former case.

The closest I came to the correct template is
template(alias F) timer {
   auto timer {
     auto start = Clock.currStdTime();
     F();
     return Clock.currStdTime() - time;
   }
}

The problem with the template is I cannot pass any argument into 
"F". How should I fix this problem?
Also, I am not entirely sure why I need to write "alias F" 
instead of just "F", any explanation would be appreciated.

Thank you guys,
Michael


More information about the Digitalmars-d mailing list