title: Implementation C++14 make_integer_sequence
body: <p>I tried to implement the <a href="http://isocpp.org/blog/2013/05/new-paper-n3690-programming-languages-c-committee-draft">C++14</a> alias template <code>make_integer_sequence</code>, which simplifies the creation of the class template <a href="http://en.cppreference.com/w/cpp/utility/integer_sequence"><code>integer_sequence</code></a>.</p>
  
  <pre><code>template&lt; class T, T... I&gt; struct integer_sequence
  {
      typedef T value_type;
      static constexpr size_t size() noexcept { return sizeof...(I) ; }
  
  };
  
  template&lt; class T, T N&gt;
  using make_integer_sequence = integer_sequence&lt; T, 0,1,2, ... ,N-1 &gt;; // only for illustration.
  </code></pre>
  
  <p>To implement <code>make_integer_sequence</code> we need a helper structure <code>make_helper</code>.</p>
  
  <pre><code>template&lt; class T , class N &gt;
  using make_integer_sequence = typename make_helper&lt;T,N&gt;::type;
  </code></pre>
  
  <p>Implementing <code>make_helper</code> isn't too difficult.</p>
  
  <pre><code>template&lt; class T, T N, T... I &gt;
  struct make_helper
  {
     typedef typename mpl::if_&lt; T(0) == N,  
                    mpl::identity&lt; integer_sequence&lt;T,I...&gt; &gt;,
                    make_helper&lt; T, N-1, N-1,I...&gt; 
                 &gt;::type;
  };
  </code></pre>
  
  <p>To test <code>make_integer_sequence</code> I made this main function:</p>
  
  <pre><code>int main()
  {
      #define GEN(z,n,temp)   \
       typedef make_integer_sequence&lt; int, n &gt;  BOOST_PP_CAT(int_seq,n) ;
  
     BOOST_PP_REPEAT(256, GEN, ~);
  }
  </code></pre>
  
  <p>I compiled the program with GCC 4.8.0, on a quad-core i5 system with 8GBs of RAM.
  Successful compilation took 4 seconds.</p>
  
  <p>But, when I changed the GEN macro to:</p>
  
  <pre><code>int main() {
  
  #define GEN(z,n,temp) \
  typedef make_integer_sequence&lt; int, n * 4 &gt; BOOST_PP_CAT(int_seq, n) ;
  
  BOOST_PP_REPEAT(256, GEN, ~ );
  }
  </code></pre>
  
  <p>The compilation was unsuccessful and outputted the error message:</p>
  
  <blockquote>
    <p>virtual memory exhausted.</p>
  </blockquote>
  
  <p>Could somebody explain this error and what caused it?</p>
  
  <p>EDIT:</p>
  
  <p>I simplified the test to:</p>
  
  <pre><code>int main()
  {
     typedef make_integer_sequence&lt; int, 4096 &gt; int_seq4096;
  }
  </code></pre>
  
  <p>I then successfully compiled with GCC 4.8.0 -ftemplate-depth=65536.</p>
  
  <p>However this second test:</p>
  
  <pre><code>int main()
  {
      typedef make_integer_sequence&lt; int, 16384 &gt; int_seq16384;
  }
  </code></pre>
  
  <p>Did not compile with GCC 4.8.0 -ftemplate-depth=65536, and resulted in the error:</p>
  
  <blockquote>
    <p>virtual memory exhausted. </p>
  </blockquote>
  
  <p>So, my question is, how do I decrease template deep instantiation?</p>
  
  <p>Regards,
  Khurshid.</p>