Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform variadic template parameters to another types

How to transform types from variadic template parameters to another type?

For example:

template <typename... T>
struct single
{
   std::tuple<T...> m_single;
};

template <typename... T>
struct sequences
{
   single<T...> get(size_t pos)
   {
       // I don't know how to convert here
       return std::make_tuple(std::get<0>(m_sequences)[pos]... std::get<N>(m_sequences)[pos]);
   }

   template <size_t Idx>
   std::vector<
      typename std::tuple_element<Idx, std::tuple<T...>>::type
      >
   get_sequence()
   {
      return std::get<Idx>(m_sequences);
   }

   std::tuple<T...> m_sequences; // std::tuple<std::vector<T...>> I don't know how to conver here
};

I want to write so:

sequences<int, double, double> seq;
single<int, double, double> sin = seq.get(10);

And have std::tuple<std::vector<int>, std::vector<double>, std::vector<double>> in struct sequences. And get single from it.

std::vector<single<T...>> is bad idea for me, because i need get one sequence full to and it's easy to copy it from .

Is it possible?

Thank you very much. Sorry for my bad English.

like image 448
Max Avatar asked Mar 08 '11 16:03

Max


1 Answers

You can do more than just expand a variadic parameter pack as a plain list: you can expand an expression too. You can therefore have m_sequences be a tuple of vectors rather than a tuple of the elements:

template <typename... T>
struct sequences
{
   std::tuple<std::vector<T>...> m_sequences;
};

You can also do nifty tricks with parameter packs to pick the appropriate element from the vector:

template<size_t ... Indices> struct indices_holder
{};

template<size_t index_to_add,typename Indices=indices_holder<> >
struct make_indices_impl;

template<size_t index_to_add,size_t...existing_indices>
struct make_indices_impl<index_to_add,indices_holder<existing_indices...> >
{
    typedef typename make_indices_impl<
        index_to_add-1,
        indices_holder<index_to_add-1,existing_indices...> >::type type;
};

template<size_t... existing_indices>
struct make_indices_impl<0,indices_holder<existing_indices...> >
{
    typedef indices_holder<existing_indices...>  type;
};

template<size_t max_index>
typename make_indices_impl<max_index>::type make_indices()
{
    return typename make_indices_impl<max_index>::type();
}



template <typename... T>
struct sequences
{
    std::tuple<std::vector<T>...> m_sequences;

    template<size_t... Indices>
    std::tuple<T...> get_impl(size_t pos,indices_holder<Indices...>)
    {
        return std::make_tuple(std::get<Indices>(m_sequences)[pos]...);
    }

    std::tuple<T...> get(size_t pos)
    {
        return get_impl(pos,make_indices<sizeof...(T)>());
    }
};
like image 190
Anthony Williams Avatar answered Oct 17 '22 20:10

Anthony Williams