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

≯℡__Kan透↙ 提交于 2019-12-25 16:37:20

问题


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?

来源:https://stackoverflow.com/questions/30828683/c-stdget-equivalent-with-run-time-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!