是按位还是保证评估顺序

是按位还是保证评估顺序

本文介绍了是按位还是保证评估顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

说我有这个代码:

unsigned int func1();
unsigned int func2();
unsigned int func3();

unsigned int x = func1() | func2() | func3();

C ++保证先调用func1(),然后调用func2(),然后调用func3 )?

Does C++ guarantee that func1() will be called first, then func2(), and then func3()?

还是编译器允许以任何顺序调用函数?

Or is the compiler allowed to call the functions in any order it feels like?

是编译器允许实现短路优化这里如果它想要? (例如,如果func1()返回〜0,编译器决定不要调用func2()或func3(),因为它知道它们的返回值不可能影响分配给x的值?)

Also, is the compiler allowed to implement a short-circuit optimization here if it wants to? (e.g. if func1() returned ~0, could the compiler decide not to bother calling func2() or func3(), because it knows their return values can't possibly affect the value assigned to x?)

推荐答案

不,不能保证调用函数的顺序。 || | 不表示序列点。

No, there is no guarantee which order the functions will be called in. Unlike ||, | does not imply a sequence point.

必须调用表达式中的所有函数,除非实现可以确定他们没有副作用,它可以确定表达式的结果,而不实际调用其中的一个函数。实现可以在仿佛规则下做到这一点,其允许实现执行任何优化,该优化不能由合格程序观察或检测。

All functions in the expression must be called unless the implementation can determine that they have no side-effects and it can determine the result of the expression without actually calling one of the functions. The implementation can do this under the "as if" rule which allows the implementation to perform any optimization which cannot be observed or detected by a conforming program.

这篇关于是按位还是保证评估顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:17