这个可以从某个角度进行理解吗,还是标准规定的?
[/Quote]
ISO C99:
6.5.2.4
Semantics
2 The result of the postfix ++ operator is the value of the operand. After the result is obtained, the value of the operand is incremented. (That is, the value 1 of the appropriate type is added to it.) See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The side effect of updating the stored value of the operand shall occur between the previous and the next sequence point.
6.5.2.1
Semantics
2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++E is equivalent to (E+=1). See the discussions of additive operators and compound assignment for information on
constraints, types, side effects, and conversions and the effects of operations on pointers.
关于非lvalue:
ISO C99:
6.5.3.1
1 Anlvalue is an expression with an object type or an incomplete type other than void;53) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure
or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.
53) The name "lvalue" comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object "locator value". What is sometimes called "rvalue" is in this International Standard described as the "value of an expression".
rvalue显然不是lvalue。在ISO C中,rvalue和value等同。因此,value不是lvalue。
未定义行为可以理解为在不同的编译环境下可能会得到不同的结果吗?
[/Quote]
ISO C99:
6.5.16 Assignment operators
Constraints
2 An assignment operator shall have a modifiable lvalue as its left operand.
↑根据这条应该认为非左值作为=的做操作数是错误的(如果不是constraints中的shall被违反,根据Clause 4,程序存在未定义行为,而不是被直接认为是错误的程序导致编译失败)。
具有未定义行为的程序是指程序具有错误的/不兼容的构造或数据,不能预期有合理的行为,即在逻辑上应该是错误的,尽管可以在形式上是正确的(well-formed)ISO C++程序——它可以不违反可诊断语义、语法规则和ODR。(在ISO C中未定义行为的范围更广,包括语言实现;ISO C的那些在constraints之外部分的未定义行为和ISO C++的相当。一个符合标准的编译器在遇到这样的未定义行为时,有权不告诉用户存在编译错误而是崩溃之类,尽管这没什么现实意义。)
具有未指定行为的程序可能是正确的,但也没有在相同的环境下就一定有相同行为的保证。事实上,对于未指定行为,即使是同一个程序片段在同一个环境下,实际都可以不同。
另外,C++03中说2这样的程序产生未指定行为,后来被标准委员会认为是错误的,是标准的bug,被后来的defect report修正,也就是现在的C++11规定的结果。