C++ Transform a std::tuple to a std::vector or std::deque

前端 未结 2 1578
不思量自难忘°
不思量自难忘° 2021-02-04 15:57

In the simple parser library I am writing, the results of multiple parsers is combined using std::tuple_cat. But when applying a parser that returns the same result

2条回答
  •  爱一瞬间的悲伤
    2021-02-04 16:14

    Here's one way to do it:

    #include 
    #include 
    #include 
    #include 
    
    template
    auto to_vector_helper(const tuple_type &t, std::index_sequence)
    {
        return std::vector{
            std::get(t)...
                };
    }
    
    template
    auto to_vector(const std::tuple &t)
    {
        typedef typename std::remove_reference::type tuple_type;
    
        constexpr auto s =
            std::tuple_size::value;
    
        return to_vector_helper
            (t, std::make_index_sequence{});
    }
    
    int main()
    {
        std::tuple t{2,3};
    
        std::vector v=to_vector(t);
    
        std::cout << v[0] << ' ' << v[1] << ' ' << v.size() << std::endl;
        return 0;
    }
    

提交回复
热议问题