运行期以索引获取tuple元素-C++11之1

时间:2022-08-23 19:12:26
//运行期以索引获取tuple元素-C++11之1
//需支持C++11及以上标准的编译器,VS2017 15.5.x、CodeBlocks 16.01 gcc 7.2
//参见《深入应用C++11:代码优化与工程级应用》p147,祁宇,机械工业出版社,2015年05
#include <iostream>
#include <tuple>
using namespace std;

template <size_t k, typename Tuple>
typename enable_if<(k == tuple_size<Tuple>::value)>::type
GetArgByIndex(size_t index, Tuple& tp)
{
    throw invalid_argument("arg index out of range");
}

template <size_t k = , typename Tuple>
typename enable_if<(k < tuple_size<Tuple>::value)>::type
GetArgByIndex(size_t index, Tuple& tp)
{
    if (k == index)
    {
        cout << get<k>(tp) << endl;
    }
    else
    {
        GetArgByIndex<k + >(index, tp);
    }
}

int main()
{
    auto tp = make_tuple(, , );
    size_t len = tuple_size<decltype(tp)>::value;

    //打印每个元素
    ; i < len; ++i)
    { GetArgByIndex(i, tp);    }

    ;
}