加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

铸造 – C 17:“折叠”概念中“具有优先权的操作符”是什么意思

发布时间:2020-12-16 03:43:25 所属栏目:百科 来源:网络整理
导读:http://en.cppreference.com/w/cpp/language/fold的网站给出了如何使用折叠概念的例子,它说: NoteIf the expression used as init or as pack has an operator with precedence below cast at the top level,it can be parenthesed:templatetypename ...Arg
http://en.cppreference.com/w/cpp/language/fold的网站给出了如何使用折叠概念的例子,它说:
Note
If the expression used as init or as pack has an operator with precedence    
below cast at the top level,it can be parenthesed:

template<typename ...Args>
int sum(Args&&... args) {
//    return (args + ... + 1 * 2); // Error: operator with precedence below cast
    return (args + ... + (1 * 2)); // OK
}

作为一个非英语母语的人,我不退出这句话:

has an operator with precedence below cast at the top level

它实际意味着什么,它的例子,它表明了什么?
你能帮忙解释一下吗?

非常感谢.

解决方法

转换运算符((Typename)expr)在 C++’s operator precedence rules中具有非常高的优先级.很少有运算符具有更高的优先级.具有强制级别或更高优先级的操作符是非常特殊的操作,通常应用于单个表达式.

在表达式args … 1 * 2中,…适用于左侧和右侧的所有内容.但“右翼”究竟意味着什么呢?它只意味着1部分,还是1 * 2?

对于具有高优先级的运算符,很明显意图是什么.例如,args … func(),很明显()函数调用运算符适用于func而不适用于args … func.如果你这样做((args … func)()),你就不太可能想要后者强制你明确地使用括号.

然而,对于图表中优先级低于3的情况,人们理解的事情会变得多云.因此,C不是使用正常的优先级规则,而是强制用户明确它.你可以有args …(1 * 2)或(args … 1)* 2.但是你必须清楚你想要哪一个.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读