Search code examples
c++templatesvariadic-templates

C++: Explicitly call function overload with param pack


How can I call the overloaded version of the function with a param pack? This is roughly what I would like to do:

void foo(int x=5) {
  // Call foo<Args...>(x) here
}

template <typename... Args>
void foo(int x, Args&&... args) {
}

Is that possible? Or do I need different function names?


Solution

  • You can call the template version by specifying the template arguments explicitly. If there're no template arguments to be specified you can specify empty list. e.g.

    template <typename... Args>
    void foo(int x, Args&&... args) {
    }
    
    void foo(int x=5) {
      foo<>(x);
    }