home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,306 of 33,346   
   Alf P. Steinbach to ravinder thakur   
   Re: postfix operator++   
   17 May 12 11:45:00   
   
   6246f224   
   From: alf.p.steinbach+usenet@gmail.com   
      
   On 17.05.2012 09:13, ravinder thakur wrote:   
   > { Though this question may seem trivial, it may also be confusing   
   >    and tricky in light of the series of relevant changes in the   
   >    standard, so hopefully the follow-ups will clear up the   
   >    issue. -mod }   
      
   Right.   
      
   > 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 ?   
      
   As already noted umpteen times, it's Undefined Behavior.   
      
   But why?   
      
   Well, in C++03 modifying a value twice between sequence points, such   
   as within a full-expression, was Undefined Behavior.   
      
   However, in C++11 the sequence point idea was ditched in favor of   
   "sequenced before" and "sequenced after":   
      
   C++11 §1.9/13   
      
   /Sequenced before/ is an asymmetric, transitive, pair-wise relation   
   between evaluations executed by a single thread (1.10), which induces   
   a partial order among those evaluations. Given any two evaluations /A/   
   and /B/, if /A/ is sequenced before /B/, then the execution of /A/   
   shall precede the execution of /B/. If /A/ is not sequenced before /B/   
   and /B/ is not sequenced before /A/, then /A/ and /B/ are   
   /unsequenced/.   
   [/Note:/ The execution of unsequenced evaluations can overlap. —/end   
   note/] Evaluations /A/ and /B/ are /indeterminately sequenced/ when   
   either /A/ is sequenced before /B/ or /B/ is sequenced before /A/, but   
   it is unspecified which. [/Note:/ Indeterminately sequenced   
   evaluations cannot overlap, but either could be executed first. —/end   
   note/]   
      
      
   Sometimes computations of values can be sequenced, while side effects   
   such as updating a variable can be unsequenced:   
      
   C++11 §1.9/12   
      
   /Evaluation/ of an expression (or a sub-expression) in general   
   includes both value computations (including determining the identity   
   of an object for glvalue evaluation and fetching a value previously   
   assigned to an object for prvalue evaluation) and initiation of side   
   effects.   
      
      
   Then finally in §1.9/15 comes the C++11 equivalent of the old   
   "modified twice" rule:   
      
   C++11 §1.9/15:   
      
   If a side effect on a scalar object is unsequenced relative to either   
   another side effect on the same scalar object or a value computation   
   using the value of the same scalar object, the behavior is undefined.   
      
      
   So all that's necessary to demonstrate the UB for your case in C++11,   
   is to check out the sequencing of the built-in assignment operator:   
      
   C++11 §5.17/1   
      
   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.   
      
      
   The significant wording here is that which does not appear, namely   
   about sequencing of side effects, such as updates of the variable x in   
      
       x = x++;   
      
   The side effects are not sequenced. Hence §1.9/15 kicks in, and you   
   have UB.   
      
      
   Cheers & hth.,   
      
   - Alf   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca