C++(14):获取类型在tuple中的索引

tuple没有提供方法可以获取某个类型在tuple中的索引,借助模板推到,我们可以自己实现一个:

#include 
#include 

using namespace std;

template
struct get_index_of_element_from_tuple_by_type
{
    static constexpr auto value = N;
};

template
struct get_index_of_element_from_tuple_by_type    //T与tuple中要查找的T匹配
{
    static constexpr auto value = N;
};

template
struct get_index_of_element_from_tuple_by_type    //T与tuple的第一个类型U不匹配,弹出U,N+1,递归调用get_index_of_element_from_tuple_by_type
{
    static constexpr auto value = get_index_of_element_from_tuple_by_type::value;
};

template
T& get_tuple_element_by_type(tuple& t)
{
    return get

你可能感兴趣的:(C/C++,c++)