Why can a variadic class template have at most one parameter pack?

前端 未结 1 1072
面向向阳花
面向向阳花 2021-01-17 00:53

There are moments when wish I could write a class template parameterized by a punctuated list of variadic template parameter packs, e.g.

template

        
相关标签:
1条回答
  • 2021-01-17 01:43

    Variadic template lists cannot be manipulated as first class objects. As a result it is usually more convenient to work with parameter packs wrapped in some template object.

    This is how I would go about passing two lists of types to a template class:

    // create an empty default implementation
    template <typename LeftTuple, typename RightTuple> 
    class tuplePair {};
    
    // specialise to allow tupled lists of types to be passed in
    template <typename ...LHS, typename ...RHS>
    class tuplePair<tuple<LHS...>, tuple<RHS...> > 
    {
       // ...
    };
    
    //or more catholically:
    template <typename ...LHS, typename ...RHS, template<typename...> class tuple_template>
    class tuplePair<tuple_template<LHS...>, tuple_template<RHS...>>  
    {
       // ...
    };
    
    template<typename... X>
    class some_other_tuple {};
    
    
    
    int main() {
       tuplePair<tuple<char,char,char>, tuple<char,char,char>> tango_tuple;
       tuplePair<some_other_tuple<int>, some_other_tuple<char>> other_tuple;
       return 0;
    }
    

    I this formulation clearer than using some sort of deliminator (void) in any case. As a general rule semantics that afford for a list or tuple object rather than simply using deliminators are more powerful (because they allow nesting) and easier to manipulate.

    Addendum:

    I will risk of giving another answer which misses the point, in particular it is not authoritative, in the hope that it might be helpful.

    I have read through the drafts of the variadic template proposal and scanned all 196 threads on comp.std.C++ mentioning the word "variadic" and it seems that the only reason for this limitation is simplicity. In particular simplicity of drafting the standard rather than implementation.

    I couldn't find any discussion of generalization that you present (allowing a parameter pack anywhere in the template parameter list which is not followed by a parameter or parameter pack of the same kind). However other generalizations were discussed, such as allowing a parameter pack expansion to appear in places other than the end of of a template specialization and it looks like time just ran out on these discussions.

    Do you have a persuasive use case? I really don't mean to be the "show me a use case or I will shut you down" bully, I am just interested.

    0 讨论(0)
提交回复
热议问题