Home > Software engineering >  Override parameter pack declaration and expansion loci
Override parameter pack declaration and expansion loci

Time:12-17

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?

CodePudding user response:

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...;
  };                       
  • Related