Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Insert spaces in (cout << ... << args) using fold expressions?

Given

template<typename ...Types>
void print(Types&& ...args) {
    (cout << ... << args);
}
// ....
print(1, 2, 3, 4); // prints 1234

How to add spaces so we get 1 2 3 4?

Update:

Correct answer: ((std::cout << args << ' ') , ...);

like image 969
Amin Roosta Avatar asked Feb 13 '18 06:02

Amin Roosta


1 Answers

The usual workaround is to fold over the comma operator instead, though the simplistic approach will leave a trailing space:

((std::cout << args << ' '), ...);

Changing it to avoid the trailing space is left as an exercise for the reader.

like image 82
T.C. Avatar answered Nov 11 '22 12:11

T.C.