In the following code:
#include <iostream>
using namespace std;
int f()
{
throw 1;
}
int main()
{
try
{
cout << "Output: " << f() << endl;
}
catch (int x)
{
cout << x;
}
}
Why isn't "Output: "
printed? Shouldn't the operator<<(cout, "Output: ")
be called before operator<<(cout, f())
? If the line is atomic, how is the printing reversed then?
the order of argument evaluation for the << operator is not defined in the c++ standard. It looks like your compiler evaluates all arguments first, before actually printing.
It may help to think about the actual operator function calls being assembled as operator<<(operator<<(operator<<(cout, "Output:"), f()), endl)
: then you can see that operator<<(cout, "Output:")
and f()
are just two function arguments to another invocation of operator<<
: there's no requirement about which function argument is evaluated first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With