我正在寻找以下代码片段中 L1 行和 L2 行与 l-values
有何不同的解释,即,为什么我会收到:C2105 错误
在 L1 而不是在 L2?
*s = 'a';
printf("%c\n", *s );
//printf("%c\n", ++(*s)++ ); //L1 //error C2105: '++' needs l-value
printf("%c\n", (++(*s))++); //L2
printf("%c\n", (*s) );
注意:我在将代码编译为.cpp 文件时得到了上述结果。现在,在编译为 .c 文件时,我在 L1 和 L2 两行都得到了相同的错误 C2105。为什么 L2 在 C++ 中编译,而不是在 C 中编译是另一个谜:(。
如果有任何帮助,我正在使用 Visual C++ 速成版。
编译器将 ++(*s)++
视为 ++((*s)++)
,因为后递增的优先级高于预递增.在后递增之后,(*s)++
变成了一个 r-value 并且它不能被进一步预递增(这里)。
是的,这不是 UB 的情况(至少在 C 中)。
并阅读此 answer .
对于 C++ 中的 L2 不报错是因为
C++11:5.3.2 递增和递减 说:
The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated). The
operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The result is the updated operand; it is an lvalue, and it is a bit-field if the operand is a bit-field. If x is not of type bool, the expression ++x is equivalent to x+=1.
C++11:5.2.6 递增和递减 说:
The value of a postfix ++ expression is the value of its operand. [ Note: the value obtained is a copy of the original value —end note ] The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a complete object type. The value of the operand object is modified by adding 1 to it, unless the object is of type bool, in which case it is set to true. The value computation of the ++ expression is sequenced before the modification of the operand object. With respect to an indeterminately-sequenced function call, the operation of postfix
++ is a single evaluation. [ Note: Therefore, a function call shall not intervene between the lvalue-to-rvalue conversion and the side effect associated with any single postfix ++ operator. —end note ] The result is a
prvalue. The type of the result is the cv-unqualified version of the type of the operand.
还有MSDN site据称:
The operands to postfix increment and postfix decrement operators must be modifiable (not const) l-values of arithmetic or pointer type. The type of the result is the same as that of the postfix-expression, but it is no longer an l-value.