Search code examples
c++functionargumentsstandards

Does C++ guarantee the atomicity of argument evaluation?


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()?


Solution

  • 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.