3

考虑为 Java 发布的 SO 问题+++ 运算符如何工作?

嗯,我明白了

  1. 没有像 '+++' 这样的运算符,它只是一个后缀增量,后跟一个中缀添加
  2. 这是对可读性的犯罪

我想知道的(只是为了好奇)如果

+++它只是一个后缀增量,后跟一个中缀添加,而 +++不仅仅是一个中缀添加,后跟一个前缀增量或其未定义的行为

考虑我已经测试了以下程序

#include <iostream>
int main() {
    int x = 1;
    std::cout<< x+++1 << std::endl;
    std::cout<< 1+++x << std::endl;
}

在 VC++、gcc 和 g++ 中,它们都符合以下事实:

'+++' its just a post-fix increment followed by an infix add

并不是

'+++' its just an infix add followed by a prefix increment
4

1 回答 1

15

是的,最大咀嚼规则告诉我们,它+++被解析为++ +(不是后缀后跟中缀,而是后缀后跟+运算符),它也呈现

1+++x  <----> 1++ + x

非法,因为1不是左值。

于 2013-03-13T10:07:44.323 回答