With std::min()
, we can std::min(a, b)
. But, what if I want min(a, b, c)
or min(a, b, c, d, e)
? I know the following implementation works:
template <typename T>
const T& min(const T& x, const T& y) {
return x < y ? x : y;
}
template <typename T, typename... Ts>
const T& min(const T& x, const T& y, const Ts&... xs) {
return min(min(x, y), xs...);
}
But I want a single succinct function (provided that it's possible at all, of course). I've tried the following
using std::swap;
template <typename T, typename... Ts>
const T& min(const T& x, const T& y, const Ts&... xs) {
return min(min(x, y), xs...);
}
This does not work with min(1, 2, 3)
. This issue can be solved if I can just import a specific overload of std::swap()
, which unfortunately doesn't seem possible in current C++. So, I'm asking, is there a succinct implementation that achieves what I want with just a single function? Note that I'm considering up to C++14. No C++1z please.