How can I pull variadic template arguments off from the tail instead of the head?

后端 未结 4 1178
离开以前
离开以前 2021-02-04 13:10

For silly reasons I\'ll not go into here, I need the commented out line to work and the line above it it to not work:

template
         


        
4条回答
  •  死守一世寂寞
    2021-02-04 13:24

    Here's a solution: Instead of truncating N from the back, I just truncate sizeof...(Args) - N from the front:

    #include 
    
    /* Concatenator helper */
    
    template  struct cat;
    template 
    struct cat>
    {
      typedef typename std::tuple value;
    };
    
    
    /* Head-of-tuple */
    
    template  struct tuple_head;
    
    // Base case. Need to specialize twice, once for one and once for variadic types
    template 
    struct tuple_head<0, Args...>
    {
      typedef std::tuple<> value;
    };
    template 
    struct tuple_head<0, T>
    {
      typedef std::tuple<> value;
    };
    
    // Recursion step
    template 
    struct tuple_head
    {
      typedef typename cat::value>::value value;
    };
    
    
    /* User interface */
    
    template 
    struct PartialTuple
    {
      typedef typename tuple_head::value type;
    };
    
    
    /* Usage */
    
    #include 
    int main()
    {
      // I want this to not work...
      //PartialTuple<1, std::string, std::string, int, int>::type A{"test", 5, 1};
    
      // I want this to work...
      PartialTuple<1, std::string, std::string, int, int>::type B("test", "test", 5);
      PartialTuple<0, std::string, std::string, int, int>::type C("test", "test", 5, 6);
    }
    

提交回复
热议问题