是否有与放大器定义的计算顺序; =和| =? [英] Is there a defined evaluation order for &= and |=?

查看:206
本文介绍了是否有与放大器定义的计算顺序; =和| =?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有一个C函数返回一个整数,你可以写这样的语句:

 敏&安培; = MYFUNC();

...这里我们使用按位与赋值运算符。

现在的问题是:是MYFUNC()保证会执行,即使敏等于零

Likwise,如果我们使用按位OR赋值运算符?(| =),将MYFUNC()总是被执行,即使是敏设置为所有的人

换句话说:是在C允许按位运算符懒惰的评价


解决方案

 敏&安培; = MYFUNC();

相当于:

 敏=敏和放大器; MYFUNC();

语言指出&放大器;经营者不得短路。然而,
一个优化器可以产生code不调用函数如敏
是零,这是肯定的功能没有副作用。我怀疑任何编译器acrtually做到这一点,因为运行时测试可能使它成为pessimisation。

If you have a C function which returns an integer, you could write a statement like this:

MyInt &= MyFunc();

...where we're using the bitwise-AND assignment operator.

The question is: is MyFunc() guaranteed to be executed, even if MyInt equals zero?

Likwise, if we used the bitwise-OR assignment operator (|=), would MyFunc() always be executed, even if MyInt were set to all ones?

Put another way: is lazy evaluation permitted in C for bitwise operators?

解决方案

MyInt &= MyFunc();

is equivalent to:

MyInt = MyInt & MyFunc();

The language states that the & operator is not short-circuited. However, an optimiser could generate code not to call to the function if MyInt was zero and it was sure that the function had no side effects. I doubt any compilers acrtually do this, as the runtime test probably makes it a pessimisation.

这篇关于是否有与放大器定义的计算顺序; =和| =?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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