Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contexts in which pack expansions can occur

There is a clear and comprehensible list of contexts in which a pack expansion can occur on cppreference.com. What I am trying to do is to derive the same information from the standard, partly to gain experience in using the standard. However, I am unable to derive all pack expansion contexts listed on cppreference.com from the standard.

cppreference.com lists, among others, the following four contexts: function argument lists, template argument lists, function parameter lists and template parameter lists.

On the other hand, the standard says (14.5.3.4):

A pack expansion consists of a pattern and an ellipsis, the instantiation of which produces zero or more instantiations of the pattern in a list (described below). The form of the pattern depends on the context in which the expansion occurs. Pack expansions can occur in the following contexts:

  • In a function parameter pack (8.3.5); the pattern is the parameter-declaration without the ellipsis.
  • In a template parameter pack that is a pack expansion (14.1):
    • if the template parameter pack is a parameter-declaration; the pattern is the parameter-declaration without the ellipsis;
    • if the template parameter pack is a type-parameter with a template-parameter-list; the pattern is the corresponding type-parameter without the ellipsis.
  • ...
  • In a template-argument-list (14.3); the pattern is a template-argument.
  • ...

I am unable to find out where the standard says that pack expansion can occur in function argument lists. I suppose this context is somehow covered by one of the three bullet points quoted above.

Just in case it is unclear what I mean by pack expansions in function argument lists, consider the following example:

template <typename ...Args> void f(Args ...args) {}
template <typename ...Args> void g(Args ...args) {
  f(args...); // Pack expansion in a function argument list.
}
like image 732
user1494080 Avatar asked May 08 '17 13:05

user1494080


People also ask

What is Pack expansion?

A pack expansion may designate the list of base classes in a class declaration. Typically, this also means that the constructor needs to use a pack expansion in the member initializer list to call the constructors of these bases: template<class... Mixins> class X : public Mixins...

What are Variadic functions in C?

Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.

Which of the following are valid reasons for using Variadic templates in C++?

Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue.

What is parameter pack in c++?

Parameter packs (C++11) A parameter pack can be a type of parameter for templates. Unlike previous parameters, which can only bind to a single argument, a parameter pack can pack multiple parameters into a single parameter by placing an ellipsis to the left of the parameter name.


1 Answers

A function argument list is syntactically an initializer-list1, so it's covered by [temp.variadic]/(4.4):

In an initializer-list; the pattern is an initializer-clause.


1. The grammar of a function call is postfix-expression ( expression-listopt). And an expression-list is just an initializer-list. ([expr.post])

like image 155
cpplearner Avatar answered Sep 19 '22 18:09

cpplearner