lundi 6 décembre 2021

Get the tuple max in C++11 [closed]

I would like something like std::numeric_limits<Type>::max(), but which could take a tuple as parameter Type and would return a tuple containing the max of each component.

For example: std::tuple<short, int long> -> <32767, 2147483647, 9223372036854775807>

I tried:

template<typename T, T...>
struct integer_sequence { };

template<std::size_t N, std::size_t... I>
struct gen_indices : gen_indices<(N - 1), (N - 1), I...> { };
template<std::size_t... I>
struct gen_indices<0, I...> : integer_sequence<std::size_t, I...> { };

template <typename ... Ts, std::size_t ... Is>
std::tuple<Ts...> worst_impl(
        integer_sequence<std::size_t, Is...> const &)
{
     return { (std::numeric_limits<Ts>::max())... };
}

template <typename ... Ts>
std::tuple<Ts...> worst()
{
    return worst_impl(gen_indices<sizeof...(Ts)>{});
}

But I get the error:

error: cannot convert 'std::tuple<std::tuple<short int, long int, long int, long int, long int> >' to 'const std::tuple<short int, long int, long int, long int, long int>&'

Is it possible to get this in C++11?

Aucun commentaire:

Enregistrer un commentaire