Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++17 fold expression in cout

I am learning the new c++17 fold expression and I saw this code from c++17 fold expression. I would like to know why this code work :

template<typename ...Args>
void printer(Args&&... args) {
    (std::cout << ... << args) << '\n';
}

but not this one :

template<typename ...Args>
void printer(Args&&... args) {
    (std::cout << args << ...) << '\n';
}

which could seems logic too and would reverse the print order in my opinion.

like image 773
Gabriel de Grimouard Avatar asked Aug 29 '16 10:08

Gabriel de Grimouard


1 Answers

As seen on cppreference, binary folds can have the following two forms:

Screenshot from cppreference/fold

Where E is the pack expression and I is the initialization expression.


There is no binary fold that matches your (std::cout << args << ...), which has the form of (I op E op ...).

like image 88
Vittorio Romeo Avatar answered Nov 18 '22 13:11

Vittorio Romeo