How to find the constexpr max of variable number of constexpr std::arrays with a default value

Viewed 515

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:

  1. A default (or minimal) value; and

  2. That the types across the std::array should 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.

2 Answers

Some problems, in no particular order

(1) as pointed by S.M., you've forgotten a typename before T::value_type

template<typename B, typename T, typename... Ts> // .......................VVVVVVVV
constexpr std::enable_if_t<std::is_arithmetic_v<B> && std::is_arithmetic_v<typename T::value_type>, std::common_type_t<B, typename T::value_type>>

(2) you've forgotten b in recursive calling

// .........................V
return std::max(v, the_max2(b, ts...));

(3) you have used int type for v when you should use auto (or typename T::value_type, if you prefer)

// ...VVVV (not int)
const auto v = *std::max_element(t.cbegin(), t.cend());

(4) the "common type" should evaluate also the Ts::value_type's, so

// ...........................................VVVVVVVVVVVVVVVVVVVVVVVVVV
std::common_type_t<B, typename T::value_type, typename Ts::value_type...>

(5) you should explicit the type for std::max().

   using rt = std::common_type_t<B, typename T::value_type, typename Ts::value_type...>;
   // ...
   return std::max<rt>(v, the_max2(b, ts...));
   // ............^^^^

(6) I suggest to receive arguments as const pointers instead of rvalues

 //..........VVVVVVV......VVVVVVV.......VVVVVVV
 the_max2 (B const & b, T const & t, Ts const & ... ts)

The following is a full compiling example (with a simplification to detect only one time the returned common type)

#include <array>
#include <iostream>
#include <algorithm>
#include <type_traits>

template <typename B>
constexpr std::enable_if_t<std::is_arithmetic<B>::value, B>
   the_max2 (B const & b)
 { return b; }


template <typename B, typename T, typename ... Ts,
          typename RT = std::common_type_t<B, typename T::value_type,
                                           typename Ts::value_type...>>
constexpr std::enable_if_t<std::is_arithmetic<B>::value
                        && std::is_arithmetic<typename T::value_type>::value, RT>
   the_max2 (B const & b, T const & t, Ts const & ... ts)
 {
   const auto v = *std::max_element(t.cbegin(), t.cend());
   return std::max<RT>(v, the_max2(b, ts...));
 }

int main()
 {
   constexpr std::array<short, 3> r1 {{1, 3, 5}};
   constexpr std::array<int, 2> r2 {{3, 4}};
   constexpr std::array<long, 4> r3 {{1, 2, 5, 6}};
   constexpr std::array<long long, 2> r4 {{2, 6}};

   auto  m { the_max2(4l, r1, r2, r3, r4) };

   std::cout << m << std::endl;
 }

Bonus suggestion: if you can give up the std::is_arithmetic test, you don't need recursion and you can write your function simply expanding the variadic template as follows

template <typename B, typename ... Ts,
          typename RT = std::common_type_t<B, typename Ts::value_type...>>
constexpr RT the_max3 (B const & b, Ts const & ... ts)
 { return std::max<RT>({b, *std::max_element(ts.cbegin(), ts.cend())...}); }

If you can use C++17, instead of C++14, you can use template folding to recover the std::is_arithmetic SFINAE test as follows

template <typename B, typename ... Ts,
          typename RT = std::common_type_t<B, typename Ts::value_type...>>
constexpr std::enable_if_t<
     (std::is_arithmetic<B>::value && ...
   && std::is_arithmetic<typename Ts::value_type>::value), RT>
   the_max3 (B const & b, Ts const & ... ts)
 { return std::max<RT>({b, *std::max_element(ts.cbegin(), ts.cend())...}); }

You need to apply the std::is_arithmetic trait to the value_type of the passing arguments not to themselves, you also need to remove references from template parameters since you're using forwarding references.

using

namespace impl {
    template <bool... Preds> struct all_dummy;
    template <bool... Preds> using all = std::is_same<all_dummy<Preds...>, all_dummy<((void)Preds, true)...>>;
}

template<typename T, typename... Ts>
constexpr std::enable_if_t<
    impl::all<
        std::is_integral<typename std::remove_reference_t<T>::value_type>::value
    >::value,
    typename std::remove_reference_t<T>::value_type
>
the_max2(T&& t) {
    const int v = *std::max_element(t.cbegin(), t.cend());
    return v;
}


template<typename T, typename... Ts, typename R = 
    std::common_type_t<
            typename std::remove_reference_t<T>::value_type,
            typename std::remove_reference_t<Ts>::value_type...>
>
constexpr std::enable_if_t<
    impl::all<
        std::is_integral<typename std::remove_reference_t<T>::value_type>::value,
        std::is_integral<typename std::remove_reference_t<Ts>::value_type>::value...
    >::value,
    R
>
the_max2(T&& t, Ts&&... ts) {
    const int v = *std::max_element(t.cbegin(), t.cend());
    return std::max<R>(v, the_max2(ts...));
}

if is available the code can be simplified by using if constexpr

namespace impl {
    template <bool... Preds> struct all_dummy;
    template <bool... Preds> using all = std::is_same<all_dummy<Preds...>, all_dummy<((void)Preds, true)...>>;
}


template<typename T, typename... Ts, typename R =
    std::common_type_t<
        typename std::remove_reference_t<T>::value_type,
        typename std::remove_reference_t<Ts>::value_type...>
>
constexpr std::enable_if_t<
    impl::all<
        std::is_integral_v<typename std::remove_reference_t<T>::value_type>,
        std::is_integral_v<typename std::remove_reference_t<Ts>::value_type>...
    >::value,
    R
>
the_max2(T&& t, Ts&&... ts) {
    const int v = *std::max_element(t.cbegin(), t.cend());
    if constexpr (sizeof...(ts) > 0) {
        return std::max<R>(v, the_max2(ts...));
    } else {
        return v;
    }
}
Related