2

As far as we know, the function argument evaluation order is not defined by c++ standard.
For example:

 f(g(), h());

So we know it is undefined.
My question is, why cant c++ standard define the order of evaluation from left to right??

Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
  • 4
    Optimization. Compiler might choose the best order to evaluate expressions, if it sees possibility for optimization. And besides, why would you need left-to-right order? – Yksisarvinen Apr 27 '18 at 12:59
  • i mean for example left to right, it could be any order – Eduard Rostomyan Apr 27 '18 at 13:00
  • Not a duplicated, I am familiar with that question @codekaizer, but it does not answer my question overall, it just says that better code can be generated. – Eduard Rostomyan Apr 27 '18 at 13:10
  • 1
    @EduardRostomyan so does this not answer your question: "Better code can be generated in the absence of restrictions on expression evaluation order" – Joseph D. Apr 27 '18 at 13:11
  • 1
    Related: [What are the evaluation order guarantees introduced by C++17?](https://stackoverflow.com/questions/38501587/what-are-the-evaluation-order-guarantees-introduced-by-c17) – Jesper Juhl Apr 27 '18 at 13:11
  • 1
    For example, when pushing parameters on the stack, functions with a variable number of arguments (`printf`) might work the best if you evaluate right-to-left so the first argument always ends up on top. – Bo Persson Apr 27 '18 at 13:52

1 Answers1

5

Because there is no good reason to do so.

The c++ standard generally only defines what is necessary and leaves the rest up to implementers.

This is why it produces fast code and can be compiled for many platforms.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60