gpt4 book ai didi

c++ - 折叠表达式的结合性

转载 作者:可可西里 更新时间:2023-11-01 15:08:53 25 4
gpt4 key购买 nike

N4191向 C++ 提出了折叠表达式。那里的定义是

(args + ...)

是左折叠(即 (((a0 + a1) + a2) + ...),而

(... + args) 

是右折叠(即 (... + (a8 + (a9 + a10)))。但是,修订后的论文 N4295 颠倒了左右一元折叠的定义.

问题:理由是什么?从左到右评估 (args + ...) 似乎更直观(至少当您习惯从左到右的字母表时)。

最佳答案

根据@cpplearner 的评论,这里有一些来自 std-discussion 的考古学

2015 年 2 月 4 日星期三凌晨 1:30,@T.C. wrote :

In N4295, which was actually voted into the standard,

(... op e) is a unary left fold;

(e op ...) is a unary right fold;

In N4191, however,

(e op ...) is called a left fold.

(... op e) is called a right fold.

Why the 180-degree turn?

@RichardSmith 的回答

The form in the original paper was simply a typo. Here are some reasons why the definition that was voted into the standard is the correct one:

  1. In the standard's formulation, (e op ...) has subexpressions of the form (e_i op <stuff>). It does not have subexpressions of the form (<stuff> op e_i). This is consistent with all other pack expansions, where the expansion comprises repeated instances of the pattern.

  2. (e op ... op eN), where eN is a non-pack, must have eN as the innermost operand in order to be useful -- that is, it must be (e1 op (e2 op (e3 op (... op eN)...))), not (...(((e1 op e2) op e3)
    op ...) op eN)
    -- and vice versa for (e0 op ... op e). This allows, for instance, (string() + ... + things) and (std::cout << ... <<
    things)
    to work. For consistency, (e op ...) must also be (e1 op
    (e2 op (...)))
    .

关于c++ - 折叠表达式的结合性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com