Vincent
Vincent

Reputation: 60381

Order of calls and side effects

Consider an operation like this :

int a = f1(mystream)*f2(mystream)+f3(mystream);

Where f1, f2, f3 are of the following form :

int f(std::istream&)

or

int f(std::ostream&)

Do I have the guarantee that f1, f2 and f3 will be executed in that order ?

Upvotes: 1

Views: 111

Answers (3)

Steve
Steve

Reputation: 7271

No, they can be executed in any order. This is because the built-in * and + operators do not introduce a sequence point. Some built-in operators, such as || and && do introduce sequence points and causes the order of execution to be defined.

Upvotes: 0

tzwickl
tzwickl

Reputation: 1371

You don't have the guarantee that every compiler use the order left-right. So if you are not sure you can look it up in your assembly-code. Once the compiler have created the assembly-code the order is guaranteed. Have a look at the following Assembly-Code:

cout << f1() * f2() * f3();
00C6452E  call        f1 (0C61096h)  
00C64533  mov         esi,eax  
00C64535  call        f2 (0C6112Ch)  
00C6453A  imul        esi,eax  
00C6453D  call        f3 (0C61127h) 
00C64542  imul        esi,eax  
00C64545  mov         edi,esp 

This is the code that my compiler creates ...

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477140

No. The individual subexpressions are not sequenced with respect to one another. What is guaranteed is that any one function call completes before another starts, but the order of the three function calls is indeterminate.

Upvotes: 4

Related Questions