title: How to understand the result of output with C++ templates
body: <p>I wrote a judgment code of even/odd numbers with C++ templates.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template &lt;int N, int Mod2=N%2&gt;
  struct Print {
    Print() {
      std::cout &lt;&lt; N &lt;&lt; std::endl;
    }
  };
  
  template &lt;int N&gt;
  struct Print&lt;N, 0&gt; {
    Print() {
      std::cout &lt;&lt; "Even!" &lt;&lt; std::endl;
    }
  };
  
  template &lt;int N&gt;
  struct Print&lt;N, 1&gt; {
    Print() {
      std::cout &lt;&lt; "Odd!" &lt;&lt; std::endl;
    }
  };
  
  template &lt;int N&gt;
  struct EvenOdd {
    EvenOdd() {
      EvenOdd&lt;N+1&gt;();
      Print&lt;N&gt;();
    }
  };
  
  template &lt;&gt;
  struct EvenOdd&lt;10&gt; {
    EvenOdd() {
      std::cout &lt;&lt; "Hey!" &lt;&lt; std::endl;
    }
  };
  
  int main()
  {
    EvenOdd&lt;0&gt;();
    return 0;
  }
  </code></pre>
  
  <p>This code outputs:</p>
  
  <pre><code>$ ./a.out
  Hey!
  Odd!
  Even!
  Odd!
  Even!
  Odd!
  Even!
  Odd!
  Even!
  Odd!
  Even!
  </code></pre>
  
  <p>I predicted that <pre>EvenOdd&lt;10>::EvenOdd() //=> "Hey!"</pre> is called lastly. But, it's mistake.</p>
  
  <p>Why does "Hey!" output firstly?</p>