Search code examples
c++c++17fold-expression

C++ unary right fold vs unary left fold with comma operator


Why comma separated unary left fold produce the same result as the right one?

Live code

template<class ...Args>
void right_fold(Args... args){
    ((std::cout << args),...);
}

template<class ...Args>
void left_fold(Args... args){
    (...,(std::cout << args));
}

int main()
{
    right_fold(1,2,3,4);
    std::cout << std::endl;
    left_fold(1,2,3,4);
}

OUTPUT:

1234
1234

Shouldn't it be:

4321
1234

?


Solution

  • Left vs right folding is the difference between ((a,b),c) and (a,(b,c)), but they have exactly the same effect when the built-in , operator is used: they first evaluate a, then b and finally c. To see a difference, you'd need to use a type with a custom overloaded , operator.

    The syntax may look as if it would reverse the arguments, but no, that won't happen.