title: Using an int as a template parameter that is not known until run-time
body: <p>I am trying to use an integer as a template parameter for a class. Here is a sample of the code:</p>
  
  <pre><code>template&lt; int array_qty &gt; 
  class sample_class {
  
      public:
          std::array&lt; std::string, array_qty &gt; sample_array;
  
  }
  </code></pre>
  
  <p>If I do so something like this, it works:</p>
  
  <pre><code>sample_class&lt; 10 &gt; sample_class_instance;
  </code></pre>
  
  <p>However, let's say that I do not know the value of array_qty (the template parameter) when compiling, and will only know it during run-time. In this case, I would essentially be passing an int variable as the template argument. For the sake of demonstration, the following code does not work:</p>
  
  <pre><code>int test_var = 2;
  int another_test_var = 5;
  int test_array_qty = test_var * another_test_var;
  
  sample_class&lt; test_array_qty &gt; sample_class_instance;
  </code></pre>
  
  <p>I get the following error during compile time when trying the above:</p>
  
  <pre><code>the value of ‘test_array_qty’ is not usable in a constant expression
  </code></pre>
  
  <p>I've tried converting test_array_qty to a const while passing it as the template parameter, but that doesn't seem to do the trick either. Is there any way to do this, or am I misusing template parameters? Perhaps they need to be known at compile time?</p>
  
  <p>The goal is NOT to solve this specific approach, but rather to find a way to set the length of the array to an int variable that can be stated when instantiating the class. If there is a way to do this via a template parameter, that would be ideal.</p>
  
  <p>Please note that I have to use an array for this, and NOT a vector which I may end up as a suggestion. Additionally, array_qty will always be a value between 0 and 50 - in case that makes a difference.</p>