C ++中的“副作用”究竟是什么? [英] What exactly is a 'side-effect' in C++?

查看:332
本文介绍了C ++中的“副作用”究竟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是一个标准术语,是定义明确的,或只是一个由开发人员解释一个概念的术语(..和什么是概念)?据我所知,这与所有混淆的序列点,但不确定。



我发现一个定义这里,但是这不会使每个代码的语句都有副作用吗?



< blockquote>

副作用是运算符,表达式,语句或函数在运算符,表达式,语句或函数完成计算后仍然存在的结果。


有人可以解释一下术语副作用在C ++中的形式意味着什么,意义何在? b
$ b

为了参考,一些关于副作用的问题:


  1. 我的逗号运算子没有副作用?

  2. 强制编译器不优化副作用语句

  3. 传递对象时的副作用在C ++中运行


解决方案

副作用 [intro.execution] 中的C ++标准:


通过volatile glvalue(3.10),修改对象,调用库I / O函数或调用执行任何这些操作的函数都是副作用,这是执行环境的状态的变化。



Is it a standard term which is well defined, or just a term coined by developers to explain a concept (.. and what is the concept)? As I understand this has something to do with the all-confusing sequence points, but am not sure.

I found one definition here, but doesn't this make each and every statement of code a side effect?

A side effect is a result of an operator, expression, statement, or function that persists even after the operator, expression, statement, or function has finished being evaluated.

Can someone please explain what the term 'side effect' formally means in C++, and what is its significance?

For reference, some questions talking about side effects:

  1. Is comma operator free from side effect?
  2. Force compiler to not optimize side-effect-less statements
  3. Side effects when passing objects to function in C++

解决方案

A "side effect" is defined by the C++ standard in [intro.execution], by:

Accessing an object designated by a volatile glvalue (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.

这篇关于C ++中的“副作用”究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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