title: GCC emits extra code for boost::shared_ptr dereference
body: <p>I have the following code:</p>
  
  <pre><code>#include &lt;boost/shared_ptr.hpp&gt;
  
  struct Foo { int a; };
  static int A;
  
  void
  func_shared(const boost::shared_ptr&lt;Foo&gt; &amp;foo) {
      A = foo-&gt;a;
  }
  
  void
  func_raw(Foo * const foo) {
      A = foo-&gt;a;
  }
  </code></pre>
  
  <p>I thought the compiler would create identical code, but for <code>shared_ptr</code> version an extra seemingly redundant instruction is emitted.</p>
  
  <pre><code>Disassembly of section .text:
  
  00000000 &lt;func_raw(Foo*)&gt;:
     0:   55                      push   ebp
     1:   89 e5                   mov    ebp,esp
     3:   8b 45 08                mov    eax,DWORD PTR [ebp+8]
     6:   5d                      pop    ebp
     7:   8b 00                   mov    eax,DWORD PTR [eax]
     9:   a3 00 00 00 00          mov    ds:0x0,eax
     e:   c3                      ret
     f:   90                      nop
  
  00000010 &lt;func_shared(boost::shared_ptr&lt;Foo&gt; const&amp;)&gt;:
    10:   55                      push   ebp
    11:   89 e5                   mov    ebp,esp
    13:   8b 45 08                mov    eax,DWORD PTR [ebp+8]
    16:   5d                      pop    ebp
    17:   8b 00                   mov    eax,DWORD PTR [eax]
    19:   8b 00                   mov    eax,DWORD PTR [eax]
    1b:   a3 00 00 00 00          mov    ds:0x0,eax
    20:   c3                      ret
  </code></pre>
  
  <p>I'm just curious, is this necessary, or it is just an optimizer's shortcoming?</p>
  
  <p>Compiling with <code>g++ 4.1.2</code>, <code>-O3 -NDEBUG</code>.</p>