title: C++ Fill template array with random values
body: <p>How to fill a <strong>template</strong> array with random values? I want to fill array depending on the type (or char, or int, or double, or float), and with using template functions. And I have the error: C2297 '%': illegal, right operand has type 'T' template.</p>
  <p>My code:</p>
  <pre><code>template &lt;typename T&gt;
  void fillArray(T arr[], int n, T a, T b)
  {
      if (is_same&lt;T, char&gt;::value)
      {
          T c;
          int r, aNo, bNo;
          aNo = (int)a - 97;
          bNo = (int)b - 97;
          for (int i = 0; i &lt; n; i++)
          {
              r = rand() % (bNo - aNo + 1) + aNo;
              arr[i] = (T)r + 97;
          }
      }
      else if (is_same&lt;T, double&gt;::value || is_same&lt;T, float&gt;::value)
      {
          for (int i = 0; i &lt; n; i++)
          {
              T f = (T)rand() / RAND_MAX;
              arr[i] = a + f * (b - a);
          }
      }
      else if (is_same&lt;T, int&gt;::value)
      {
          for (int i = 0; i &lt; n; i++)
          {
              T f = (T) (rand() % b + a);
              arr[i] = f;
          }
      }
  }
  </code></pre>