Is there any "tool" in standard, that for such template function:
template<typename FirstArg, typename... Args>
auto average(FirstArg &&firstArg_, Args&&... args_)
{
// example:
std::widest_type_t<FirstArg> sum;
sum += std::forward<FirstArg>(firstArg_);
sum += (... + std::forward<Args>(args_)); // unfold
sum /= (sizeof...(Args) + 1);
return sum;
}
Lets say, that every parameter type in this template is the same. For example: average of n std::int32_t
's. I used imaginary widest_type_t
to visualize the usage. Average calculation needs to sum every parameter, therefore, to avoid (or minimize as best as I can) overflows I need to use maximal-width type possible. Example:
char
->std::intmax_t
std::uint16_t
->std::uintmax_t
float
->long double
(or some other type, decided by implementation)
Surely, I can write this myself, but having something like this in the standard would be nice.
Edit:
I could use moving average, however this function will be used only on small number of parameters (typically 2-8), but types I use are easily "overflowable".
Edit 2:
I also know, that for larger amounts of parameters, it would be better to use any type of array.