Search code examples
c++c++17variadic-templatesparameter-pack

Override parameter pack declaration and expansion loci


I have a simple interface setter:

template<typename Interface>
struct FrontEnd
{
  virtual void inject(Interface*& ptr, Client* client) = 0;
}

I want to implement these interfaces through parameter packs like this:

template<typename ... Is>
struct BackEnd : public FrontEnd<Is>...
{
   void inject(Is*& ptr, Client* client) override
   {
      ptr = someStuff<Is>.get(client);
   }
};

However this fails with parameter pack not expanded with '...'. I couldn't find something similar to this in cppref. I have no clue where the expansion loci should go (assuming this is even legal, which I'm leaning toward no). Any idea how to provide overrides for every type in a parameter pack?


Solution

  • You probably want something like this

      template<typename Is>             
      struct OneBackEnd : public FrontEnd<Is>
      {                                 
         void inject(Is*& ptr, Client* client) override { /* whatever */ }
      };                                
                                        
      template<typename ... Is>         
      struct BackEnd : public OneBackEnd<Is>...
      {                                 
          using OneBackEnd<Is>::inject...;
      };