Search code examples
c++templatesvariadic-templates

Single template parameter pack for multiple variadic functions?


Usually we would do something like this to define multiple variadic functions taking the same parameter pack:

template<typename ...Pack>
void func1(Pack... params);

template<typename ...Pack>
void func2(Pack... params);

template<typename ...Pack>
void func3(Pack... params);

template<typename ...Pack>
void func4(Pack... params);

Is there some way to avoid this redundant repetition? For example, something like:

template<typename ...Pack>
{
    void func1(Pack... params);
    void func2(Pack... params);
    void func3(Pack... params);
    void func4(Pack... params);
}

Solution

  • Pre C++20 answer: No, there isn't anything you can do to get a syntax like that. Best you could do is create a macro that does a lot of the work for you.

    C++20: You can use auto for the function parameter type which is syntactic sugar for writing out a template like it is for a lambda currently. That would give you

    void func1(auto... params);
    void func2(auto... params);
    void func3(auto... params);
    void func4(auto... params);