增量可堆叠吗?即 x++++;或 (x++)++;

Is increment stackable? I.e x++++; or (x++)++;

我和朋友准备考试的时候,朋友说x+++;x+=3;一样

这不是真的,但 x++++;x+=1; 相同还是 (x++)++;?我可以概括一下吗? IE。 x++++++++++++++;((((((x++)++)++)++)++)++)++; 等同于 x+=7;

也许这是完全错误的,++++++x;++(++(++x)); 等同于 x+=3;

它还应该概括为 --x;x--;

您的程序的行为可以使用标准中的以下规则来理解。

来自 lex.pptoken#3.3:

Otherwise, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail, except that a header-name is only formed within a #include directive.

来自 lex.pptoken#5:

[ Example: The program fragment x+++++y is parsed as x ++ ++ + y, which, if x and y have integral types, violates a constraint on increment operators, even though the parse x ++ + ++ y might yield a correct expression.  — end example ]


is x++++; same as x+=1;

使用上面引用的语句,x++++ 将被解析为 x++ ++

但请注意来自 increment/decrement operator's documentation:

The operand expr of a built-in postfix increment or decrement operator must be a modifiable (non-const) lvalue of non-boolean (since C++17) arithmetic type or pointer to completely-defined object type. The result is prvalue copy of the original value of the operand.

这意味着 x++ 的结果将是 prvalue。因此,下一个后缀增量 ++ 不能应用于该纯右值,因为它需要一个 左值 。因此,x++++ 不会编译.

同样,您可以使用上面引用的语句来了解代码段中其他示例的行为。