18. Minimum function with any number of arguments

It is possible to write function templates that can take a variable number of arguments using variadic function templates. For this, we need to implement compile-time recursion (which is actually just calls through a set of overloaded functions). The following snippet shows how the requested function could be implemented:

template <typename T>T minimum(T const a, T const b) { return a < b ? a : b; }template <typename T1, typename... T>T1 minimum(T1 a, T... args){   return minimum(a, minimum(args...));}int main(){   auto x = minimum(5, 4, 2, 3);}

In order to be able to use a user-provided binary comparison function, we need to write another function template. The comparison function must be the ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.