Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a 'side-effect' in C++?

Tags:

c++

c++11

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++
like image 374
Lazer Avatar asked Mar 05 '12 08:03

Lazer


People also ask

What does side effect definition?

Definition of side effect : a secondary and usually adverse effect (as of a drug) toxic side effects. — called also side reaction.

What is a method side effect?

A side effect method is a method which modifies some state variable value/arguments passed having a consequence beyond its scope, that is to say it has an observable effect besides returning a value (the main effect) to the invoker of the operation.

What is persistent side effects in C?

Definition of persistent side effect: "A side effect is said to be persistent at a particular point in execution if it might have an effect on the execution state at that point." [Reference: Misra C, Appendix J] c volatile misra.

What is a side effect free function?

A method of an object can be designed as a Side-Effect-Free Function [Evans]. A function is an operation of an object that produces output but without modifying its own state. Since no modification occurs when executing a specific operation, that operation is said to be side-effect free.


1 Answers

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

Reading 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.

like image 200
Mankarse Avatar answered Oct 04 '22 09:10

Mankarse