title: Type-dependent constant in template function
body: <p>I want a static array in a templated function whose length depends on the type with which the function is specialized. My first attempt was:</p>
  
  <p>Header:</p>
  
  <pre><code>template&lt;typename T&gt;
  struct Length {
      const static size_t len;
  };
  
  template&lt;typename T&gt;
  void func(){
    static T vars[Length&lt;T&gt;::len]; // len not const. according to compiler!
    // ...
  }
  </code></pre>
  
  <p>Source file:</p>
  
  <pre><code>template&lt;&gt; const size_t Length&lt;double&gt;::len = 2;
  template&lt;&gt; const size_t Length&lt;float&gt;::len = 1;
  // ...
  </code></pre>
  
  <p>However, <code>g++</code> does not compile this and complains</p>
  
  <blockquote>
    <p>error: storage size of ‘vars’ isn’t constant</p>
  </blockquote>
  
  <p>So, what exactly is the problem here? I know that the size of a fixed-length array needs to be a constant and known on compile time, but that seems to be the case here.
  When I write</p>
  
  <pre><code>const size_t len = 2;
  
  void func(){
      static double vars[len];
  }
  </code></pre>
  
  <p>it compiles without problems.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>What is wrong with the code and which alternatives are there for achieving the desired behavior? I would not like to allocate the memory during runtime...</p>