stackcpp
stackcpp

Reputation: 1285

sequenced before:the order of arguments of different functions

#include <iostream>

using namespace std;

int f(int){cout << "f ";return 0;}
int g(int){cout << "g ";return 0;}
int a(){cout << "a ";return 0;}
int b(){cout << "b ";return 0;}

int main()
{
    f(a()) + g(b());
    return 0;
}

I konw a is sequenced before f, b is sequenced before g. f and g are unsequenced.

How many results are there?

1.a f b g

2.b g a f

Example 1 and 2 may happen. How about these?

  1. a b f g

  2. a b g f

  3. b a f g

  4. b a g f

Possibly or impossible?

Upvotes: 1

Views: 60

Answers (1)

vsoftco
vsoftco

Reputation: 56547

The only thing that's guaranteed is that b() will be evaluated before g(), and a() before f(). Those are the only 2 ordering relations that are obeyed. So any output compatible with this ordering is possible. In your case, this means that any outputs from 1 to 6 may happen.

Upvotes: 2

Related Questions