How to get N-th type from a tuple?

前端 未结 2 1699
慢半拍i
慢半拍i 2020-12-16 12:40

I want to make a template where I can input an index and it will give me the type at that index. I know I can do this with decltype(std::get(tup)) but

2条回答
  •  盖世英雄少女心
    2020-12-16 13:11

    You can use a class template and partial specializations to do what you want. (Note that std::tuple_element does almost the same like the other answer says):

    #include 
    #include 
    
    template 
    struct get;
    
    template 
    struct get>
    {
        using type = typename get>::type;
    };
    
    template 
    struct get<0, std::tuple>
    {
        using type = T;
    };
    
    int main()
    {
        using var = std::tuple;
        using type = get<2, var>::type;
    
        static_assert(std::is_same::value, ""); // works
    }
    

提交回复
热议问题