Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11006

Re: Help with post-increment and sequence points

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Help with post-increment and sequence points
Date 2011-10-20 08:26 -0400
Organization A noiseless patient Spider
Message-ID <j7p41q$lt3$1@dont-email.me> (permalink)
References <26287949.549.1319095405219.JavaMail.geo-discussion-forums@prfp13>

Show all headers | View raw


On 10/20/2011 3:23 AM, laird@lbreyer.com wrote:
> #include<iostream>
> int main() {
>    int i = 1;
>    std::cout<<  i<<  i++<<  i<<  "\n";

This expression actually means
     std::cout.
       operator<<(i).          // #1
         operator<<(i++).      // #2
           operator<<(i).      // #3
             operator<<("\n"); // #4

Which, in reality means

     mem_op<<(        // #4
       mem_op<<(      // #3
         mem_op<<(    // #2
           mem_op<< ( std::cout, i),  // #1
           i++ ), i ), "\n");

which, according to the Standard can have any order of evaluation of its 
operands, except that there is a sequence point before a call to a 
function.  So, before calling #4, it has to evaluate #3, which means it 
will call the function, and in order to do that it has to call #2, and 
in order to do that it has to call #1.  BUT...  The second operands in 
all those calls can be evaluated an *any* time after the expression 
starts evaluating just before the function call that needs it.  IOW, the 
*second* arguments could be evaluated as i,"\n",i,i++, and you would see 
111, which should still be "correct".  Or the order could be 
"\n",i++,i,i, which *could* give 211, or 222, depending on when the 
effects of ++ take place (they are supposed to take place before the 
next "intervening" sequence point).

> }
>
> Compiling this code in g++ prints out 212 . Is this correct? If so, I don't understand why.

It is just as correct as any other combination of 1s and 2s.

>
> Shouldn't this actually be equivalent to
f(f(f(f(std::cout,i),i++),i),"\n") where f is operator<< ? In that case,
each time a function is entered, there should be a sequence point which
makes the side effects happen. So I'd expect the output to be 112 instead.

Yes, but the sequence point has no effect on when the *second* argument 
for every call is evaluated.  You seem to forget that.

>
> % g++ --version
> g++ (Debian 4.6.1-4) 4.6.1
>
> Best regards,
> Laird Breyer

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Help with post-increment and sequence points laird@lbreyer.com - 2011-10-20 00:23 -0700
  Re: Help with post-increment and sequence points Leigh Johnston <leigh@i42.co.uk> - 2011-10-20 12:57 +0100
  Re: Help with post-increment and sequence points softwareEngineer <ovNOSPAM@NOSPAMlibero.it> - 2011-10-20 14:16 +0200
  Re: Help with post-increment and sequence points Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-20 08:26 -0400
    Re: Help with post-increment and sequence points Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-10-20 06:06 -0700
      Re: Help with post-increment and sequence points Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-20 09:50 -0400
    Re: Help with post-increment and sequence points laird@lbreyer.com - 2011-10-20 14:16 -0700
  Re: Help with post-increment and sequence points Markus Wichmann <nullplan@gmx.net> - 2011-10-20 18:09 +0200

csiph-web