How would you write a template function that takes a variable number of homogeneous non-POD function arguments in C++11?
For example suppose we wanted to write a min function for any type that defines the less than “operator<” as follows:
// pseduo-code...
template<class T...>
T min(T x1, T x2, ..., T xn)
{
T lowest = x1;
for (T x : {x2,...,xn})
if (x < lowest)
lowest = x;
return lowest;
}
The above is illegal C++11, how would you write it legally?
Homogeneous? Just use
std::initializer_list.(As @Jesse noted, this is the equivalent to std::min in the standard library.)
If you don’t like the extra braces, make a variadic template that forwards to the initializer list implementation: