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
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