Consider the following program:
#include <iostream>
int f() {
std::cout << 0;
return 1;
}
int main() {
std::cout << f();
}
It outputs the following text:
01
Is this guaranteed by the standard though? The following things happen (not neccesarly in order)
- (A) side effects of
std::cout
(not observable here) - (B) value evaluation of
std::cout
- (C) side effects of
f()
(print0
) - (D) value evaluation of
f()
- (E) side effects of the
<<
operator (print1
) - (F) value evaluation of the
<<
operator
Reading the order of evaluation rules on cppreference I managed to find the following:
- Rule 2) says that (B) and (D) happen before (F).
- Rule 19) (C++17) says that (A) and (B) happen before (C) and (D)
I could imagine, that there is a rule that says the side effects of a function happen before the return of a value, that means (C) happens before (D). I am not a hundred percent sure about where this is explicitely written down though. Maybe rule 11)?
That leaves (E) which I could not find a sequence rule for. Rule 2) explicitely excludes side effects from the sequence order, so:
- What are the rules for (E)?
- Is this guaranteed to be sequenced after the other observable side effect (C)?
Note:
I stumbled on this problem when discussing this recent question, which is related but more about sequencing of chained <<
operators. I wanted to ask for this specific question in a more condensed form and with more of my own reasoning behind it.