为什么将x分为++ x将被分为++(+ x),而不是+(+(+ x2b; x2b; x)在c++?

发布于 2025-01-22 19:26:36 字数 263 浏览 0 评论 0原文

当我键入此代码时,

int x = 1;
+++x;

它会将其分为++(+X),当然是错误的,因为++之后有一个rvalue。

我很好奇为什么它不能+(++ X),其中代码是正确的。

这取决于IDE还是编译器?

可以在C ++标准中找到吗?还是只是不确定的行为?

非常感谢您回答这个问题,并原谅我的英语不好。

When I type this code bellow

int x = 1;
+++x;

it would be divided into ++(+x), and of course the sentence is wrong cause there's a rvalue after ++.

I am curious about why it can not be +(++x), in which the code is correct.

Is this depend on the IDE or the compiler ?

Can it be find in C++ Standard ? Or it's just a undefined behaviour ?

Thanks a lot to answer this question and forgive my poor English.

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

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

发布评论

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

评论(2

信愁 2025-01-29 19:26:36

来自C ++ 20(草稿N4860)[Lex.pptoken] /3.3

- 否则,下一个预处理令牌是可能构成的最长字符序列
预处理令牌,即使这会导致进一步的词汇分析失败,...

[lex.pptoken]/6

[示例:程序片段x +++++ y y被解析为x ++ +++y,如果x和y具有积分类型,
即使分析X ++ ++ Y可能会产生正确的限制
表达。 - 末尾示例]

因此,+与变量相关,这是语言的规则,因为++首先将其分组在一起。

有趣的是,这让我想起了一个旧问题,其中:std :: vector< std :: vector< int>>用于引起问题的是因为>>将是一个令牌而不是两个(因为它应该是字符的最长序列)。这是由[temp.names]/3解决的

当名称被认为是模板名称时,其次是a< the<总是被视为
模板 - 题词列表的定界符,从来没有作为运营商不到。解析模板argumentList时,
第一个非巢>被视为最终定界符,而不是超过运营商。相似地,
第一个非巢穴>被视为两个连续但独特的>令牌,第一个被视为
模板 - argument-list的结尾并完成模板ID。 [注意:第二>由此产生的令牌
替换规则可能终止封闭模板ID构建体,或者可能是不同构造的一部分
(例如,演员)。 - 末尾注]

From C++20 (draft N4860) [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, ...

and [lex.pptoken]/6

[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]

So, it is a rule of the language, that the + goes with the variable, because the ++ is first grouped together.

Funnily, this reminds me of an old problem where: std::vector<std::vector<int>> a used to cause problems because >> would be one token instead of two (since it's supposed to be the longest sequence of characters). This is addressed by [temp.names]/3

When a name is considered to be a template-name, and it is followed by a <, the < is always taken as the
delimiter of a template-argument-list and never as the less-than operator. When parsing a template-argumentlist,
the first non-nested > is taken as the ending delimiter rather than a greater-than operator. Similarly,
the first non-nested >> is treated as two consecutive but distinct > tokens, the first of which is taken as the
end of the template-argument-list and completes the template-id. [Note: The second > token produced by this
replacement rule may terminate an enclosing template-id construct or it may be part of a different construct
(e.g., a cast). —end note]

绝不服输 2025-01-29 19:26:36

这是最大Munch象征化原理的结果

C ++实现必须将尽可能多的连续字符收集到令牌中。

来自 lex.pptoken#3.3

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

而且由于++是最长的有效令牌,因此解析器将表达式视为++++X

This is a consequence of the maximum munch tokenization principle:

A C++ implementation must collect as many consecutive characters as possible into a token.

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 since ++ is the longest valid token, the parser treats the expression as if ++ +x.

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