c - 允许对易失性对象进行优化
问题描述
来自ISO/IEC 9899:201x第5.1.2.3 节程序执行第4段:
在抽象机中,所有表达式都按照语义的规定进行评估。如果一个实际的实现可以推断出它的值没有被使用并且没有产生所需的副作用(包括调用函数或访问易失性对象引起的任何副作用),则它不需要评估表达式的一部分。
关于 volatile 对象,这里允许的优化究竟是什么?有人可以举一个可以优化的易失性访问的例子吗?
由于 volatiles 访问是一种可观察的行为(在第6段中描述),似乎没有关于 volatiles 的优化可以采取,所以,我很想知道第4部分允许哪些优化。
解决方案
稍微重新格式化:
An actual implementation need not evaluate part of an expression if:
a) it can deduce that its value is not used; and
b) it can deduce that that no needed side effects are produced (including any
caused by calling a function or accessing a volatile object).
在不改变含义的情况下反转逻辑:
An actual implementation must evaluate part of an expression if:
a) it can't deduce that its value is not used; or
b) it can't deduce that that no needed side effects are produced (including
any caused by calling a function or accessing a volatile object).
简化以关注易失性部分:
An actual implementation must evaluate part of an expression if needed
side effects are produced (including accessing a volatile object).
推荐阅读
- laravel - 使用 clientOriginalName 保存多个上传
- javascript - 在javascript中将字符串转换为数组
- c# - C# ASP.net MVC Ajax MySQL DATATABLES 列搜索无法将搜索值传递给控制器
- php - 如何在PHP中删除数组中的值
- javascript - 如何在单击事件中为具有相同类的多个按钮添加多个相同的 ID
- php - 转换日期时区的问题
- react-native - 每次应用程序从后台进入前台时都会触发“FCM.on(FCMEvent.Notification,notif”)
- android - 如何获得多个查询的结果?
- dart - Flutter中基于动态数据扩展卡片高度
- ios - 如何在 iOS swift 中制作登录屏幕的单元测试用例