gpt4 book ai didi

c++ - C++是否保证参数评估的原子性?

转载 作者:行者123 更新时间:2023-12-02 09:53:27 24 4
gpt4 key购买 nike

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());
}

我知道根据C++标准,未指定函数参数的求值顺序。

换句话说,实际评估顺序可能是:
  • a(), b(), c()
  • c(), a(), b()
  • b(), a(), c()
  • c(), b(), a()

  • 我只是好奇:

    C++ stardard是否保证c()a()之间永远不会调用b()

    最佳答案

    我想从C++ 17开始就可以保证。 N4659(2017年3月,科纳后工作草案/ 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.



    在C++ 17之前,我们没有这样的保证。

    关于c++ - C++是否保证参数评估的原子性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62298087/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com