Search code examples
c++c++14variadic-templates

Multiple Variadic Parameter Pack for Template Class


I am using variadic parameter packs for policy based class design.

template <APITypes APIType, class... Policies>
class IShader : public Policies... {

};

Policies are defined when called or with defaults if none are specified. The problem comes when I need to add another variadic parameter pack:

template <AttributeType... Attributes, APITypes APIType, class... Policies>
class IShader : public Policies... {

};

This results in the error "Template parameter pack must be the last template parameter". I am planning to use the attribute pack to change the behaviour of at least one of the policies. But I can't work out how to get two variadic parameter packs in one template class.


Solution

  • In the discussion comments you expressed a willingness to consider some kind of indirection, or "a wrapper of some kind for the attribute list".

    A lightweight std::tuple-based wrapper, together with specialization, might work here:

    template <typename attribute_tuple, APITypes APIType,
              typename policy_tuple> class IShader;
    
    template <AttributeType... Attributes, APITypes APIType,
              class... Policies>
    class IShader<std::tuple<Attributes...>, APIType,
                  std::tuple<Policies...>> : public Policies... {
    
    // ...
    
    };
    

    The goal here is to use a template instance along the lines of:

    IShared<std::tuple<Attribute1, Attribute2>, APITypeFoo,
            std::tuple<Policy1, Policy2>> ishared_instance;
    

    And cross your fingers that this is going to match the specialized template declaration, at which point both parameter packs are available for the template specialization to use, individually.