How to construct a tuple from an array

前端 未结 1 704
北海茫月
北海茫月 2020-12-30 16:25

I am designing a C++ library that reads a CSV file of reported data from some experiment and does some aggregation and outputs a pgfplots code. I want to make the library as

相关标签:
1条回答
  • 2020-12-30 16:57

    As with all questions involving std::tuple, use index_sequence to give you a parameter pack to index the array with:

    template <class... Formats, size_t N, size_t... Is>
    std::tuple<Formats...> as_tuple(std::array<char*, N> const& arr,
                                    std::index_sequence<Is...>)
    {
        return std::make_tuple(Formats{arr[Is]}...);
    }
    
    template <class... Formats, size_t N,
              class = std::enable_if_t<(N == sizeof...(Formats))>>
    std::tuple<Formats...> as_tuple(std::array<char*, N> const& arr)
    {
        return as_tuple<Formats...>(arr, std::make_index_sequence<N>{});
    }
    

    Which you would use as:

    std::tuple<Format...> the_line = as_tuple<Format...>(toks);
    
    0 讨论(0)
提交回复
热议问题