12

I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below

    A1B2

While I can see that the output I get is

    BA12

I thought that the call std::cout<< b->fooA() << b->fooB() << std::endl was equivalent to call

  std::cout.operator<<( b->fooA() ).operator<< ( b->fooB() )

but I can see that this is not the case. Can you help me understanding better how this does it work and the relationship with the global operator<<? Is this last ever called in this sequence?

#include <iostream>

struct cbase{
    int fooA(){
        std::cout<<"A";
        return 1;
    }
    int fooB(){
        std::cout <<"B";
        return 2;
    }
};

void printcbase(cbase* b ){
    std::cout << b->fooA() << b->fooB() << std::endl;
}

int main(){
    cbase b;
    printcbase( &b );
}
Salman A
  • 262,204
  • 82
  • 430
  • 521
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
  • 3
    You are correct in thinking that it is equivalent to that. However, you are making the mistaken assumption that there is a well-defined order of evaluation. There isn't. That is all. – R. Martinho Fernandes Feb 11 '13 at 10:14
  • Order of subexpression evaluation is not guaranteed here (and it *has* to be a duplicate, but I can't find a good canonical question right now) – Anton Kovalenko Feb 11 '13 at 10:14
  • `AB12` or `BA12` are possible outputs. `A1B2` is not (hopefully). – jrok Feb 11 '13 at 10:19
  • Not exactly a duplicate but the answers to this question do explain the unspecified order you are experiencing. [What is the correct answer for cout << c++ << c;?](http://stackoverflow.com/questions/10782863/what-is-the-correct-answer-for-cout-c-c) – Alok Save Feb 11 '13 at 10:19
  • 1
    @jrok yes, it is. (assume < means "is before") Every combination where: A < 1 && B < 2 && 1 < 2 is valid. – R. Martinho Fernandes Feb 11 '13 at 10:24

4 Answers4

11

The compiler can evaluate the function printcbase() as this:

void printcbase(cbase* b ){
    int a = b->FooA();    // line 1
    int b = b->FooB();    // line 2
    std::cout << a;       // line 3
    std::cout << b;       // line 4
    stc::cout << std::endl;
}

or some of many permutatins of lines marked as 1 - 4. You are only guaranteed that that the line 1 is done before the line 3, and line 2 before the line 4 (and of course line 3 before line 4). Standard does not say more and indeed you can expect different results with different C++ compilers.

mity
  • 2,299
  • 17
  • 20
7

The order of execution of << is well defined but the order of evaluation of sub-expressions is not defined in C++. This article and the C code example illustrates the problem you mentioned.

BA12 and AB12 are both correct. In the following code:

std::cout<< b->fooA() << b->fooB()

1 will appear before 2 but A could appear before or after B since the compiler does not promise whether it will evaluate fooA or fooB first.

Salman A
  • 262,204
  • 82
  • 430
  • 521
5

The shift operators are left-associative; a << b << c is read as (a << b) << c, meaning that if a is of a type with member user-defined operator<< (and returns that type) then the expression reads as a.operator<<(b).operator<<(c). If instead a free operator<< is used, then this reads as operator<<(operator<<(a, b), c).

So the evaluation of a << b is sequenced before the evaluation of (a << b) << c, but there is no sequencing dependency between the evaluation of b and c:

a << b << c[1]
|         |
a << b[2] |
|    |    c[5]
a[3] b[4]

If we number the side-effects as above, then the side-effects can be sequenced as any of:

54321
53421
45321
43521
43251
35421
34521
34251
ecatmur
  • 152,476
  • 27
  • 293
  • 366
0

Since C++17, from https://en.cppreference.com/w/cpp/language/eval_order

  1. In a shift operator expression E1 << E2 and E1 >> E2, every value computation and side effect of E1 is sequenced before every value computation and side effect of E2

You are guaranteed that:

std::cout << b->fooA() << b->fooB() << std::endl;
// (((std::cout << b->fooA()) << b->fooB()) << std::endl);

is evaluate from left to right, resulting in output A1B2.

For previous version, guaranties on output are only:

  • A before 1
  • B before 2
  • 1 before 2

so one of A1B2, AB12, BA12.

Jarod42
  • 203,559
  • 14
  • 181
  • 302