gpt4 book ai didi

c++ - 为什么+++x在C++中会被分成++(+x)而不是+(++x)?

转载 作者:行者123 更新时间:2023-12-05 00:42:56 25 4
gpt4 key购买 nike

当我在下面输入这段代码时

int x = 1;
+++x;

它会被分成++(+x),当然这句话是错误的,因为++后面有一个右值。

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

这取决于 IDE 还是编译器?

可以在 C++ 标准中找到吗?还是只是一种未定义的行为?

非常感谢您回答这个问题并原谅我糟糕的英语。

最佳答案

来自 C++20(草案 N4860)[lex.pptoken]/3.3

— Otherwise, the next preprocessing token is the longest sequence of characters that could constitutea preprocessing token, even if that would cause further lexical analysis to fail, ...

[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 correctexpression. —end example]

所以,+ 是语言的规则。与变量一起使用,因为 ++首先被组合在一起。

有趣的是,这让我想起了一个老问题:std::vector<std::vector<int>> a曾经导致问题,因为 >>将是一个标记而不是两个(因为它应该是最长的字符序列)。 [temp.names]/3 解决了这个问题

When a name is considered to be a template-name, and it is followed by a <, the < is always taken as thedelimiter 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 theend of the template-argument-list and completes the template-id. [Note: The second > token produced by thisreplacement rule may terminate an enclosing template-id construct or it may be part of a different construct(e.g., a cast). —end note]

关于c++ - 为什么+++x在C++中会被分成++(+x)而不是+(++x)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71928532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com