为什么X ++有较高的precedence比++ X - [英] Why does x++ have higher precedence than ++x?

查看:136
本文介绍了为什么X ++有较高的precedence比++ X - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是后增量的角度++有较高precedence超过preincrement ++运算符运算符?因此,有一种情况,其中x ++有precedence的同一级别++十大将导致前pression返回一个错误的结果?

What’s the point of post increment ++ operator having higher precedence than preincrement ++ operator? Thus, is there a situation where x++ having same level of precedence as ++x would cause an expression to return a wrong result?

推荐答案

让我们从定义一些术语入手,让我们都在谈论同样的事情。

Let's start with defining some terms, so that we're all talking about the same thing.

运营商后缀X ++和x--,成员访问运算符XY,呼叫运营商F(X),数组引用操作一[ X],而新的typeof,默认情况下,选中,未选中和委托运营商。

The primary operators are postfix "x++" and "x--", the member access operator "x.y", the call operator "f(x)", the array dereference operator "a[x]", and the new, typeof, default, checked, unchecked and delegate operators.

一元运营商+ X,-X,〜X,!X+ X,--x和演员( T)X。

The unary operators are "+x", "-x", "~x", "!x", "++x", "--x" and the cast "(T)x".

主要的运营商都较高precedence比一元运算符的定义。

The primary operators are by definition of higher precedence than the unary operators.

您的问题是

有一个情况,其中x ++有precedence的同一级别++十大将导致前pression返回一个错误的结果?

is there a situation where x++ having same level of precedence as ++x would cause an expression to return a wrong result?

这是不完全清楚我你是什么意思的逻辑的是错误的结果。如果我们改变了precedence规则以这样的方式,一个前pression的值改变,那么新的结果会的正确的结果的。 正确的结果是任何规则的的正确的结果是。这就是我们的确定的正确的结果 - 正确的结果就是你得到什么,当你正确地应用规则。

It is not at all clear to me what you mean logically by "the wrong result". If we changed the rules of precedence in such a way that the value of an expression changed then the new result would be the right result. The right result is whatever the rules say the right result is. That's how we define "the right result" -- the right result is what you get when you correctly apply the rules.

我们尝试建立的规则,使他们的有用的和的可以很容易地前preSS的意思你打算EX preSS 的。那是你所说的错误的结果是什么意思?也就是说,你要问,如果有一种情况,什么是正确的答案是是不正确的人的直觉?

We try to set up the rules so that they are useful and make it easy to express the meaning you intend to express. Is that what you mean by "the wrong result" ? That is, are you asking if there is a situation where one's intuition about what the right answer is would be incorrect?

我向你,如果是这样的话,那么这是不是追求一个有用的角度,因为的有关,几乎没有人的直觉增量运营商正确的操作实际上目前的规范,更不用说一些假设反规格相匹配 的。几乎在每一个C#的书,我已经编辑,笔者有一些微妙或总的方式错误陈述的增量运营商的意义。

I submit to you that if that is the case, then this is not a helpful angle to pursue because almost no one's intuition about the "correct" operation of the increment operators actually matches the current specification, much less some hypothetical counterfactual specification. In almost every C# book I have edited, the author has in some subtle or gross way mis-stated the meaning of the increment operators.

这是不寻常的语义副作用的操作,他们出来的一种语言 - C - 与刻意模糊操作语义。我们已经在C#中的定义,使增量和减量运算明智和严格界定拼命,但不可能拿出东西,使直观的感觉给大家,因为每个人都有使用C和C运营商不同的体验++

These are side-effecting operations with unusual semantics, and they come out of a language - C - with deliberately vague operational semantics. We have tried hard in the definition of C# to make the increment and decrement operators sensible and strictly defined, but it is impossible to come up with something that makes intuitive sense to everyone, since everyone has a different experience with the operators in C and C++.

也许这将是有帮助的,从不同的角度来解决这个问题。你的问题presupposes在Postfix和preFIX ++被指定为具有相同的precedence一个反世界,然后要求该设计选择在世界反批评。但也有可能发生的许多不同的方式。我们可以让他们通过将双双进入主类具有相同的precedence。或者,我们可以让他们通过把他们两人进入一元一类具有相同的precedence。或者,我们可以创造小学和一元之间precedence一个新的水平。或低于一元。以上主要。我们还可以改变运营商的关联的,而不仅仅是他们的precedence。

Perhaps it would be helpful to approach the problem from a different angle. Your question presupposes a counterfactual world in which postfix and prefix ++ are specified to have the same precedence, and then asks for a criticism of that design choice in that counterfactual world. But there are many different ways that could happen. We could make them have the same precedence by putting both into the "primary" category. Or we could make them have the same precedence by putting them both into the "unary" category. Or we could invent a new level of precedence between primary and unary. Or below unary. Or above primary. We could also change the associativity of the operators, not just their precedence.

也许你可以澄清的问题,你想批评到反世界。鉴于其中任何反事实的,我可以给你这样的选择会如何导致code,这是不必要的冗长或混淆,但没有你问对批评的反设计一个清晰的概念批评,我担心我会花很多时间批评低于实际心里有其他的东西。

Perhaps you could clarify the question as to which counterfactual world you'd like to have criticized. Given any of those counterfactuals, I can give you a criticism of how that choice would lead to code that was unnecessarily verbose or confusing, but without a clear concept of the counterfactual design you're asking for criticism on, I worry that I'd spend a lot of time criticising something other than what you actually have in mind.

制作的具体的提出设计变更,我们会看到它的后果。

Make a specific proposed design change, and we'll see what its consequences are.

这篇关于为什么X ++有较高的precedence比++ X - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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