“之前测序”和“调用函数中的每个评估”在c ++ [英] "sequenced before" and "Every evaluation in the calling function" in c++

查看:140
本文介绍了“之前测序”和“调用函数中的每个评估”在c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用函数(包括其他函数调用)中的每个评估在被调用函数的主体执行之前或之后不被另外特别地排序,相对于被调用函数的执行不确定地排序。



换句话说,函数执行不会相互交错。



每个评估的含义是什么。 / p>

  #include< iostream> 
using namespace std;
int a = 0;
ing b = 0;
int f(int,int)
{
cout< call f,
cout<< a =<< a<< ,;
cout<< b =<< b<< ,;
return 1;
}

int g()
{
cout< call g,
cout<< a =<< a<< ,;
cout<< b =<< b<< ,;
return 1;
}

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




  1. 表达式 f(a ++,b ++),因此在执行g之前或之后对a ++,b ++的评估和f的执行都进行排序。



    在这种情况下,有两种结果。如果在执行g:



    调用f,a = 1,b = 1之前对表达式f(a ++,b ++)如果在评估表达式f(a ++,b ++)之前执行g,则调用g,a = 1,b = 1,



    调用g,a = 0,b = 0,调用f,a = 1,b = 1, / p>


2.这意味着评估++,评估b ++或执行f。因此,可以在执行g之前对a ++的求值进行排序,对b ++的求值可以在执行g之后对f的执行进行排序。



<

 调用g,a = 1,b = 0,调用f,a = 1,b = 1,
/ pre>


  1. 这是指价值计算或副作用。

$ a
$ b

因此,a ++的值计算可以在执行g之前排序,a ++的副作用,b ++的计算和f的执行可以在执行g之后排序。

 调用g,a = 0,b = 0,调用f,a = 1,b = 1,



在这种情况下,

  b ++)+(a = g()); 
1. avalue计算a ++
2.执行g
3.side的效果a ++
4.(a = 0)
的效果5。评估b ++
6.执行f

调用g,a = 0,b = 0,调用f,a = 0,b = 1,

哪一个是正确的?或其他答案?



我不是英语人士,而且英语不好。



我希望你能理解我的意思



f(h1(),h2())+ g(h3(),h4())



h1和h2在f,h3和h4在g之前排序之前进行排序。



可能:


  1. h1


  2. h4

    b $ b
  3. h2


  4. f


  5. p>


  6. g



解决方案> ... 表达式在操作数对象的修改之前被排序。对于不确定顺序的函数调用,后缀
++ 的操作是单次评估。因此,函数调用不应介入左值 - 右值转换和与任何单个后缀 ++ 相关联的副作用。 -end note ] ...

我的看法是,<$ c在执行 f 的正文之前必须完成$ c> a ++ 和 b ++ 因此 f 必须是 a == 1 b == 1



调用 g 的顺序不确定调用 f ,因此可以观察到预增量或后增量值。


Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.

In other words, function executions do not interleave with each other.

what is the meaning of "Every evaluation".

#include <iostream>
using namespace std;
int a = 0;
ing b = 0;
int f(int,int)
{
    cout << "call f, ";
    cout << "a=" << a << ", ";
    cout << "b=" << b << ", ";
    return 1;
}

int g()
{
    cout << "call g, ";
    cout << "a=" << a << ", ";
    cout << "b=" << b << ", ";
    return 1;
}

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

  1. it menas evaluation of function call expression f(a++, b++), so evaluation of a++, evaluation of b++, and execution of f are all sequenced before or after the execution of g.

    In this case, there are two kinds of results. If evaluation of expression f(a++, b++) is sequenced before the execution of g:

    call f, a=1, b=1, call g, a=1, b=1,

    If execution of g issequenced before evaluation of expression f(a++, b++):

    call g, a=0, b=0, call f, a=1, b=1,

2.It means evaluation of a++, evaluation of b++, or execution of f.

So evaluation of a++ may be sequenced before execution of g, evaluation of b++ and execution of f may be sequenced after execution of g.

call g, a=1, b=0, call f, a=1, b=1,

  1. It means value computation or side effect.

So value computation of a++ may be sequenced before execution of g, side effect of a++, evaluation of b++ and execution of f may be sequenced after execution of g.

call g, a=0, b=0, call f, a=1, b=1,

In this case,

f(a++, b++) + (a = g());
1.value computation of a++
2.execution of g
3.side effect of a++
4.side effect of = (a = 0)
5.evaluation of b++
6.execution of f

call g, a=0, b=0, call f, a=0, b=1,

Which one is right? Or other answer?

I'm not an English speaker, and I'm not very good at English.

I hope you can understand what i say

f(h1(), h2()) + g(h3(), h4())

h1 and h2 are sequenced before f, h3 and h4 are sequenced before g.

Is it possible:

  1. h1

  2. h4

  3. h2

  4. f

  5. h3

  6. g

解决方案

[expr.post.incr]/1 ... The value computation of the ++ expression is sequenced before the modification of the operand object. With respect to an indeterminately-sequenced function call, the operation of postfix ++ is a single evaluation. [ Note: Therefore, a function call shall not intervene between the lvalue-to-rvalue conversion and the side effect associated with any single postfix ++ operator. —end note ]...

My reading of this is that the side effects of a++ and b++ must complete before the body of f is executed, and therefore within f it must be that a==1 and b==1. Your examples #2 and #3 are not possible with a conforming implementation.

A call to g is indeterminately sequenced with a call to f, and thus can observe either pre-increment or post-increment values.

这篇关于“之前测序”和“调用函数中的每个评估”在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆