How to write a variadic template recursive function?
<p>I'm trying to write a variadic template <code>constexpr</code> function which calculates sum of the template parameters given. Here's my code:</p>

<pre><code>template&lt;int First, int... Rest&gt;
constexpr int f()
{
    return First + f&lt;Rest...&gt;();
}

template&lt;int First&gt;
constexpr int f()
{
    return First;
}

int main()
{
    f&lt;1, 2, 3&gt;();
    return 0;
}
</code></pre>

<p>Unfortunately, it does not compile reporting an error message <code>error C2668: 'f': ambiguous call to overloaded function</code> while trying to resolve <code>f&lt;3,&gt;()</code> call.</p>

<p>I also tried to change my recursion base case to accept 0 template arguments instead of 1:</p>

<pre><code>template&lt;&gt;
constexpr int f()
{
    return 0;
}
</code></pre>

<p>But this code also does not compile (message <code>error C2912: explicit specialization 'int f(void)' is not a specialization of a function template</code>).</p>

<p>I could extract first and second template arguments to make this compile and work, like this:</p>

<pre><code>template&lt;int First, int Second, int... Rest&gt;
constexpr int f()
{
    return First + f&lt;Second, Rest...&gt;();
}
</code></pre>

<p>But this does not seem to be the best option. So, the question is: how to write this calculation in an elegant way?</p>

<p><em>UP</em>: I also tried to write this as a single function:</p>

<pre><code>template&lt;int First, int... Rest&gt;
constexpr int f()
{
    return sizeof...(Rest) == 0 ? First : (First + f&lt;Rest...&gt;());
}
</code></pre>

<p>And this also does not work: <code>error C2672: 'f': no matching overloaded function found</code>.</p>