title: iterate over a map of shared pointers without increasing the reference count?
body: <p>I would like to know whether the following code will result in an <strong>increase in the reference count</strong> for each shared pointer, or whether the optimizer will be clever enough to recognize that we are not in fact <strong>copying</strong> the pointer, just <strong>dereferencing</strong> it.</p>
  
  <pre><code>std::map&lt;int, std::shared_ptr&lt;foo&gt;&gt; map;
  
  ...
  
  for (auto kv : map)
      kv.second-&gt;func();
  </code></pre>
  
  <p><code>kv</code> is a <code>std::pair&lt;int, std::shared_ptr&lt;foo&gt;&gt;</code></p>
  
  <p>Since the range-based for-loop will return a stack-allocated <code>std::pair</code>, which in turn stores a <strong>copy</strong> of the <code>std::shared_ptr</code>, I believe that the reference count will be increased at this point.</p>
  
  <p>However, it is plain to see that this copy is just temporary, and the intention here is not to copy ownership, but just dereference the currently owned copy.</p>
  
  <p>But since the creation of the pair causes a a <em>side-effect</em>, an increase in the reference count, does this mean the optimizer will not be able to optimize this copy out, or have the compiler/optimizer writers recognized this use-case and been able to optimize out the copy?</p>