简体   繁体   中英

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.

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

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 ...) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM