title: inserting temporary std::shared_ptr into std::map, is it bad?
body: <p>I'm designing a class for my application that implements a lot of standard shared pointers and usage of standard containers such as <em>std::map</em> and <em>std::vector</em></p>
  
  <p>It's very specific question to the problem so I just copied a piece of code
  from my header for clarification purposes..
  here is a snapshot of that declarations from the header:</p>
  
  <pre><code>struct Drag;
  std::map&lt;short, std::shared_ptr&lt;Drag&gt;&gt; m_drag;
  typedef sigc::signal&lt;void, Drag&amp;&gt; signal_bet;
  inline signal_bet signal_right_top();
  </code></pre>
  
  <p>and here is one of the functions that uses the above declarations and a temporary shared_ptr which is intended to be used not only in this function but until some late time. that means after the function returns a shared pointer should be still alive because it will be assigned at some point to another shared_ptr.</p>
  
  <pre><code>void Table::Field::on_signal_left_top(Drag&amp; drag)
  {
      m_drag.insert(std::make_pair(drag.id, std::make_shared&lt;Drag&gt;(this))); // THIS!
      auto iter = m_drag.find(drag.id);
      *iter-&gt;second = drag;
      iter-&gt;second-&gt;cx = 0 - iter-&gt;second-&gt;tx;
      iter-&gt;second-&gt;cy = 0 - iter-&gt;second-&gt;ty;
  
      invalidate_window();
  }
  </code></pre>
  
  <p>the above function first insert a new shared_ptr and then assigns the values from one object into another,</p>
  
  <p>What I need from your answer is to tell whether is it safe to insert temporary shared_ptr into the map and be sure that it will not be a dangling or what ever bad thing.</p>
  
  <p>According to <a href="http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/shared_ptr.htm#BestPractices" rel="nofollow"><em>THIS</em></a> website the above function is not considered safe because it would much better to write it like so:</p>
  
  <pre><code>void Table::Field::on_signal_left_top(Drag&amp; drag)
  {
      std::shared_ptr pointer = std::make_shared&lt;Drag&gt;(this);
      m_drag.insert(std::make_pair(drag.id, pointer));
      auto iter = m_drag.find(drag.id);
      *iter-&gt;second = drag;
      // etc...
   }
  </code></pre>
  
  <p>well one line more in the function.</p>
  
  <p>is it really required to type it like that and why ?</p>