如何在C ++中有相关的副作用和可观察的行为? [英] How are side effects and observable behavior related in C++?

查看:228
本文介绍了如何在C ++中有相关的副作用和可观察的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 03标准1.9 / 6定义可观察的行为

C++03 Standard 1.9/6 defines observable behavior:


抽象的可观察行为机器是其对易失性数据的读取和写入以及对库I / O功能的调用的序列。

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions.

然后是1.9 / 7定义

and then and then 1.9/7 defines side effects:


访问由volatile lvalue指定的对象库I / O函数或调用执行任何这些操作的函数都是副作用,这是执行环境的状态的变化。

Accessing an object designated by a volatile lvalue (3.10), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment.

副作用是否是可观察的行为?它们之间如何相互关联?

Is a side effect an observable behavior or not? How are they related to each other?

推荐答案

不,副作用不一定是可观察的行为。例如,修改非易失性对象是一种副作用,但不可观察。差别很重要,因为只要可观察的行为保持不变,编译器可能会重新排列或删除副作用。

No, a side effect is not necessarily observable behaviour. Modifying a non-volatile object, for example, is a side effect, but not observable. The difference matters because the side effects may be rearranged or removed altogether by the compiler, so long as the observable behaviour remains the same.

int main()
{
    int a;
    a = 30;
    a += 2;
    return 0;
}

如果请求,大多数编译器将删除 / code>完全。这是允许的。

Most compilers will, if requested, remove a completely. That's permitted. The assignments and addition aren't observable.

所有可观察到的行为都必然是副作用。

All observable behaviour must necessarily be a side effect though.

这篇关于如何在C ++中有相关的副作用和可观察的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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