Transform variadic template parameters to another types

后端 未结 2 529
清酒与你
清酒与你 2020-12-29 09:45

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

For example:

template 
struct single
{
   std::tuple&l         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 10:04

    OK, this might seem a bit like overkill but how about this: As far as I know the only option to "iterate" variadics is using the notation with a template specialization for the simple case.

    Therefore you could try something like this:

    simple case:

    template 
    struct sequences
    {
       std::tuple get(size_t pos)
       {
         return values[pos];
       }
    
       std::vector get_sequence()
       {
          return values;
       }
    
       std::vector values;
    };
    

    recursive case:

    template 
    struct sequences
    {
       std::tuple > get(size_t pos)
       {
         return std::make_tuple(values[pos], remainder->get(pos));
       }
    
      template 
      std::vector<
          typename std::tuple_element>::type
        > get_sequence()
      {
        return get_sequence_internal<
             typename std::tuple_element>::type, Idx
           >();
       }
    
       template 
       std::vector get_sequence_internal()
       {
          return values;
       }
    
       template 
       std::vector get_sequence()
       {
          return remainder->getSequence_internal();
       }
    
    
    
       std::vector values;
       sequences* remainder;
    };
    

    Disclaimer: not tested, not even compiled, but I suppose you get the basic idea. At least two problems remain:

    1. The return value of get() is not your single struct but a tuple chain. Perhaps you can unchain it recursively with std::get<0>...
    2. I don't know if the specialization of get_sequence_internal generates a compile time error because the type of V can differ from T.

提交回复
热议问题