Template factorial function without template specialization : <p>I don't understand the following behavior.</p>

<p>The following code, aimed at computing the factorial at compile time, doesn't even compile:</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
template&lt;int N&gt;
int f() {
  if (N == 1) return 1; // we exit the recursion at 1 instead of 0
  return N*f&lt;N-1&gt;();
}
int main() {
  cout &lt;&lt; f&lt;5&gt;() &lt;&lt; endl;
  return 0;
}
</code></pre>

<p>and throws the following error:</p>

<pre><code>...$ g++ factorial.cpp &amp;&amp; ./a.out 
factorial.cpp: In instantiation of ‘int f() [with int N = -894]’:
factorial.cpp:7:18:   recursively required from ‘int f() [with int N = 4]’
factorial.cpp:7:18:   required from ‘int f() [with int N = 5]’
factorial.cpp:15:16:   required from here
factorial.cpp:7:18: fatal error: template instantiation depth exceeds maximum of 900 (use ‘-ftemplate-depth=’ to increase the maximum)
    7 |   return N*f&lt;N-1&gt;();
      |            ~~~~~~^~
compilation terminated.
</code></pre>

<p>whereas, upon adding the specialization for <code>N == 0</code> (which the template above doesn't even reach),</p>

<pre><code>template&lt;&gt;
int f&lt;0&gt;() {
  cout &lt;&lt; "Hello, I'm the specialization.\n";
  return 1;
}
</code></pre>

<p>the code compiles and give the correct output of, even if the specialization is never used:</p>

<pre><code>...$ g++ factorial.cpp &amp;&amp; ./a.out 
120
</code></pre>
