<p>> The closest I came to the correct template is<br>
> template(alias F) timer {<br>
>  auto timer {<br>
>    auto start = Clock.currStdTime();<br>
>    F();<br>
>    return Clock.currStdTime() - time;<br>
>  }<br>
> }</p>
<p>> The problem with the template is I cannot pass any argument into "F". How should I fix this problem?</p>
<p>As Simendsjo said, you can use a function template to get what you want and a nicer syntax as the same time.</p>
<p>Slightly different code can be obtained by starting from the above code:</p>
<p>template timer(alias F) // capture F's name<br>
{<br>
    auto timer(Args...)(Args args) // there are your parameters<br>
     {<br>
        import std.datetime; // inner imports<br>
        auto start = Clock.currStdTime();<br>
        F(args);<br>
        return Clock.currStdTime() - start;<br>
    }<br>
}</p>
<p>So it's a template that gets expanded into another template with the same name, a function.</p>
<p>Note that the construct:</p>
<p>template (T...)<br>
{<br>
    T foo(T t) // some function that depends on the template parameters<br>
    {<br>
    }<br>
}</p>
<p>can get syntax sugar like this:</p>
<p>T foo(T...)(T t)<br>
{<br>
}</p>
<p>you can 'fuse' the external template and the inner code to get a function template. The same for classes and structs.</p>
<p>Now, for something a bit more hairy: Simendsjo's version and mine are not strictly equivalent:</p>
<p>- his version is simpler to type and to reason about. You should use it, it's the D way to do this kind of thing.<br>
- mine (the two level templates) gets an interesting effect: I can use the first level only (getting the function name), keeping the second level for later:</p>
<p>// mine<br>
alias timer!foo tfoo; // tfoo is the inner template, the function template<br>
 /* a bit later */<br>
tfoo(args1);<br>
tfoo(args2);</p>
<p>import std.algorithm;<br>
Args[] argsArray;<br>
auto m = map!(tfoo)(argsArray); // applying tfoo on all arguments groups</p>
<p>// Simendsjo:<br>
timer!foo(args1);<br>
timer!foo(args2); // You must provide 'foo' for each call.<br><br><br></p>
<p>><br>
> Also, I am not entirely sure why I need to write "alias F" instead of just "F", any explanation would be appreciated.</p>
<p>Because in your case, you want to get the name, the symbol. So use an alias parameter. 'F' would be to deal with types, as Ali said.</p>
<p>For example:</p>
<p>auto foo(F, Args...)(F fn, Args args)<br>
{<br>
...<br>
}</p>
<p>in this case, fn is passed as a runtime value of type F:</p>
<p>timer(&foo, args); // the compiler automatically deduces the types of fn and args.<br></p>
<p>Philippe</p>