How can I have multiple parameter packs in a variadic template?

后端 未结 4 1581
情书的邮戳
情书的邮戳 2020-11-27 12:55

Function one() accepts one parameter pack. Function two() accepts two. Each pack is constrained to be wrapped in types A and B. Why is it

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 13:26

    Function templates (like skypjack's example) and partial specializations of class and variable templates can have multiple parameter packs if each template parameter subsequent to a template parameter pack either has a default value or can be deduced. The only thing I'd like to add/point out is that for class and variable templates you need a partial specialization. (See: C++ Templates, The Complete Guide, Vandevoorde, Josuttis, Gregor 12.2.4, Second Edition)

    // A template to hold a parameter pack
    template < typename... >
    struct Typelist {};
    
    // Declaration of a template
    template< typename TypeListOne 
            , typename TypeListTwo
            > 
    struct SomeStruct;
    
    // Specialization of template with multiple parameter packs
    template< typename... TypesOne 
            , typename... TypesTwo
            >
    struct SomeStruct< Typelist < TypesOne... >
                     , Typelist < TypesTwo... >
                     >
    {
            // Can use TypesOne... and TypesTwo... how ever
            // you want here. For example:
            typedef std::tuple< TypesOne... > TupleTypeOne;
            typedef std::tuple< TypesTwo... > TupleTypeTwo;
    };      
    

提交回复
热议问题