1
template<typename T>
void print(T &t) {
    cout << t << " ";
}

template<typename ...Ts>
void gogo(Ts&& ...agvs) {
    int arr[] = {  (print(agvs),0)... };
    cout << "\n";
    for (auto vi:arr)
        cout << vi << " ";
}

template<typename ...Ts>
void go(Ts&& ...agv) {
    gogo(forward<Ts>(agv)...);
}

The following are actual calls

enter image description here

enter image description here

I don't understand this (print(agvs),0)...

Why it works this way?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
oyfz
  • 11
  • 2

2 Answers2

0

This is simply the comma operator. It evaluates the LHS followed by the RHS, and returns the value of the RHS.

Thus

print(agvs),0

first evaluates print(agvs) and then 0. The entire expression evaluates to 0.

The ... is parameter pack expansion. The compiler will insert (print(agvs),0) for each value in agvs where agvs is substituted with the actual value.

idmean
  • 14,540
  • 9
  • 54
  • 83
0

This expression

(print(agvs),0)

is an expression with the comma operator. Its value is the value of the second operand.

From the C++ 14 Standard (5.19 Comma operator)

1 The comma operator groups left-to-right. A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discardedvalue expression (Clause 5).87 Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. The type and value of the result are the type and value of the right operand; the result is of the same value category as its right operand, and is a bit-field if its right operand is a glvalue and a bit-field. If the value of the right operand is a temporary (12.2), the result is that temporary.

So the array is zero-initialized. And the side effect of such an initialization is outputting passed to the function arguments.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335