这是否被视为 C/C++ 中的未定义行为?
Is this considered undefined behaviour in C/C++?
int x = 2;
int y = 5;
int z = x +++ y;
printf("%d",z);
VC++ 和 GCC 都给出 7 作为输出。我在这里的困惑是,它可能是 x++ + y,或 x + ++y。这是定义了吗?
根据最大咀嚼规则,编译器总是将 x +++ y
解释为 x++ + y
,因此行为定义明确。
C11:6.4 词汇元素:
p(4)
If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.[...]
p(6)
EXAMPLE 2 The program fragment x+++++y
is parsed as x ++ ++ + y
, which violates a constraint on increment operators, even though the parse x ++ + ++ y
might yield a correct expression.
在C和C++中,词法分析的原则都是,将能够组成有效token的最长字符序列取为一个(也称为"maximal munch")。所以 x+++y
被明确解析为 (x++) + y
.
2.4/(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.
int x = 2;
int y = 5;
int z = x +++ y;
printf("%d",z);
VC++ 和 GCC 都给出 7 作为输出。我在这里的困惑是,它可能是 x++ + y,或 x + ++y。这是定义了吗?
根据最大咀嚼规则,编译器总是将 x +++ y
解释为 x++ + y
,因此行为定义明确。
C11:6.4 词汇元素:
p(4)
If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.[...]
p(6)
EXAMPLE 2 The program fragment
x+++++y
is parsed asx ++ ++ + y
, which violates a constraint on increment operators, even though the parsex ++ + ++ y
might yield a correct expression.
在C和C++中,词法分析的原则都是,将能够组成有效token的最长字符序列取为一个(也称为"maximal munch")。所以 x+++y
被明确解析为 (x++) + y
.
2.4/(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.