Average with small types
<p>I want to write a generic averaging algorithm. That is, for any type T that has an operator+ (T) and an operator/(float), I would like to find the average of T's in a container:</p>

<p>Something like this works fine for float-ish types:</p>

<pre><code>template&lt;typename T&gt;
typename T::value_type RunningAverage(const T&amp; v)
{
  typename T::value_type vectorRunningAverage = 0;

  for(unsigned int i = 0; i &lt; v.size(); ++i)
    {
    vectorRunningAverage = (v[i] + i*vectorRunningAverage)/(i+1);
    }

  return vectorRunningAverage;
}
</code></pre>

<p>I could change</p>

<pre><code>  typename T::value_type vectorRunningAverage = 0;
</code></pre>

<p>to</p>

<pre><code>float vectorRunningAverage = 0;
</code></pre>

<p>and then it would work with a type like unsigned char (can't add more than a few unsigned chars and store the result in an unsigned char without overflow) because it can be implicitly converted to float. However, if I have some more complicated types (say I want to average the components in a <code>std::vector&lt;std::vector&lt;unsigned char&gt; &gt;</code>, this doesn't work. Is there something I'm missing? Or does it just not make sense to do this?</p>
