P
publictom
Is the following statement true?
"It is required that the operands to an operator be fully evaluated
before the operator itself is evaluated."
The only way I can think of that it would be practically possible to
violate this would be for subexpression like a += b to return its value
(a + b) for use in the parent expression, but leave it until some time
later before it assigned that value to a. Is this allowed?
If this statement above is true, then the following is defined:
int a = /*something*/, b = /*something else*/;
a ^= b ^= a ^= b;
A couple of people at this page:
http://c2.com/cgi/wiki?ThreeStarProgrammer, have said that the above
expression is undefined because it modifies 'a' twice without an
intervening sequence point. If operands have to be fully evaluated
before the operator can be evaluated, this is not undefined though,
because it is equivalent to (a ^= (b ^= (a ^= b))), in which each
operator with side-effects depends on the result of the next. Is it
undefined or not?
ps. Clearly the order of evaluation of the subexpressions in (a ^= b)
^= (a ^= b) is undefined - I'm certainly not disagreeing with that.
pps. If a and b were of a user-defined type, the ^= operator would be a
function call, and the parameters to a function call have to be
evaluated before the call, so in this case it would be defined.
"It is required that the operands to an operator be fully evaluated
before the operator itself is evaluated."
The only way I can think of that it would be practically possible to
violate this would be for subexpression like a += b to return its value
(a + b) for use in the parent expression, but leave it until some time
later before it assigned that value to a. Is this allowed?
If this statement above is true, then the following is defined:
int a = /*something*/, b = /*something else*/;
a ^= b ^= a ^= b;
A couple of people at this page:
http://c2.com/cgi/wiki?ThreeStarProgrammer, have said that the above
expression is undefined because it modifies 'a' twice without an
intervening sequence point. If operands have to be fully evaluated
before the operator can be evaluated, this is not undefined though,
because it is equivalent to (a ^= (b ^= (a ^= b))), in which each
operator with side-effects depends on the result of the next. Is it
undefined or not?
ps. Clearly the order of evaluation of the subexpressions in (a ^= b)
^= (a ^= b) is undefined - I'm certainly not disagreeing with that.
pps. If a and b were of a user-defined type, the ^= operator would be a
function call, and the parameters to a function call have to be
evaluated before the call, so in this case it would be defined.