title: Does const allow for (theoretical) optimization here?
body: <p>Consider this snippet:</p>
  
  <pre><code>void foo(const int&amp;);
  
  int bar();
  
  int test1()
  {
      int x = bar();
      int y = x;
      foo(x);
      return x - y;
  }
  
  int test2()
  {
      const int x = bar();
      const int y = x;
      foo(x);
      return x - y;
  }
  </code></pre>
  
  <p>In my understanding of the standard, neither <code>x</code> nor <code>y</code> are allowed to be changed by <code>foo</code> in <code>test2</code>, whereas they could be changed by <code>foo</code> in <code>test1</code> (with e.g. a <code>const_cast</code> to remove <code>const</code> from the <code>const int&amp;</code> because the referenced objects aren't actually const in <code>test1</code>).</p>
  
  <p>Now, <a href="https://godbolt.org/z/eI0bXA" rel="noreferrer">neither gcc nor clang nor MSVC</a> seem to optimize <code>test2</code> to <code>foo(bar()); return 0;</code>, and I can understand that they do not want to waste optimization passes on an optimization that only rarely applies in practice.</p>
  
  <p>But am I at least correct in my understanding of this situation, or am I missing some legal way for <code>x</code> to be modified in <code>test2</code>?</p>