Associativity of fold-expressions

后端 未结 2 1301
离开以前
离开以前 2021-02-19 23:11

N4191 proposed fold-expressions to C++. The definition there was that

(args + ...)

is a left-fold (i.e. (((a0 + a1) + a2) + ...),

2条回答
  •  太阳男子
    2021-02-19 23:15

    I can't speak for the proposal, but the new, swapped definitions seem natural to me. My rationale for that is that (... + args) is a sub-expression of a left fold, and (args + ...) is a sub expression of a right fold. In fact, the former is the final segment, and the latter is the initial segment of the expression (I may not be using the correct terminology).

    Here is how I would illustrate the expansion of the fold from the syntax:

    Left fold

                         (... + args)
                 (... + args) + a999)
         (... + args) + a998) + a999)
    
    ((...((a0 + a1) + a2)...) + a999)
    

    Right fold

    (args + ...)
    (a0 + (args + ...)
    (a0 + (a1 + (args + ...)
    
    (a0 + (...(a997 + (a998 + a999))...))
    

提交回复
热议问题