How are side effects and observable behavior related in C++?

霸气de小男生 提交于 2019-12-04 23:25:30

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;
}

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!