Am 17.05.2012 09:13, schrieb ravinder thakur:
> hello guys,
>
> I am kind of puzzled by one simple c++ constuct. What should be the
> value of x after the statement:
>
> int x = 10;
> x = x++;
>
> I expect value of x to be 10 after the second assignment, however
> its coming out to be 11. Any ideas whats happening ?
>
In an expression "a = b", if "b" is a value (like "one" or "two"), we
will just take that value and write it into what "a" computed to. If
"b" just refers to an object, we will first have to read a value from
that object, and then write that value into what "a" computed to. The
write into "a" happens *after* the computation of the result of the
expressions "a" and "b" and *before* the computation of the result of
the entire expression "a = b". (The result computation (which is
formally called "value computation" in the Standard) can be an lvalue
refering to an object or a prvalue that is just a value that is not
associated with any object).
Your code snippet has undefined behavior once you run this. The
intuitive reason for this is that there is no "dependency" on the
result of the expression "x++" and the variable "x". Expression "x++"
is the value "x" has before "x" is incremented, and "x++" increments
"x".
So the "x = x++", which assigns to "x" the value of what appears on
the right has no order with respect to the increment that "x++" does -
only with respect to the computation of the value of expression
"x++". The increment and the assign to and of "x" may happen
"overlapping". "x" may end up with a value of 42 or your compiler
could crash with "undefined behavior detected" at runtime - this is
entirely depending on your implementation.
A comparison with a similar construct: If you change "x++" to "++x",
the behavior becomes defined in C++11:
int x = 10;
x = ++x;
Now there is an intuitive "dependency" on the result of "++x" and the
variable "x": The expression "++x" is an lvalue and results in
referring to the variable "x". "++x" is defined as
x = x + 1
So the entire thing becomes
x = (x = x + 1)
Taking the above rules for "a = b", we notice that the left
assignment-write happens after result computation of the right
expression. But the result computation of that right expression
happens in turn after its assignment-write. And that in turn happens
after the result computation of "x + 1". In this code snippet, we have
no overlapping writes (in Standardese, "overlapping writes" will be
two side effects that are neither "indeterminately sequenced" nor will
one be "sequenced before" the other).
This change was a deliberate change in C++11; it was the result of
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#637 .
(Note that the Standards use of "value computation" does not refer to
computing a "value". The term "value computation" refers to computing
either an lvalue, prvalue or xvalue. A "value" is what meaning is
contained by the bits in an object, while a lvalue, prvalue or xvalue
is an expression's result when analyzed. The Standard also has the
more precise notions of "lvalue computation" for determining what an
lvalue identifies and "prvalue computation" for what the actual value
of an expression is).