Prev: Fast Assignment of POD Struct Whose Members Have Copy Constructors
Next: Variadic Templates – Recursion – Initializer Lists.
From: Anubhav on 9 Dec 2009 03:52 Hi, 5.17/1 mentions "In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression." This statement is not clear to me. Consider the code below: int a = 0, b = 0, c = 0; a = b + c; The steps in the evaluation of the expression �a = b + c� are 1. Value computation of �a�, which is the Lvalue evaluation to determine the target memory location for assignment (side effect) 2. Value computation of �b� which involves Lvalue to Rvalue conversion of the Lvalue expression �b�. There is no side effect in this subexpression 3. Value computation of �c� which involves Lvalue to Rvalue conversion of the Lvalue expression �c�. There is no side effect in this subexpression 4. Calling operator+ with the values of operands calculated in Steps 2 and 3 above. The Rvalue result of this subexpression is the right operand of the assignment operator. There is no side effect of this subexpression 5. Call to the built in assignment operator with the Rvalue expression computed in Step 4. The side effect of this is the atual modification of the memory location corresponding to �a�. 6. Value computation of the Lvalue expression �a� which is the result or the return value of the full expression; also called the assignment expression. Q1. Is my understanding correct? "In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression." To me, this means that a) 1 will be done before 5 (value computation of left operand of operator=) b) 4 will be done before 5 (value computation of right operand of operator=) c) 2 and 3 will be done before 4 (value computation of operator+) in an unsequenced manner What is not clear to me is the part "and before the value computation of the assignment expression.". Q2. Does it means that d) 5 will be done before 6? Can some one explain this? Regards, Dabs. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Goran on 9 Dec 2009 17:26 On Dec 9, 9:52 pm, Anubhav <rkld...(a)gmail.com> wrote: > Hi, > > 5.17/1 mentions "In all cases, the assignment is sequenced after the > value computation of the right and left operands, and before the value > computation of the assignment expression." > > This statement is not clear to me. Consider the code below: > > int a = 0, b = 0, c = 0; > a = b + c; > > The steps in the evaluation of the expression �a = b + c� are > 1. Value computation of �a�, which is the Lvalue evaluation to > determine the target memory location for assignment (side effect) > 2. Value computation of �b� which involves Lvalue to Rvalue conversion > of the Lvalue expression �b�. There is no side effect in this > subexpression > 3. Value computation of �c� which involves Lvalue to Rvalue conversion > of the Lvalue expression �c�. There is no side effect in this > subexpression > 4. Calling operator+ with the values of operands calculated in Steps 2 > and 3 above. The Rvalue result of this subexpression is the right > operand of the assignment operator. There is no side effect of this > subexpression > 5. Call to the built in assignment operator with the Rvalue expression > computed in Step 4. The side effect of this is the atual modification > of the memory location corresponding to �a�. > 6. Value computation of the Lvalue expression �a� which is the result > or the return value of the full expression; also called the assignment > expression. > > Q1. Is my understanding correct? I would say yes. > > "In all cases, the assignment is sequenced after the value computation > of the right and left operands, and before the value computation of > the assignment expression." > > To me, this means that > > a) 1 will be done before 5 (value computation of left operand of > operator=) > b) 4 will be done before 5 (value computation of right operand of > operator=) > c) 2 and 3 will be done before 4 (value computation of operator+) in > an unsequenced manner > > What is not clear to me is the part "and before the value computation > of the assignment expression.". > > Q2. Does it means that > d) 5 will be done before 6? Yes. > > Can some one explain this? I honestly think that you both understood and explained that without a hitch. Goran. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Frank Birbacher on 11 Dec 2009 03:16 Hi! Anubhav schrieb: > What is not clear to me is the part "and before the value computation > of the assignment expression.". > > Q2. Does it means that > d) 5 will be done before 6? In an expression like int a=0, b=1, c=2; a = (b = c); It means that you cannot have the evaluation of (b=c) return the value of b before the assignment "b=c". Instead the assignment "b=c" has to be carried out ("sequenced") before the new value for a is determined. It means a cannot receive the value "1", but has to receive the value "2". Frank -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Anubhav on 12 Dec 2009 16:22
> It means that you cannot have the evaluation of (b=c) return the value > of b before the assignment "b=c". Instead the assignment "b=c" has to be > carried out ("sequenced") before the new value for a is determined. > > It means a cannot receive the value "1", but has to receive the value "2". > void f(int &x) { // < --- Sequence Point here S1 ++x; // Line 2 } // < --- Sequence Point here S2 int main(){ int y = 0; char buf[10]; buf[y] = f(y); // Line 1 } I believe that this code has an unspecified behavior rather than undefined behavior. It is not undefined behavior because the scalar 'y' is updated with an intervening sequence point. However this code has an unspecified behavior because 'y' can be read before it is updated in 'f' or vice-versa. So the output of this code is a limited set of options i.e. either buf[0] is updated with 1, or buf[1] is updated with 1. Is my understanding correct? Dabs -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |