title: Why this template recursion won't compile?
body: <p>I am attempting to print out the numbers from 1 to 1000 in C++ without any control statements or conditionals. I have found <a href="https://stackoverflow.com/a/4568650/675100">this question + answer on SO</a>, but I cannot compile it on Linux with gcc 4.8. Any ideas why this is not working?</p>
  
  <p>This is the code I'm trying:</p>
  
  <pre><code>#include &lt;iostream&gt;
  template&lt;int N&gt;
  struct NumberGeneration{
    static void out(std::ostream&amp; os)
    {
      NumberGeneration&lt;N-1&gt;::out(os);
      os &lt;&lt; N &lt;&lt; std::endl;
    }
  };
  template&lt;&gt;
  struct NumberGeneration&lt;1&gt;{
    static void out(std::ostream&amp; os)
    {
      os &lt;&lt; 1 &lt;&lt; std::endl;
    }
  };
  int main(){
     NumberGeneration&lt;1000&gt;::out(std::cout);
  }
  </code></pre>
  
  <p>The error I get is this: </p>
  
  <pre><code>main2.cpp:9:34: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) instantiating ‘struct NumberGeneration&lt;100&gt;’
       NumberGeneration&lt;N-1&gt;::out(os);
                                    ^
  main2.cpp:9:34:   recursively required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 999; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:9:34:   required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 1000; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:21:28:   required from here
  
  main2.cpp:9:34: error: incomplete type ‘NumberGeneration&lt;100&gt;’ used in nested name specifier
  main2.cpp:10:8: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template&lt;class _CharT, class _Traits&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, char) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]’
       os &lt;&lt; N &lt;&lt; std::endl;
          ^
  main2.cpp:9:34:   recursively required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 999; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:9:34:   required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 1000; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:21:28:   required from here
  
  main2.cpp:10:8: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, char) [with _Traits = std::char_traits&lt;char&gt;]’
  main2.cpp:9:34:   recursively required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 999; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:9:34:   required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 1000; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:21:28:   required from here
  
  main2.cpp:10:8: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, signed char) [with _Traits = std::char_traits&lt;char&gt;]’
  main2.cpp:9:34:   recursively required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 999; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:9:34:   required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 1000; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:21:28:   required from here
  
  main2.cpp:10:8: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, unsigned char) [with _Traits = std::char_traits&lt;char&gt;]’
  main2.cpp:9:34:   recursively required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 999; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:9:34:   required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 1000; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:21:28:   required from here
  
  main2.cpp:10:13: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template&lt;class _CharT, class _Traits&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, _CharT) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  main2.cpp:9:34:   recursively required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 999; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:9:34:   required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 1000; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:21:28:   required from here
  
  main2.cpp:10:13: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template&lt;class _CharT, class _Traits&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, const _CharT*) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]’
  main2.cpp:9:34:   recursively required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 999; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:9:34:   required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 1000; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:21:28:   required from here
  
  main2.cpp:10:13: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template&lt;class _CharT, class _Traits&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::endl(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]’
  main2.cpp:9:34:   recursively required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 999; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:9:34:   required from ‘static void NumberGeneration&lt;N&gt;::out(std::ostream&amp;) [with int N = 1000; std::ostream = std::basic_ostream&lt;char&gt;]’
  main2.cpp:21:28:   required from here
  
  main2.cpp:10:13: error: no match for ‘operator&lt;&lt;’ (operand types are ‘std::basic_ostream&lt;char&gt;’ and ‘&lt;unresolved overloaded function type&gt;’)
  main2.cpp:10:13: note: candidates are:
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:108:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; (*)(std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp;)) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(__ostream_type&amp; (*__pf)(__ostream_type&amp;))
         ^
  /usr/include/c++/4.8/ostream:108:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘std::basic_ostream&lt;char&gt;::__ostream_type&amp; (*)(std::basic_ostream&lt;char&gt;::__ostream_type&amp;) {aka std::basic_ostream&lt;char&gt;&amp; (*)(std::basic_ostream&lt;char&gt;&amp;)}’
  /usr/include/c++/4.8/ostream:117:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;::__ios_type&amp; (*)(std::basic_ostream&lt;_CharT, _Traits&gt;::__ios_type&amp;)) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ios_type = std::basic_ios&lt;char&gt;]
         operator&lt;&lt;(__ios_type&amp; (*__pf)(__ios_type&amp;))
         ^
  /usr/include/c++/4.8/ostream:117:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘std::basic_ostream&lt;char&gt;::__ios_type&amp; (*)(std::basic_ostream&lt;char&gt;::__ios_type&amp;) {aka std::basic_ios&lt;char&gt;&amp; (*)(std::basic_ios&lt;char&gt;&amp;)}’
  /usr/include/c++/4.8/ostream:127:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::ios_base&amp; (*)(std::ios_base&amp;)) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(ios_base&amp; (*__pf) (ios_base&amp;))
         ^
  /usr/include/c++/4.8/ostream:127:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘std::ios_base&amp; (*)(std::ios_base&amp;)’
  /usr/include/c++/4.8/ostream:166:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(long __n)
         ^
  /usr/include/c++/4.8/ostream:166:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘long int’
  /usr/include/c++/4.8/ostream:170:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long unsigned int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(unsigned long __n)
         ^
  /usr/include/c++/4.8/ostream:170:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘long unsigned int’
  /usr/include/c++/4.8/ostream:174:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(bool) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(bool __n)
         ^
  /usr/include/c++/4.8/ostream:174:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘bool’
  In file included from /usr/include/c++/4.8/ostream:609:0,
                   from /usr/include/c++/4.8/iostream:39,
                   from main2.cpp:1:
  /usr/include/c++/4.8/bits/ostream.tcc:91:5: note: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(short int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]
       basic_ostream&lt;_CharT, _Traits&gt;::
       ^
  /usr/include/c++/4.8/bits/ostream.tcc:91:5: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘short int’
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:181:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(short unsigned int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(unsigned short __n)
         ^
  /usr/include/c++/4.8/ostream:181:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘short unsigned int’
  In file included from /usr/include/c++/4.8/ostream:609:0,
                   from /usr/include/c++/4.8/iostream:39,
                   from main2.cpp:1:
  /usr/include/c++/4.8/bits/ostream.tcc:105:5: note: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]
       basic_ostream&lt;_CharT, _Traits&gt;::
       ^
  /usr/include/c++/4.8/bits/ostream.tcc:105:5: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘int’
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:192:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(unsigned int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(unsigned int __n)
         ^
  /usr/include/c++/4.8/ostream:192:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘unsigned int’
  /usr/include/c++/4.8/ostream:201:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long long int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(long long __n)
         ^
  /usr/include/c++/4.8/ostream:201:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘long long int’
  /usr/include/c++/4.8/ostream:205:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long long unsigned int) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(unsigned long long __n)
         ^
  /usr/include/c++/4.8/ostream:205:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘long long unsigned int’
  /usr/include/c++/4.8/ostream:220:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(double) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(double __f)
         ^
  /usr/include/c++/4.8/ostream:220:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘double’
  /usr/include/c++/4.8/ostream:224:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(float) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(float __f)
         ^
  /usr/include/c++/4.8/ostream:224:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘float’
  /usr/include/c++/4.8/ostream:232:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(long double) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(long double __f)
         ^
  /usr/include/c++/4.8/ostream:232:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘long double’
  /usr/include/c++/4.8/ostream:245:7: note: std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(const void*) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__ostream_type = std::basic_ostream&lt;char&gt;]
         operator&lt;&lt;(const void* __p)
         ^
  /usr/include/c++/4.8/ostream:245:7: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘const void*’
  In file included from /usr/include/c++/4.8/ostream:609:0,
                   from /usr/include/c++/4.8/iostream:39,
                   from main2.cpp:1:
  /usr/include/c++/4.8/bits/ostream.tcc:119:5: note: std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; std::basic_ostream&lt;_CharT, _Traits&gt;::__streambuf_type = std::basic_streambuf&lt;char&gt;]
       basic_ostream&lt;_CharT, _Traits&gt;::
       ^
  /usr/include/c++/4.8/bits/ostream.tcc:119:5: note:   no known conversion for argument 1 from ‘&lt;unresolved overloaded function type&gt;’ to ‘std::basic_ostream&lt;char&gt;::__streambuf_type* {aka std::basic_streambuf&lt;char&gt;*}’
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:548:5: note: template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, const unsigned char*)
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, const unsigned char* __s)
       ^
  /usr/include/c++/4.8/ostream:548:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   cannot convert ‘std::endl’ (type ‘&lt;unresolved overloaded function type&gt;’) to type ‘const unsigned char*’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:543:5: note: template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, const signed char*)
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, const signed char* __s)
       ^
  /usr/include/c++/4.8/ostream:543:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   cannot convert ‘std::endl’ (type ‘&lt;unresolved overloaded function type&gt;’) to type ‘const signed char*’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:530:5: note: template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, const char*)
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, const char* __s)
       ^
  /usr/include/c++/4.8/ostream:530:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   cannot convert ‘std::endl’ (type ‘&lt;unresolved overloaded function type&gt;’) to type ‘const char*’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  In file included from /usr/include/c++/4.8/ostream:609:0,
                   from /usr/include/c++/4.8/iostream:39,
                   from main2.cpp:1:
  /usr/include/c++/4.8/bits/ostream.tcc:321:5: note: template&lt;class _CharT, class _Traits&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, const char*)
       operator&lt;&lt;(basic_ostream&lt;_CharT, _Traits&gt;&amp; __out, const char* __s)
       ^
  /usr/include/c++/4.8/bits/ostream.tcc:321:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   cannot convert ‘std::endl’ (type ‘&lt;unresolved overloaded function type&gt;’) to type ‘const char*’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:513:5: note: template&lt;class _CharT, class _Traits&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, const _CharT*)
       operator&lt;&lt;(basic_ostream&lt;_CharT, _Traits&gt;&amp; __out, const _CharT* __s)
       ^
  /usr/include/c++/4.8/ostream:513:5: note:   substitution of deduced template arguments resulted in errors seen above
  /usr/include/c++/4.8/ostream:493:5: note: template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, unsigned char)
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, unsigned char __c)
       ^
  /usr/include/c++/4.8/ostream:493:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   cannot convert ‘std::endl’ (type ‘&lt;unresolved overloaded function type&gt;’) to type ‘unsigned char’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:488:5: note: template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, signed char)
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, signed char __c)
       ^
  /usr/include/c++/4.8/ostream:488:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   cannot convert ‘std::endl’ (type ‘&lt;unresolved overloaded function type&gt;’) to type ‘signed char’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:482:5: note: template&lt;class _Traits&gt; std::basic_ostream&lt;char, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;char, _Traits&gt;&amp;, char)
       operator&lt;&lt;(basic_ostream&lt;char, _Traits&gt;&amp; __out, char __c)
       ^
  /usr/include/c++/4.8/ostream:482:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   cannot convert ‘std::endl’ (type ‘&lt;unresolved overloaded function type&gt;’) to type ‘char’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:476:5: note: template&lt;class _CharT, class _Traits&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, char)
       operator&lt;&lt;(basic_ostream&lt;_CharT, _Traits&gt;&amp; __out, char __c)
       ^
  /usr/include/c++/4.8/ostream:476:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   cannot convert ‘std::endl’ (type ‘&lt;unresolved overloaded function type&gt;’) to type ‘char’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  In file included from /usr/include/c++/4.8/iostream:39:0,
                   from main2.cpp:1:
  /usr/include/c++/4.8/ostream:471:5: note: template&lt;class _CharT, class _Traits&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, _CharT)
       operator&lt;&lt;(basic_ostream&lt;_CharT, _Traits&gt;&amp; __out, _CharT __c)
       ^
  /usr/include/c++/4.8/ostream:471:5: note:   substitution of deduced template arguments resulted in errors seen above
  In file included from /usr/include/c++/4.8/string:52:0,
                   from /usr/include/c++/4.8/bits/locale_classes.h:40,
                   from /usr/include/c++/4.8/bits/ios_base.h:41,
                   from /usr/include/c++/4.8/ios:42,
                   from /usr/include/c++/4.8/ostream:38,
                   from /usr/include/c++/4.8/iostream:39,
                   from main2.cpp:1:
  /usr/include/c++/4.8/bits/basic_string.h:2753:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;, const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;)
       operator&lt;&lt;(basic_ostream&lt;_CharT, _Traits&gt;&amp; __os,
       ^
  /usr/include/c++/4.8/bits/basic_string.h:2753:5: note:   template argument deduction/substitution failed:
  main2.cpp:10:13: note:   couldn't deduce template parameter ‘_Alloc’
       os &lt;&lt; N &lt;&lt; std::endl;
               ^
  </code></pre>
  
  <p>And when I increase <code>template instantiation depth</code> I get : </p>
  
  <pre><code>$ gcc -Wall -ftemplate-depth=1100  -omain2 main2.cpp
  /tmp/ccI81cmF.o: In function `main':
  main2.cpp:(.text+0x5): undefined reference to `std::cout'
  /tmp/ccI81cmF.o: In function `__static_initialization_and_destruction_0(int, int)':
  main2.cpp:(.text+0x38): undefined reference to `std::ios_base::Init::Init()'
  main2.cpp:(.text+0x47): undefined reference to `std::ios_base::Init::~Init()'
  /tmp/ccI81cmF.o: In function `NumberGeneration&lt;1&gt;::out(std::ostream&amp;)':
  main2.cpp:(.text._ZN16NumberGenerationILi1EE3outERSo[_ZN16NumberGenerationILi1EE3outERSo]+0x19): undefined reference to `std::ostream::operator&lt;&lt;(int)'
  main2.cpp:(.text._ZN16NumberGenerationILi1EE3outERSo[_ZN16NumberGenerationILi1EE3outERSo]+0x1e): undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::endl&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)'
  main2.cpp:(.text._ZN16NumberGenerationILi1EE3outERSo[_ZN16NumberGenerationILi1EE3outERSo]+0x26): undefined reference to `std::ostream::operator&lt;&lt;(std::ostream&amp; (*)(std::ostream&amp;))'
  /tmp/ccI81cmF.o: In function `NumberGeneration&lt;1000&gt;::out(std::ostream&amp;)':
  main2.cpp:(.text._ZN16NumberGenerationILi1000EE3outERSo[_ZN16NumberGenerationILi1000EE3outERSo]+0x25): undefined reference to `std::ostream::operator&lt;&lt;(int)'
  main2.cpp:(.text._ZN16NumberGenerationILi1000EE3outERSo[_ZN16NumberGenerationILi1000EE3outERSo]+0x2a): undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::endl&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)'
  main2.cpp:(.text._ZN16NumberGenerationILi1000EE3outERSo[_ZN16NumberGenerationILi1000EE3outERSo]+0x32): undefined reference to `std::ostream::operator&lt;&lt;(std::ostream&amp; (*)(std::ostream&amp;))'
  </code></pre>