C++ 是否保证参数评估的原子性?
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());
}
我知道根据 C++ 标准未指定函数参数的计算顺序。
也就是说,实际的求值顺序可能是:
a(), b(), c()
c(), a(), b()
b(), a(), c()
c(), b(), a()
我只是想知道:
C++标准是否保证c()
永远不会在a()
和b()
之间被调用?
我猜它是从 C++17 开始保证的。 N4659(2017 年 3 月 post-科纳工作 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.
在 C++17 之前我们没有这样的保证。
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++标准是否保证c()
永远不会在a()
和b()
之间被调用?
我猜它是从 C++17 开始保证的。 N4659(2017 年 3 月 post-科纳工作 draft/C++17 DIS)[intro.execution]/18,reads:
For each function invocation
F
, for every evaluationA
that occurs withinF
and every evaluationB
that does not occur withinF
but is evaluated on the same thread and as part of the same signal handler (if any), eitherA
is sequenced beforeB
orB
is sequenced beforeA
. In other words, function executions do not interleave with each other.
在 C++17 之前我们没有这样的保证。