Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call the idiom of using an array to apply a function to a variadic pack

Here is the idiom in question:

template<typename... T>
void f(T... t) {
    int temp[] = {(g(t), 0)...};
}

This will be compiled as g(t0); g(t1); ..., order of function calls is guaranteed by C++11[dcl.init.list]/4.
A better version uses std::initializer_list instead of array, but it's not important here.

The question is: how should we call this idiom?

Upd:
Basically, it's the idiom which we should advise to people to use it instead of recursion, i.e. to replace two overloads
void f() {}
void f(H head, T... tail) { g(head); f(tail...); }
with single
void f(T... t) { int temp[]{(g(t), 0)...}; }

Of course we can call it "An idiom which will be replaced by the Fold Expressions" but I hope there is a proper term for it.

like image 924
Abyx Avatar asked Nov 09 '22 18:11

Abyx


1 Answers

Pack expansion.

C++11 §5.1.2/23 in [expr.prim.lambda]:

A capture followed by an ellipsis is a pack expansion (14.5.3). [Example:

template<class... Args>
void f(Args... args) {
  auto lm = [&, args...] { return g(args...); };
  lm();
}

—end example ]

I think that covers it. The pack expansion without applying a function can be viewed as one applying an identity function.

C++11 §14.5.3/4 in [temp.variadic]:

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). […]

like image 117
Cheers and hth. - Alf Avatar answered Nov 15 '22 04:11

Cheers and hth. - Alf