title: Instantiation of array class of size unknown at compile time
body: <p>Below is a small script that prints the sums of the numbers 1 to n, for n = 1,...,20.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;array&gt;
  
  using namespace std;
  
  int test(int n)
  {
      array&lt;int,n&gt; myArray;
  
      for (int iii = 0; iii &lt; n; iii++)
          myArray[iii] = iii+1;
  
      int nSum;
  
      for (int iii = 0; iii &lt; n; iii++)
          nSum += myArray[iii];
  
      return nSum;
  }
  
  
  int main()
  {
      for (int n = 1; n &lt;= 20; n++)
          cout &lt;&lt; test(n) &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Of course, this won't compile:</p>
  
  <pre><code>Main.cpp: In function ‘int test(long unsigned int)’:
  Main.cpp:9:13: error: ‘n’ is not a constant expression
    array&lt;int,n&gt; myArray;
           ^
  Main.cpp:9:13: note: in template argument for type ‘long unsigned int’ 
  Main.cpp:9:22: error: invalid type in declaration before ‘;’ token
    array&lt;int,n&gt; myArray;
                    ^
  Main.cpp:11:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int iii = 0; iii &lt; n; iii++)
                        ^
  Main.cpp:12:14: error: invalid types ‘int[int]’ for array subscript
     myArray[iii] = iii+1;
            ^
  Main.cpp:16:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int iii = 0; iii &lt; n; iii++)
                        ^
  Main.cpp:17:22: error: invalid types ‘int[int]’ for array subscript
     nSum += myArray[iii];
                    ^
  make: *** [Main.o] Error 1
  </code></pre>
  
  <p>The problem seems to be (among other things) that <code>n</code> isn't a constant expression, and I get why that is a problem, but I have no idea how to solve it.</p>
  
  <p>I know how to instantiate "regular" arrays with the new function, but I don't understand how it is possible with the array class.</p>
  
  <p>How do I make the <code>test</code> function treat <code>n</code> as a constant?</p>