gpt4 book ai didi

c++ - C++ 中的副作用和可观察行为如何相关?

转载 作者:IT老高 更新时间:2023-10-28 22:38:12 27 4
gpt4 key购买 nike

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

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

and then and then 1.9/7 定义了副作用:

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.

副作用是可观察到的行为吗?它们之间的关系如何?

最佳答案

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

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

如果需要,大多数编译器会完全删除 a。这是允许的。分配和添加是不可观察的。

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

关于c++ - C++ 中的副作用和可观察行为如何相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13271469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com