C++17 fold expression in cout

爱⌒轻易说出口 提交于 2019-12-01 03:42:47

问题


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.


回答1:


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

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



来源:https://stackoverflow.com/questions/39203891/c17-fold-expression-in-cout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!