1
int a()
{
    return 1;
}

int b()
{
    return 2;
}

int c()
{
    return 3;
}

int g(int, int)
{
    return 0;
}

void f(int, int)
{}

int main()
{
    f(g(a(), b()),
      c());
}

I know the evaluation order of function arguments is unspecified as per the C++ standard.

In other words, the actual evaluation order may be:

  1. a(), b(), c()
  2. c(), a(), b()
  3. b(), a(), c()
  4. c(), b(), a()

I just wonder:

Does the C++ stardard guarantee c() will never be called between a() and b()?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • Does this answer your question? [Parameter evaluation order before a function calling in C](https://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c) – Jan Schultke Jun 10 '20 at 07:38
  • 1
    The evaluation of an argument does not overlap the evaluation of another argument in the same call. But the evaluation is not atomic in the usual sense of the word, because expressions evaluated in a different thread can overlap. – rici Jun 10 '20 at 07:49
  • Shouldn't we replace "atomicity" with "order" in the title so that title and question body match? – Evg Jun 10 '20 at 10:11

1 Answers1

3

I guess it is guaranteed since C++17. N4659 (March 2017 post-Kona working draft/C++17 DIS) [intro.execution]/18, reads:

For each function invocation F, for every evaluation A that occurs within F and every evaluation B that does not occur within F but is evaluated on the same thread and as part of the same signal handler (if any), either A is sequenced before B or B is sequenced before A. In other words, function executions do not interleave with each other.

Before C++17 we had no such guarantees.

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 1
    As you write this was introduced in C++17; may I propose replacing the standard link from current working draft (C++2a, WIP) to N4659 (March 2017 post-Kona working draft/C++17 DIS) instead? [N4659: \[intro.execution\]/18](https://timsong-cpp.github.io/cppwp/n4659/intro.execution#18). – dfrib Jun 10 '20 at 08:11