So, I have a number of constexpr std::array<int, N> for various values of N. In this case:
constexpr std::array<int, 3> r1 {1, 3, 5};
constexpr std::array<int, 2> r2 {3, 4};
constexpr std::array<int, 4> r3 {1, 2, 5, 6};
constexpr std::array<int, 2> r4 {2, 6};
What I'd like to do is find the constexpr max (and subsequently, min) element across all of the arrays. This seems to work just fine:
constexpr int the_max() {
return 0;
}
template<typename T, typename... Ts>
constexpr int the_max(T&& t, Ts&&... ts) {
const int v = *std::max_element(t.cbegin(), t.cend());
return std::max(v, the_max(ts...));
}
As shown here:
constexpr auto max_entry = dlx::the_max(r1, r2, r3);
std::cout << max_entry << '\n';
which prints 6, as expected.
However, it feels like there should be more logic here, such as:
A default (or minimal) value; and
That the types across the
std::arrayshould be able to be different, as long as they are all arithmetic types.
I feel like this should work:
template<typename B>
constexpr std::enable_if_t<std::is_arithmetic_v<B>, B>
the_max2(B&& b) {
return b;
}
template<typename B, typename T, typename... Ts>
constexpr std::enable_if_t<std::is_arithmetic_v<B> && std::is_arithmetic_v<T::value_type>, std::common_type_t<B, typename T::value_type>>
the_max2(B&& b, T&& t, Ts&&... ts) {
const int v = *std::max_element(t.cbegin(), t.cend());
return std::max(v, the_max2(ts...));
}
but it borks out with:
error: no matching function for call to 'the_max2<int>(int, const std::array<int, 3>&, const std::array<int, 2>&, const std::array<int, 4>&)'
and only expecting 1 parameter but receiving 4, and:
error: 'value_type' is not a member of 'const std::array<int, 3>&'
Anyone tell me what I'm doing wrong? Any help would be really appreciated.