简体   繁体   English

为什么postfix operator ++的优先级高于前缀operator ++?

[英]Why does postfix operator++ have higher precedence than prefix operator++?

Defined this way, we can do neither ++x++ nor ++x-- . 这样定义,我们既不能做++x++也不能做++x-- But on the other hand, both (++x)++ and (++x)-- are useful expressions: (++x)++ increments x by two and returns the value "in the middle", while (++x)-- is essentially equivalent to x+1 but completely avoids having to call operator+ , which can be quite useful sometimes. 但另一方面, (++x)++(++x)--都是有用的表达式: (++x)++ x递增2并返回值“在中间”,而(++x)--基本上等于x+1但完全避免必须调用operator+ ,这有时非常有用。

So why is the precedence not defined to have ++x++ automatically expand to (++x)++ rather than ++(x++) ? 那么为什么没有定义优先级让++x++自动扩展为(++x)++而不是++(x++) Is there some hidden meaning to the latter which I don't understand, or is it just to keep the precedence a simple list with all prefix operators making up one single level? 对于后者有一些隐藏的含义,我不明白,或者只是将优先级保持为一个简单的列表,所有前缀运算符组成一个单一的级别?

EDIT Ok, I didn't explicitly say it, but: of course I meant x to be user-defined type. 编辑好吧,我没有明确说出来,但是:当然我的意思是x是用户定义的类型。 For built-in types, (x+=2)-1 is of course better than (++x)++ , and x+1 is a lot better than (++x)-- . 对于内置类型, (x+=2)-1当然优于(++x)++ ,而x+1(++x)--很多 (++x)-- The situation that I have in mind is an iterator to a rather complicated type of semi-associative container, where operators += and + (being designed for random access) have to rebuild a cache in order to work efficiently for general requests, and are therefore an order of magnitude slower than ++ . 我想到的情况是一个相当复杂的半关联容器的迭代器,其中运算符+=+ (设计用于随机访问)必须重建缓存才能有效地处理一般请求,并且因此比++慢一个数量级。 But of course I can modify them to always check first if the argument is a very small integer, and in that case just call operator++ repeatedly instead of doing the random-access procedure. 但是我当然可以修改它们以便总是首先检查参数是否是一个非常小的整数,并且在这种情况下只是反复调用operator++而不是执行随机访问过程。 That should work fine here, though I could imagine I might at some point have a situation in which I want operator+= to always go the random-access way, regardless of how small numbers I present it. 这应该可以正常工作,虽然我可以想象我可能在某种程度上有一种情况,我希望operator+=总是采用随机访问方式,无论我呈现的数字有多小。


So... for me, I would conclude the answer to be: 所以...对我来说,我会得出答案:

the advantage of having a simple and well-memorizeable precedence list in which all postfix operators come before any of the prefix operators is sufficient to tolerate the minor drawback of always having to use parentheses to compose pre- and postfix operators ++ / -- , as this composition is used very seldom. 具有简单且可记忆良好的优先级列表的优点,其中所有后缀运算符在任何前缀运算符之前都足以容忍总是必须使用括号来组成前缀和后缀运算符的小缺点++ / -- ,因为这种成分很少使用。

The simpler "C does it this way", while it seems likely to be the real reason, is far less satisfying in to me, because since ++x++ was not allowed at all in C it would have been possible to redefine this very composition without damaging any existing code. 较简单的“C确实是这样”,而这很可能是真正的原因,是迄今为止对我不太满意,因为自从++x++是不是在C 在所有允许它本来可以重新定义这个非常组成不会损坏任何现有代码。

Anyway, I will go on using (++x)-- , as the parentheses really do not hurt so much. 无论如何,我将继续使用(++x)--因为括号真的没有那么多伤害。

(++x)++ increments x by two and returns the value "in the middle" (++x)++ x递增2并返回“中间”的值

Why not (x += 2) - 1 or (++x, x++) ? 为什么不(x += 2) - 1(++x, x++) Both seem to be clearer. 两者似乎都更清楚。 For scalars, both are well-defined also in C++03, as opposed to your proposed expression. 对于标量,两者在C ++ 03中都有明确的定义,而不是你提出的表达式。


(++x)-- is essentially equivalent to x+1 but completely avoids having to call operator+ , which can be quite useful sometimes. (++x)--基本上等同于x+1但完全避免必须调用operator+ ,这有时非常有用。

That's an arbitrary statement without any explanation. 这是一个没有任何解释的任意陈述。 So I'm going to throw into the pool: 所以我要扔进游泳池:

x+1 is essentially equivalent to (++x)-- but completely avoids having to call operator++ and operator-- which can be useful sometimes. x+1基本上等价于(++x)--但完全避免必须调用operator++operator--这有时很有用。


So why is the precedence not defined to have ++x++ automatically expand to (++x)++ rather than ++(x++) 那么为什么没有定义优先级让++ x ++自动扩展为(++ x)++而不是++(x ++)

Just to make such arcane corner cases not error out? 只是为了让这种神秘的角落案例没有错误? No way. 没门。 Can you please recite man operator for me? 你能为我诵读man operator吗? If you cannot do that, better not try and write ++x++ in your code. 如果你不能这样做,最好不要尝试在你的代码中编写++x++

C++ standard just kept the C rules and obviously those weren't fixed considering operator overloading and idioms yet be invented in a yet to be invented language. C ++标准只是保留了C规则,显然那些并没有被修复,考虑到运算符重载和成语仍然是用一种尚未发明的语言发明的。

Looking at what is available in DM Ritchie Home Page , on see that this precedence is already present in B ( Unary operators are bound right to left. Thus -!x++ is bound -(!(x++)) in Users' Reference to B ) and I didn't see increment operators in BCPL. 看看DM Ritchie主页中可用的内容,看到这个优先级已经存在于B中( 一元运算符从右到左绑定。因此-!x++绑定-(!(x++))用户参考B中 )我没有在BCPL中看到增量运算符。

Both (++x)++ and (++x)-- invoke undefined behaviour [assuming x to be a primitive type]. (++x)++(++x)--调用未定义的行为 [假设x是基本类型]。 For user defined type the scenario would be different . 对于用户定义的类型,场景将是不同的 However it is not generally recommended to use such confusing expressions in your code. 但是,通常不建议在代码中使用这种令人困惑的表达式

As far as the precendence is concerned this answer explains why post increment has higher precedence than pre increment. 就优先权而言, 这个答案解释了为什么后增量优先于预增量。

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

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