一元运算符具有关联性有意义吗? [英] Does it make sense for unary operators to be associative?

查看:108
本文介绍了一元运算符具有关联性有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 http://en.cppreference.com/w/cpp/language/operator_precedence的C ++运算符优先级表(我知道这不是规范性的,但是该标准并未讨论优先级或关联性)将一元运算符标记为右/左关联.

The C++ operator precedence table from http://en.cppreference.com/w/cpp/language/operator_precedence (I know it's not normative, but the standard doesn't talk about precedence or associativity) marks unary operators as right/left associative.

在关于另一个问题的讨论中,我充满了疑问.一元运算符具有关联性有意义吗?

From a discussion on a different question, I'm left with doubts. Does it make sense for unary operators to be associative?

推荐答案

这只是从语法派生关联性的一种方法.

It's just an artefact of the way that the associativity is derived from the grammar.

加法是左关联的原因 additive-expression 的产生形式之一是 additive-expression + 乘法表达式,左侧为加法表达式.因此,当您看到:

The reason that addition is left-associative is that one of the productions for additive-expression is additive-expression + multiplicative-expression, with the additive-expression on the left. So when you see:

a + b + c

这必须等效于(a + b) + c,因为匹配产品的唯一方法是将a + b用作 additive-expression ,将c用作 multiplicative-expression . a本身是 additive-expression ,但是b + c不是 multiplicative-expression ,因此,如果我们尝试a + b + c与产品不匹配,将a用作 additive-expression .

this must be equivalent to (a + b) + c, because the only way to match the production is with a + b as the additive-expression and c as the multiplicative-expression. a on its own is an additive-expression, but b + c is not a multiplicative-expression and so a + b + c doesn't match the production if we try to take a as the additive-expression.

如果您以前从未学习过,我建议您通读表达式"一章而忽略其语义:仅查看语法生成.然后,您会看到如何语法定义了优先级和关联性.最大的技巧是每种高优先级"类型的表达式都是IS-A低优先级"类型的表达式.因此,每个 multiplicative-expression 都是一个 additive-expression ,但反之则不然,这就是使乘法比加法绑定更紧密"的原因.

If you haven't before, I recommend that you read through the "Expressions" chapter ignoring the semantics: look only at the grammar productions. Then you'll see just how it is that precedence and associativity are defined by the grammar. The big trick is that every "high-precedence" type of expression IS-A "lower-precedence" type of expression. So every multiplicative-expression is an additive-expression, but not vice-versa, and this is what makes multiplication "bind tighter" than addition.

前缀一元运算符在语法中定义如下: unary-expression :++ cast-expression 依此类推,左侧的运算符用于前缀,等等.后缀的权利.换句话说,我们在左边插入括号"表示后缀,在右边插入括号"表示前缀.就是说,对于后缀运算符,分组是从左到右,对于前缀运算符,分组是从右到左.实际上,C ++标准正好说明了这一点(在C ++ 03中为5.2/1和5.3/1).将这个一元组称为关联性"可能是对术语的滥用或至少是新造币的使用.但这不是主要的,因为很明显这意味着什么.

Prefix unary operators are defined in the grammar like: unary-expression: ++ cast-expression and so on, with the operator on the left for prefix and on the right for postfix. In other words, we "insert the parentheses" on the left for postfix and on the right for prefix. That is, we can say that the grouping is left-to-right for postfix operators and right-to-left for prefix operators. And indeed the C++ standard says exactly that (5.2/1 and 5.3/1 in C++03). It might be an abuse of terminology or at least a new coinage to refer to this unary grouping as "associativity". But it's not a major one since it's obvious what must be meant.

二进制运算符和一元运算符之间唯一的区别是,如果二进制运算符按相反方向分组,则语法仍然有意义,因此a - b - c表示a - (b - c).这将是令人惊讶的,但不会以其他方式影响语言.对于一元运算符,将!!a分组为(!!)a会令人惊讶,该语言还必须为子表达式!!提供意义,而目前它还没有有.功能语言可以赋予其含义:!!可能表示由!!组成的函数,即与static_cast<bool>()相同的操作,但是C ++没有构成函数或运算符的概念. C ++不需要提供该含义的原因是!从右到左分组".哪个(由于语法上的大把戏)只是表示!!并不是语法正确的表达式,因此永远不是任何子表达式的另一种方式.

The only difference here between binary and unary operators is that the syntax would still make sense if the binary operators grouped in the opposite direction, so a - b - c means a - (b - c). It would be surprising but would not otherwise affect the language. With unary operators it would be more than surprising to group !!a as (!!)a, the language would also have to supply a meaning for the sub-expression !!, which currently it doesn't have. A functional language could give it a meaning: !! might mean the function composed from ! and !, i.e. the same operation as static_cast<bool>(), but C++ has no concept of composing functions or operators. The reason C++ doesn't need to supply that meaning is that ! "groups right-to-left". Which (because of the big trick in the grammar) is just another way of saying that !! is not a syntactically correct expression so is never a sub-expression of anything.

是的,说前缀运算符从右到左分组,而后缀运算符从左到右分组确实.但是,由于我们对C ++语言了解的其他情况,也必须这样解决,这也是显而易见的".

So yes, it does make sense to say that prefix operators group right-to-left and postfix operators group left-to-right. But it's also "obvious" that it must be this way around, because of other things we know about the C++ language.

顺便说一句,我认为至少从C ++的角度来讲,后缀++不是 一元运算符.这是一个后缀运算符.但这真的没关系,只是它是标准中的术语,因为显然它是一个运算符并且有一个操作数,所以英语中的一元"也是如此.

Btw, I think that technically speaking in C++ at least, postfix ++ is not a unary operator. It's a postfix operator. But that really doesn't matter except that it's the terminology in the standard, because obviously it is an operator and it has one operand, so is "unary" in English.

这篇关于一元运算符具有关联性有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆