The C++ Programming Language 4th edition, page 225 reads: A compiler may reorder code to improve performance as long as the result is identical to that of the s
At least by my reading, no, this is not allowed. The requirement from the standard is (§1.9/14):
Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.
The degree to which the compiler is free to reorder beyond that is defined by the "as-if" rule (§1.9/1):
This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.
That leaves the question of whether the behavior in question (the output written by cout) is officially observable behavior. The short answer is that yes, it is (§1.9/8):
The least requirements on a conforming implementation are:
[...]
— At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced.
At least as I read it, that means the calls to clock could be rearranged compared to the execution of your long computation if and only if it still produced identical output to executing the calls in order.
If, however, you wanted to take extra steps to ensure correct behavior, you could take advantage of one other provision (also §1.9/8):
— Access to volatile objects are evaluated strictly according to the rules of the abstract machine.
To take advantage of this, you'd modify your code slightly to become something like:
auto volatile t0 = clock();
auto volatile r = veryLongComputation();
auto volatile t1 = clock();
Now, instead of having to base the conclusion on three separate sections of the standard, and still having only a fairly certain answer, we can look at exactly one sentence, and have an absolutely certain answer--with this code, re-ordering uses of clock vs., the long computation is clearly prohibited.