C++ sum and averaging functions for arrays and argument sets Here are two sets of variadic C++ functions to do fast summing and averaging of arrays (plain &amp; std::array) or argument sets, (re)written as part of my generic toolbox.

The sum functions are pretty generic, the averaging ones operate on floating point based types only (the integer counterparts are application specific).

Usage example:

std::array&lt;volatile float, 5&gt; arr1 { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f };
volatile float arr2[] { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f };

volatile float res1 = sum(arr1);
volatile float res2 = avg(arr2, sizeof(arr2)/sizeof(arr2[0]));
volatile auto res3 = avg(1.2f, 1.4f, 1.6);


Live demo at: https://godbolt.org/g/DMHv7Y

The sum functions:

#include &lt;cstdint&gt;
#include &lt;utility&gt;
#include &lt;type_traits&gt;
#include &lt;array&gt;

/**
 * Return the sum of all values in the provided `std::array`.
 * @param  arr std::array
 * @return     sum
 */
template &lt;typename T, size_t S&gt;
inline constexpr auto sum(const std::array&lt;T, S&gt; arr) {
    std::remove_cv_t&lt;std::remove_reference_t&lt;T&gt;&gt; t = 0;
    for (const auto v : arr) {
        t += v;
    }
    return t;
}

/**
 * Return the sum of all values in the provided plain array.
 * @param  arr Array
 * @param  l   length
 * @return     sum
 */
template &lt;typename T, typename = typename std::enable_if&lt;std::is_pointer&lt;T&gt;::value, void&gt;::type&gt;
inline constexpr auto sum(const T arr, size_t l) {
    std::remove_cv_t&lt;
        std::remove_reference_t&lt;
            decltype(arr[0])&gt;&gt; t = 0;
    for (size_t i = 0; i &lt; l; i++) {
        t += arr[i];
    }
    return t;
}

/**
 * Return the sum of all values. Tries to use the common type for the return value.
 * @param  ts Arguments to sum
 * @return    sum
 */
template &lt;typename... Ts,
          typename = typename std::enable_if&lt;!std::is_pointer&lt;std::common_type_t&lt;Ts...&gt;&gt;::value, void&gt;::type&gt;
inline constexpr auto sum(Ts&amp;&amp;... ts) {
    std::common_type_t&lt;Ts...&gt; t = 0;
    for (const auto val : std::initializer_list&lt;std::common_type_t&lt;Ts...&gt;&gt;{ts...}) {
        t += val;
    }
    return t;
}


The averaging functions:

#include &lt;cstdint&gt;
#include &lt;utility&gt;
#include &lt;type_traits&gt;
#include &lt;array&gt;

/**
 * Return the average of all values in the provided `std::array`.
 * @param  arr std::array
 * @return     average
 */
template &lt;typename T, size_t S, typename = typename std::enable_if&lt;std::is_floating_point&lt;T&gt;::value, void&gt;::type&gt;
inline constexpr auto avg(const std::array&lt;T, S&gt; arr) {
    std::remove_cv_t&lt;std::remove_reference_t&lt;T&gt;&gt; t = 0;
    for (const auto v : arr) {
        t += v;
    }
    return t / S;
}

/**
 * Return the average of all values in the provided array.
 * @param  arr array
 * @param  l   length
 * @return     average
 */
template &lt;typename T, typename = typename std::enable_if&lt;std::is_pointer&lt;T&gt;::value &amp;&amp; std::is_floating_point&lt;std::remove_pointer_t&lt;T&gt;&gt;::value, void&gt;::type&gt;
inline constexpr auto avg(const T arr, size_t l) {
    std::remove_cv_t&lt;
        std::remove_reference_t&lt;
            decltype(arr[0])&gt;&gt; t = 0;
    for (size_t i = 0; i &lt; l; i++) {
        t += arr[i];
    }
    return t / l;
}

/**
 * Return the average of all arguments. Tries to use the common type for the return value.
 * @param  ts Arguments to sum
 * @return    average
 */
template &lt;typename... Ts,
          typename = typename std::enable_if&lt;!std::is_pointer&lt;std::common_type_t&lt;Ts...&gt;&gt;::value &amp;&amp; std::is_floating_point&lt;std::common_type_t&lt;Ts...&gt;&gt;::value, void&gt;::type&gt;
inline constexpr auto avg(Ts&amp;&amp;... ts) {
    std::common_type_t&lt;Ts...&gt; t = 0;
    for (const auto val : std::initializer_list&lt;std::common_type_t&lt;Ts...&gt;&gt;{ts...}) {
        t += val;
    }
    return t / sizeof...(Ts);
}


The generic patterns here can of course be used to create all kinds of data processing tools. I'd love to know if/how I can improve them.
Different approach:

I'd use iterators and standard library. Using them it becomes really simple:

template &lt;typename ForwardIt, typename T = typename std::iterator_traits&lt;ForwardIt&gt;::value_type&gt;
T average(ForwardIt first, ForwardIt last, T initvalue = {}) //will be 0 for builtins
{
    auto distance = std::distance(first, last);
    auto result = std::accumulate(first, last, initvalue);

    return result / distance;
}


It is also possible to have a distance argument that will be defaulted to what std::distance() produces, but since my use cases are mostly bound to RandomAccessIterators, I don't care, since distance is constant time on random access iterators.

It can be noticed that iterators abstract container type, so they become really easy to use. 

The code actually is part of my library. It is so ancient that I forgot it even exists.

As for sum, it is simply using std::accumulate().

Some more tips:

Standard library has type traits. They help to debug templates. The toolbox should try to provide them as well. 

template &lt;typename T&gt;
struct is_floating_point_array :
                public std::false_type 
{};

template &lt;typename T, std::size_t size&gt;
struct is_floating_point_array&lt;T[size]&gt; :
                public std::bool_constant&lt;std::is_floating_point_v&lt;T&gt;&gt;                    
{};

//one for std::array


Ironically, std::is_array&lt;&gt; won't work on instantiations of std::array, so I recommend writing your own. The above will not only provide easier debugging experience, but will also allow tagged dispatch or if constexpr opportunities. Even more, it will provide concept support, which will make metaprogramming even easier. I didn't directly inherit from std::bool_constant on the first phase since I favored readability to less code height, maybe I was wrong.

Iterators are essential part of standard library. I recommend using them at least in obvious cases. Currently using the code one needs to use std::size() or write one themselves. Pretty much everyone I know doesn't know about std::size(). std::begin() and std::end() (or their member counterparts) destroy opportunity for making the mistakes.