为什么在递增后具有较高的precedence超过preincrement? [英] Why does the postincrement has a higher precedence than preincrement?

查看:112
本文介绍了为什么在递增后具有较高的precedence超过preincrement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到这个问题

我只是想知道:当我们做一个 X ++ ++ X 的增量,我们知道< STRONG> preincrement ++ X 第一在EX pression。我们知道,还有,那后递增 X ++ 执行过去在EX pression。在previous问题一直没有得到被问这个问题的答案。

现在的问题是:


  

为什么/如何进行递增后有 precedence比
  preincrement,虽然preincrement执行第一


您可以看到C ++ 这里的precedence表。这表明该职位比pre较高。

例如:

INT X = 5;
随着后:


  X = X ++; // X = 5


使用pre:

  X = + X; // X = 6


解决方案

  

为什么递增后具有较高的precedence超过preincrement


简短的回答:从语法如下。它无关的计算顺序 - 除了 || &功放;&安培; (),运营商(和他们的precedence)不会影响评价的顺序。

稍长的答案

在C ++后缀/ preFIX运营商的系统是这样的:我们有1类后缀运算符(的() [ ] .X - &GT; X + + - )和语法一个类preFIX操作符(单目操作符: * - + ++ ! - ++ - )。解析规则说基本上是

  preFIX运营商标识后缀运营商

被解析为

  preFIX运营商(标识后缀运营商)

这是合理的考虑作为例子 * F() + P [1] 和这样的。

[当然,后缀/ preFIX运营商并不需要它们之间的任何precedence, X-&GT; Y ++ 以及 ++ * X 总是毫不含糊。

如果你想以某种方式从后缀运算符分隔 ++ 并使其少绑定,你会


  • 更改现有code,如之意。 * X ++ 将解析为(* X)++ ,而不是 *(X ++) 因为它没有今天。

  • 请语言一团糟(你必须扫描后增量,然后跳转到preFIX运营商,然后回如 * X-方式&gt; Y-GT; Z ++

不知道为什么会是无论如何需要。

I have seen this question.

I just wanted to know: while we make a x++ or ++x increments, we know that preincrement ++x is executed first in the expression. We know, as well, that postincrement x++ is executed last in the expression. The previous question hasn't got an answer for this question being asked.

The question is:

WHY / HOW does the postincrement has higher precedence than the preincrement, although the preincrement is executed first?

You can see the precedence table for C++ here. It shows that the post is higher than pre.

Example:

int x = 5; With post:

x = x++; // x=5

With pre:

x = ++x; // x = 6

解决方案

WHY does the postincrement has higher precedence than the preincrement

Short answer: it follows from the grammar. It has nothing to do with the order of evaluation - apart from ,, ||, &&, ?: and (), operators (and their precedence) don't affect order of evaluation.

Slightly longer answer

The system of postfix/prefix operators in C++ goes like this: we have 1 class of postfix operators ((), [], .x, ->x, ++, --) and one class of prefix operators (unary operators in the grammar: *, -, +, ++, --, !, ~, ++, --). The parsing rules say basically that

prefix-operators identifier postfix-operators

is parsed as

prefix-operators (identifier postfix-operators)

This is reasonable considering examples as *f(), ++p[1] and such.

[Naturally, postfix/prefix operators don't need any precedence between them, x->y++ as well as ++*x are always unambiguous].

If you wanted to somehow separate ++ from the postfix operators and make it less binding, you would

  • change the meaning of existing code, eg. *x++ would parse as (*x)++, not as *(x++) as it does today.
  • make the language a mess (you would have to scan for postincrement, then jump to prefix operators, then back in eg. *x->y->z++)

Not sure why would it be needed anyway.

这篇关于为什么在递增后具有较高的precedence超过preincrement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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