C ++ exception excluding cout printing

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 is it not printed "Output: "? Shouldn't you call operator<<(cout, "Output: ")up to operator<<(cout, f())? If the string is atomic, then how is the postback printed?

+4
source share
2 answers

The order in which the argument is evaluated for <operator is not defined in the C ++ standard. It looks like your compiler first evaluates all the arguments before printing.

+7
source

, operator<<(operator<<(operator<<(cout, "Output:"), f()), endl): , operator<<(cout, "Output:") f() operator<<: .

+2

All Articles