title: Why is '--++a-​- ++ +b--' evaluated in this order?
body: <p>Why does the following print <code>bD aD aB aA aC aU</code> instead of <code>aD aB aA aC bD aU</code>? In other words, why is <code>b--</code> evaluated before <code>--++a--++</code>?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class A {
      char c_;
  public:
      A(char c) : c_(c) {}
      A&amp; operator++() {
          cout &lt;&lt; c_ &lt;&lt; "A ";
          return *this;
      }
      A&amp; operator++(int) {
          cout &lt;&lt; c_ &lt;&lt; "B ";
          return *this;
      }
      A&amp; operator--() {
          cout &lt;&lt; c_ &lt;&lt; "C ";
          return *this;
      }
      A&amp; operator--(int) {
          cout &lt;&lt; c_ &lt;&lt; "D ";
          return *this;
      }
      void operator+(A&amp; b) {
          cout &lt;&lt; c_ &lt;&lt; "U ";
      }
  };
  
  int main()
  {
      A a('a'), b('b');
      --++a-- ++ +b--;  // the culprit
  }
  </code></pre>
  
  <p>From what I gather, here's how the expression is parsed by the compiler:</p>
  
  <ul>
  <li>Preprocessor tokenization: <code>--</code> <code>++</code> <code>a</code> <code>--</code> <code>++</code> <code>+</code> <code>b</code> <code>--</code>;</li>
  <li>Operator precedence<sup>1</sup>: <code>(--(++((a--)++))) + (b--)</code>;</li>
  <li><code>+</code> is left-to-right associative, but nonetheless the compiler may choose to evaluate the expression on the right (<code>b--</code>) first.</li>
  </ul>
  
  <p>I'm assuming the compiler chooses to do it this way because it leads to better optimized code (less instructions). However, it's worth noting that I get the same result when compiling with <code>/Od</code> (MSVC) and <code>-O0</code> (GCC). This brings me to my question:</p>
  
  <p>Since I was asked this on a test which should <em>in principle</em> be implementation/compiler-agnostic, <strong>is there something in the C++ standard that prescribes the above behavior, or is it truly unspecified?</strong> Can someone quote an excerpt from the standard which confirms either? Was it wrong to have such a question on the test?</p>
  
  <p><sub><sup>1</sup> <em>I realize the compiler doesn't really know about operator precedence or associativity, rather it cares only about the language grammar, but this should get the point across either way.</em></sub></p>