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

发布于 2025-01-22 22:16:33 字数 457 浏览 2 评论 0原文

当我和我的朋友准备考试时,我的朋友说x +++;x+= 3;相同,

这是不正确的,但是IS x ++++; < /代码>与X+= 1;或IS (X ++)++;相同?我可以概括吗?即X +++++++++++++++++;((((((((((((((((((((((((((((((((((x++++))上一x+= 7;

也许是完全错误的,对于++++++ X;++(++(++(++ X))); 等效于x+= 3;

也应该推广到-X; x; and x-;

When me and my friend were preparing for exam, my friend said that x+++; is the same as x+=3;

It is not true but is x++++; same as x+=1; or is (x++)++;? Could I generalize it? I.e. x++++++++++++++; or ((((((x++)++)++)++)++)++)++; is equivalent to x+=7;

Maybe it's completely wrong and it is true for ++++++x; or ++(++(++x)); equivalent to x+=3;

Also it should generalize to --x; and x--;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-01-29 22:16:33

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

来自 lex.pptoken#3.3

否则,下一个预处理令牌是最长的字符序列,它可能构成预处理令牌,即使这会导致进一步的词汇分析失败,只是在内部形成了一个标题名称。 #include指令。

lex.pptoken#5

[示例:程序片段x +++++ y被解析为x ++ +++y,如果x 和y具有积分类型,违反了对增量运算符的约束,即使Parse x ++ + + + ++ y可能会产生正确的表达式。 - 结束示例]



是x +++;与x+= 1;

相同

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

但是请注意,从增量/减少/减少操作员的文档

内置的后缀增量或减少操作员的操作数必须是一个可修改的(non-const) lvalue non-Boolean(因为C ++ 17 )算术类型或指向完全定义的对象类型的指针。 的结果是prvalue 操作数原始值的副本。

这意味着x ++的结果将 prvalue 。因此,下一个Postfix增量++不能应用于该prvalue,因为它需要 lvalue 。因此, x ++++不会编译

同样,您可以使用上述语句来了解摘要中其他示例的行为。

The behavior of your program can be understood using the following rules from the standard.

From 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.

And from 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;

Using the statement quoted above, x++++ will be parsed as x++ ++.

But note that from 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.

That means the result of x++ will a prvalue. Thus the next postfix increment ++ cannot be applied on that prvalue since it requires an lvalue. Hence, x++++ will not compile.

Similarly, you can use the above quoted statements to understand the behavior of other examples in your snippet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文