Variadic Templates

Let's learn about variadic templates in detail in this lesson.

We'll cover the following

Variadic Templates

A variadic template is a template that can has an arbitrary number of parameters.

template <typename ... Args>
void variadicTemplate(Args ... args){ . . . . }

Parameter Pack

A template parameter pack is a template parameter that accepts zero or more template arguments (non-types, types, or templates). A function parameter pack is a function parameter that accepts zero or more function arguments.

  • By using the ellipse (…), Args- or args becomes a parameter pack.
  • Args is a template parameter pack; args is a function parameter pack.
  • Parameter packs can only be packed and unpacked.
  • If the ellipsis is left from Args, the parameter pack will be packed and if the ellipse is right from Args, the parameter pack will be unpacked.

The compiler can automatically deduce the template arguments in case of a function template.

For example, the following classes/functions in STL extensively use variadic templates. Variadic Templates are often used in the Standard Template Library:

  • sizeof-Operator, std::tuple, std::thread, std::make_unique, std::lock

The usage of parameter packs obeys a typical pattern for class templates.

  • Perform an operation on the first element of the parameter pack and recursively invoke the operation on the remaining elements.
  • The recursion ends after a finite number of steps.
  • The boundary condition is typically a fully specialized template.

Get hands-on with 1400+ tech skills courses.