简体   繁体   English

为什么x ++的优先级高于++ x?

[英]Why does x++ have higher precedence than ++x?

What's the point of post increment ++ operator having higher precedence than preincrement ++ operator? 后增量++运算符的优先级高于preincrement ++运算符是什么意思? Thus, is there a situation where x++ having same level of precedence as ++x would cause an expression to return a wrong result? 因此,是否存在与++ x具有相同优先级的x ++会导致表达式返回错误结果的情况?

Let's start with defining some terms, so that we're all talking about the same thing. 让我们从定义一些术语开始,这样我们就可以谈论同样的事情了。

The primary operators are postfix "x++" and "x--", the member access operator "xy", the call operator "f(x)", the array dereference operator "a[x]", and the new, typeof, default, checked, unchecked and delegate operators. 主要运算符是后缀“x ++”和“x--”,成员访问运算符“xy”,调用运算符“f(x)”,数组解引用运算符“a [x]”和new,typeof,默认,选中,取消选中和委托运营商。

The unary operators are "+x", "-x", "~x", "!x", "++x", "--x" and the cast "(T)x". 一元运算符是“+ x”,“ - x”,“~x”,“!x”,“++ x”,“ - x”和强制转换“(T)x”。

The primary operators are by definition of higher precedence than the unary operators. 主要运算符定义的优先级高于一元运算符。

Your question is 你的问题是

is there a situation where x++ having same level of precedence as ++x would cause an expression to return a wrong result? 是否存在x ++与++ x具有相同优先级的情况会导致表达式返回错误的结果?

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. 这就是我们如何定义 “正确的结果” - 正确的结果是您在正确应用规则时获得的结果。

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? 也就是说,您是否在询问是否存在一种情况,即对正确答案的直觉是不正确的?

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#书中,作者都​​以一种微妙或粗略的方式错误地指出了增量运算符的含义。

These are side-effecting operations with unusual semantics, and they come out of a language - C - with deliberately vague operational semantics. 这些是具有不寻常语义的副作用操作,它们来自一种语言 - C - 故意模糊的操作语义。 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++. 我们已经在C#的定义中努力使增量和减量运算符合理且严格定义,但是不可能提出对每个人都有直觉意义的东西,因为每个人都对C和C ++中的运算符有不同的体验。 。

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. 我们还可以改变运算符的关联性,而不仅仅是它们的优先级。

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. 做出具体的设计改变,我们将看到它的后果。

John, you have answered the question yourself: these two constructions are mostly used for function calls: ++x - when you want first to increase the value and then call a function, and x++ when you want to call a function, and then make an increase. John,你已经自己回答了这个问题:这两个结构主要用于函数调用: ++x - 当你想先增加值然后调用一个函数时, x++当你想调用一个函数,然后make增加。 That might be very useful, depending on the context. 根据具体情况,这可能非常有用。 Looking at return x++ vs return ++x I see no point for error: the code means exactly how it reads :) The only problem is the programmer, who might use these two constructions without understanding the operator's precedence, and thus missing the meaning. 看看return x++ vs return ++x我认为没有错误点:代码意味着它的读取方式:)唯一的问题是程序员,他可能会使用这两种结构而不理解运算符的优先级,从而忽略了含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM