Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can function calls be reordered

Tags:

c++

I am using C++98. To what extent can function calls be reordered? I am not using any global state, only state of objects local to the function.

My particular case is:

{
  RaiiType T;
  Object1.FunctionCall();
  Object2.FunctionCall();
}

Where Object1 and Object2 are declared in the next scope up. Is the constructor for T permitted to be reordered after either function call, assuming that it can be trivially proven (at least to a human) that there are no dependencies between the construction and the function calls?


In my particular case, the RAII object is used to time the execution of the function calls.

like image 435
Graznarak Avatar asked Jun 02 '15 21:06

Graznarak


1 Answers

So long as a standards-compliant program could not tell the difference in its observable behavior, the compiler (as well as other components in the system) may freely reorder instructions and operations however it likes.

like image 157
David Schwartz Avatar answered Sep 29 '22 20:09

David Schwartz