c++overloadingc++20parameter-pack

Is it possible to define a method overload for each element of a template parameter pack?


Suppose I have a class template with a type template parameter pack, like

template<typename... types>
class Foo {};

Is there any way I could define methods in Foo, say void bar(T x); with overloads for each type T in the parameter pack types?


Solution

  • You can have Foo inherit each base with void bar(T x):

    template<typename T>
    struct FooBase {
      void bar(T x);
    };
    
    template<typename... types>
    struct Foo : FooBase<types>... {
      using FooBase<types>::bar...;
    };