Multiple Variadic Parameter Pack for Template Class

前端 未结 3 1327
予麋鹿
予麋鹿 2021-02-04 12:44

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

template 
class IShader : public Policies... {

};
         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 13:45

    I think the simplest answer is to create template type wrappers for your parameter packs. For example:

    template 
    struct Attributes {};
    
    template 
    struct Policies {};
    

    Then you can declare your IShader type:

    template 
    class IShader;
    

    Create your implementation as a specialization. Note that in a specialization, you can have multiple parameter pack arguments.

    template 
    class IShader, ApiType, Policies> 
        : public PolicyList...
    {
        ...
    };
    

    Then you can even allow the user to specify the arguments in different orders (make sure you forward the constructors if doing it this way via inheritance):

    template 
    struct IShader, Attributes
        : public IShader, ApiType, Policies>
    {
        using IShader, ApiType, Policies>::IShader;
    };
    

    If you're being really fancy, you can even use metaprogramming tricks to allow the arguments in any order without enumerating all orders. This is left as an exercise to the reader. :)

提交回复
热议问题