Folding over a comma

suggest change

It is a common operation to need to perform a particular function over each element in a parameter pack. With C++11, the best we can do is:

template <class... Ts>
void print_all(std::ostream& os, Ts const&... args) {
    using expander = int[];
    (void)expander{0,
        (void(os << args), 0)...
    };
}

But with a fold expression, the above simplifies nicely to:

template <class... Ts>
void print_all(std::ostream& os, Ts const&... args) {
    (void(os << args), ...);
}

No cryptic boilerplate required.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Fold expressions:
* Folding over a comma

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
76 Fold expressions
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype