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,312 of 33,346   
   Ike Naar to Francis Glassborow   
   Re: postfix operator++   
   18 May 12 14:19:27   
   
   From: ike@sverige.freeshell.org   
      
   On 2012-05-18, Francis Glassborow  wrote:   
   > On 18/05/2012 02:04, Ike Naar wrote:   
   >> On 2012-05-17, ravinder thakur  wrote:   
   >>> 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 ?   
   >>   
   >> (Nit: there is no second assignment. The first line is not an   
   > assignment).   
   >>   
   >> The value of x equals 10 before the assignment.   
   >> If you want x to equal 10 after the assignment as well,   
   >> then you actually want the assignment not to change x.   
   >> So get rid of the assignment. Problem solved.   
   >>   
   >>   
   >   
   > Not so:   
   >   
   > void foo(int & i, int & j){   
   >      i=j++;   
   > }   
   >   
   > int main(){   
   >      int i{10};   
   >      foo(i, i);   
   > // problem occurs   
   > }   
   >   
   >   
   > We cannot simply avoid problem code unless we understand why it is a   
   > problem. And note that any reasonable compiler is going to inline foo()   
   > resulting in EXACTLY the original code.   
      
   No, the same argument applies to your example.   
      
   OP had the following code:   
      
        int i{10};   
        i = i++; // questionable action, intended to establish i == 10   
        // desired postcondition: i == 10   
      
   my advice was to get rid of the second line, because   
      
        int i(10);   
        // desired postcondition: i == 10   
      
   does the same thing.   
      
   You have the following code:   
      
        int i(10);   
        foo(i, i); // another questionable action, intended to establish i   
   == 10   
        // desired postcondition: i == 10   
      
   and, again, my advice is to get rid of the second line, because   
      
        int i(10);   
        // desired postcondition: i == 10   
      
   does the same thing.   
      
      
   --   
         [ 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