简体   繁体   English

函数调用序列(C ++)

[英]Sequence of function calls (C++)

result= function_1()*function_2();

I am writing a code like above. 我正在写一个像上面这样的代码。 What I want to know is while doing the multiplication, which function is called first? 我想知道的是在进行乘法时,首先调用哪个函数? That is because, the first called function can effect the result returned from the other function. 这是因为,第一个被调用的函数可以影响从另一个函数返回的结果。 I assumed function_1() is called first, and when I tried I saw that it is really so. 我假设首先调用function_1() ,当我尝试时,我看到它确实如此。 However, is it always the case? 但是,总是如此吗? Does it depend on which compiler I use or the system I work on? 是否取决于我使用的编译器或我使用的系统?

Order of evaluation is unspecified by the C++ (or the C) standard (see answer from Vlad ). C ++(或C)标准未指定评估顺序(参见Vlad的回答 )。 If your function_1 or function_2 have significant side-effects , it may become some unspecified behavior which you should absolutely avoid (like you should avoid undefined behavior ). 如果你的function_1function_2有明显的副作用 ,它可能会成为你应该绝对避免的一些未指明的行为 (就像你应该避免未定义的行为 )。 And in some cases (inlined functions with strong optimizations) the computations might be intermixed. 在某些情况下(具有强大优化功能的内联函数),计算可能会混杂在一起。

Think about weird cases like 想想像奇怪的情况

 static int i;
 int function_1(void) { i++; return i; }
 int function_2(void) { i+=2; return 3*i+1; }

It probably is implementation specific, and might depend upon the actual compiler and the optimization flags. 它可能是特定于实现的,可能取决于实际的编译器和优化标志。

You should code as if the order of function calls is completely random and not reproducible (even if in practice it might be reproducible). 您应该编写代码,好像函数调用的顺序是完全随机的并且不可重现(即使在实践中它可能是可重现的)。 Likewise, you should not expect any particular order of arguments evaluation (eg in f(i++, ++j) you don't know if i or j has been incremented first), even if for a given compiler that order might be fixed. 同样,您不应该期望任何特定的参数评估顺序(例如,在f(i++, ++j)您不知道ij是否已经先递增),即使对于给定的编译器,该订单可能是固定的。 Again, you should imagine a completely random and non-reproducible order. 同样,您应该想象一个完全随机且不可重现的顺序。

As commented by David Schwartz , if you care about the order, you should code explicitly some sequence points 正如David Schwartz评论的那样,如果您关心订单,则应该明确编写一些序列点

At last, if your code is depending upon some order, it is completely unreadable and for that simple readability reason you should avoid coding this way. 最后,如果您的代码依赖于某些顺序,则它完全不可读,并且出于简单的可读性原因,您应该避免以这种方式编码。

According to the C++ Standard (1.9 Program execution) 根据C ++标准(1.9程序执行)

15 Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. 15除非另有说明,否则对单个算子的操作数和个别表达式的子表达式的评估是不确定的。

So in this expression 所以在这个表达中

result= function_1()*function_2();

some compilers can evaluate at first function_1() and then function_2() while other compilers can evaluate at first function_2() and only then function_1(). 一些编译器可以首先评估function_1()然后是function_2(),而其他编译器可以先评估function_2()然后再评估function_1()。 Even if you write like 即使你写得像

result= (function_1())*(function_2());

or 要么

result= (function_1())*function_2();

or 要么

result= function_1()*(function_2());

nothing will be changed relative to the order of evaluation of the operands. 相对于操作数的评估顺序,什么都不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM