title: Type detection: using variadic arguments to properly implement a function that calculates the mean
body: <p>I am trying to understand how to properly implement a function to calculate the mathematical sample mean, with two initial required characteristics:</p>
  
  <p>1) using variadic arguments.</p>
  
  <p>2) not using two functions to do the job, that is, not using a caller function and then a second function that actually does the calculation.</p>
  
  <p>3) function should be as generic as possible</p>
  
  <p>I am very aware that a pretty similar question has been already asked: <a href="https://stackoverflow.com/questions/25021067/calculate-the-average-of-several-values-using-a-variadic-template-function">Calculate the average of several values using a variadic-template function</a> However, while the accepted answer to that question seems to have taught the OP how to do the small part he didn't know, it presents a code that is actually wrong and does not compile.</p>
  
  <p>So, my own first attempt was along these lines:</p>
  
  <pre><code>template &lt;class... Args&gt;
  double mean(const Args &amp; ... args)
  {
      auto total = 0;
      for (auto value : { ...args })
      {
          total += value;
      }
      return (double)total / sizeof...(args);
  }
  </code></pre>
  
  <p>The problem here is that in the line <code>auto total = 0;</code> the compile naturally can't automatically identify the type that object <code>total</code> should have.</p>
  
  <p>Then, my second attempt:</p>
  
  <pre><code>template &lt;class T, class... Args&gt;
  T mean(const T &amp;t, const Args &amp; ... args)
  {
      T total = 0;
      for (auto value : { args... })
      {
          total += value;
      }
      return (T)(total / sizeof...(args));
  }
  </code></pre>
  
  <p>That version the following problem. It does not work if the caller calls the function with arguments of mixed type, like in <code>mean(1, 2.5)</code>, where the first arguments is automatically detected as an <code>int</code> and the second is detected as a <code>double</code>.</p>
  
  <p>I was able to solve that by doing the following:</p>
  
  <pre><code>template &lt;class T, class... Args&gt;
  T mean(const T &amp;t, const Args &amp; ... args)
  {
      size_t argsSize = sizeof...(args);
      T total = t;
      T arg_array[] = { args... };
      for (size_t i = 0; i&lt; argsSize; i++)
      {
          total += (T)arg_array[i];
      }
      return (T)(total / argsSize) ;
  }
  </code></pre>
  
  <p>This one works even when passed arguments are of different types (of course, provided that such types can be converted to <code>T</code>). The problem now, however, is that the function only works with a minimum of two arguments. If it is called like in <code>mean(3.14)</code>, while it should return 3.14, it actually raises an error because <code>T arg_array[] = { args... }</code> can't be compiled because it is impossible to have a static array be created with size <code>0</code>. Sure, I could substitute it for a dynamic array, but that would make me have to do one memory allocation and one memory deallocation every time that the function is called - which is an unacceptable waste.</p>
  
  <p>So, what would be the correct way to implement such a function that avoids the mentioned problems and that follows my two initial conditions?</p>