cpp/language/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.

A template with at least one parameter pack is called a variadic template.

Syntax
Template parameter pack (appears in, and  parameter lists)

Function parameter pack (a form of, appears in a function parameter list of a variadic function template)

Parameter pack expansion (appears in a body of a variadic template)

@1@ A non-type template parameter pack with an optional name @2@ A type template parameter pack with an optional name

@4@ A template template parameter pack with an optional name @5@ A function parameter pack with an optional name @6@ Parameter pack expansion: expands to comma-separated list of zero or more s. Pattern must include at least one parameter pack.

Explanation
A variadic class template can be instantiated with any number of template arguments:

A variadic function template can be called with any number of function arguments (the template arguments are deduced through ):

In a primary class template, the template parameter pack must be the final parameter in the template parameter list. In a function template, the template parameter pack may appear earlier in the list provided that all following parameters can be deduced from the function arguments, or have default arguments:

If every valid specialization of a variadic template requires an empty template parameter pack, the program is ill-formed, no diagnostic required.

Pack expansion
A pattern followed by an ellipsis, in which the name of at least one parameter pack appears at least once, is expanded into zero or more comma-separated instantiations of the pattern, where the name of the parameter pack is replaced by each of the elements from the pack, in order.

If the names of two parameter packs appear in the same pattern, they are expanded simultaneously, and they must have the same length:

If a pack expansion is nested within another pack expansion, the parameter packs that appear inside the innermost pack expansion are expanded by it, and there must be another pack mentioned in the enclosing pack expansion, but not in the innermost one:

Expansion loci
Depending on where the expansion takes place, the resulting comma-separated list is a different kind of list: function parameter list, member initializer list, attribute list, etc. The following is the list of all allowed contexts:

Function argument lists
A pack expansion may appear inside the parentheses of a function call operator, in which case the largest expression or braced-init-list to the left of the ellipsis is the pattern that is expanded:

Formally, the expression-list in a function call expression is classified as initializer-list, and the pattern is the initializer-clause, which is either an assignment-expression or a braced-init-list.

Parenthesized initializers
A pack expansion may appear inside the parentheses of a, a , and other contexts (, , etc.) in which case the rules are identical to the rules for a function call expression above:

Brace-enclosed initializers
In a braced-init-list (brace-enclosed list of initializers and other braced-init-lists, used in and some other contexts), a pack expansion may appear as well:

Template argument lists
Pack expansions can be used anywhere in a template argument list, provided the template has the parameters to match the expansion:

Function parameter list
In a function parameter list, if an ellipsis appears in a parameter declaration (whether it names a function parameter pack (as in, ) or not) the parameter declaration is the pattern:

Note: In the pattern, the ellipsis is the innermost element, not the last element as in all other pack expansions.

Note: is not allowed because the C++11 grammar requires the parenthesized ellipsis to have a name:.

Template parameter list
Pack expansion may appear in a template parameter list:

Base specifiers and member initializer lists
A pack expansion may designate the list of base classes in a. Typically, this also means that the constructor needs to use a pack expansion in the to call the constructors of these bases:

Lambda captures
Pack expansion may appear in the capture clause of a expression:

The sizeof... operator
The operator is classified as a pack expansion as well:

{{rrev|until=c++17|

Dynamic exception specifications
The list of exceptions in a may also be a pack expansion:

}}

Alignment specifier
Pack expansions are allowed in both the lists of types and the lists of expressions used by the keyword.

Attribute list
Pack expansions are allowed in the lists of, if permitted by the attibute's specification. For example:

{{rrev|since=c++17|

Fold-expressions
In, the pattern is the entire subexpression that does not contain an unexpanded parameter pack.

Using-declarations
In, ellipsis may appear in the list of declarators, this is useful when deriving from a parameter pack:

}}

Example
The below example defines a function similar to std, that replace each occurrence of the character in the format string with a value.

The first overload is called when only the format string is passed and there is no parameter expansion.

The second overload contains a separate template parameter for the head of the arguments and a parameter pack, this allows the recursive call to pass only the tail of the parameters until it becomes empty.

is the template parameter pack and is the function parameter pack.