C++ std::get equivalent with run-time arguments

805 Views Asked by At

One cannot directly use std::get with run-time arguments:

template<typename Tuple>
void get(size_t i, Tuple const& t)
{
     using std::get;
     std::cout<<get<i>(t)<<std::endl;       //error: 'i' is not a constant expression
}

int main()
{
    std::tuple<int,double> t;
    get(1,t);
}

However, one can manually map run-time to compile-time information:

template<typename Tuple>
void get_impl(size_t i, Tuple const& t, typename std::tuple_size<Tuple>::type) {}

template<size_t N, typename Tuple, typename = std::enable_if_t<std::tuple_size<Tuple>::value != N> >
void get_impl(size_t i, Tuple const& t, std::integral_constant<size_t, N>)
{
    if(i==N)
    {
        std::cout<<std::get<N>(t)<<std::endl;
    }
    else
    {
        get_impl(i, t, std::integral_constant<size_t, N+1>());
    }
}

template<typename Tuple>
void get(size_t i, Tuple const& t)
{
    get_impl(i, t, std::integral_constant<size_t, 0>());
}

which seems rather conventient to me (DEMO).

With this, one can apply arbitrary functions (which accept all tuple members) to a tuple-element chosen at run-time.

Is there a reason why this or something similar is not in the standard library? Am I missing something relevant here which hinders the proper application?

0

There are 0 best solutions below