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?
Regards
AFAG
#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 );
}
The shift operators are left-associative;
a << b << c
is read as(a << b) << c
, meaning that ifa
is of a type with member user-definedoperator<<
(and returns that type) then the expression reads asa.operator<<(b).operator<<(c)
. If instead a freeoperator<<
is used, then this reads asoperator<<(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 ofb
andc
:If we number the side-effects as above, then the side-effects can be sequenced as any of:
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
andAB12
are both correct. In the following code:1
will appear before2
butA
could appear before or afterB
since the compiler does not promise whether it will evaluatefooA
orfooB
first.The compiler can evaluate the function
printcbase()
as this: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.