0

According to 5/1 (the Standard):

An expression can result in a value and can cause side effects.

So obviously we have two possible options:
1) Expression results in a value and cause side effects
2) Expression results in a value and doesn't cause side effects

What are the other possible options? (For example are there any expressions which don't result in a value?)
I thought about throw-expressions and functions with void return type. Can we refer them to the first or second category (value of void type with possible side effects)?

Rodvi
  • 195
  • 1
  • 9

2 Answers2

6

What are the other possible options?

  1. Expression doesn't result in a value and causes side effects
  2. Expression doesn't result in a value and does not cause side effects

Expressions with void return type do not result in a value. Expressions in 4. do not affect the behaviour of the program.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 4 might be useful in debugging, depending on your tools. But other than that, spot on. – Captain Giraffe Feb 03 '17 at 11:39
  • 1
    @CaptainGiraffe I changed wording to accommodate. – eerorika Feb 03 '17 at 11:41
  • Do you mean that expressions in 3 and 4 return void? – Rodvi Feb 03 '17 at 11:49
  • 6. Expression crashes the program (no return, no side effects within the process). Example: `*(char*)nullptr = 42` However, from a C++ language perspective, one should probably rephrase this: 6. Expression exposes undefined behavior. – cmaster - reinstate monica Feb 03 '17 at 11:54
  • @M.M Thanks. I suspected that was the case. I removed since I think the distinction is off topic. – eerorika Feb 03 '17 at 11:56
  • @cmaster C++ answers usually have an implied "unless the program causes undefined behaviour in which case anything can happen" – M.M Feb 03 '17 at 11:56
  • According to this [post](http://stackoverflow.com/a/9565169/7123797) 5. can be subset of 4 too. "Exceptions are a slightly awkward special case, in that altering the flow of control does change the state of the abstract machine (by changing the current point of execution), but isn't a side-effect. The code to construct, handle and destroy the exception may have its own side-effects, of course." – Rodvi Feb 03 '17 at 11:56
  • @Rodvi side-effects of function calls involved in a side-effect are not considered side-effects of the original expression (they have different sequencing too) – M.M Feb 03 '17 at 11:57
  • @M.M Nevertheless, undefined behavior like a crash is definitely different enough from all the "normal" results that it's worth mentioning. I mean, we even have standardized functions that *intentionally crash the process* like `abort()`. You can neither say that `abort()` has no side effects (it crashes the process), nor that it has side effects (the process won't notice a side effect, it'll just be dead). – cmaster - reinstate monica Feb 03 '17 at 12:14
0

Given that exit(0) is an expression, we must include the possibility that evaluating an expression ends the program.

MSalters
  • 173,980
  • 10
  • 155
  • 350