title: Binding to a weak_ptr
body: <p>Is there a way to <strong>std::bind to a std::weak_ptr</strong>? I'd like to store a "weak function" callback that automatically "disconnects" when the callee is destroyed.</p>
  
  <p>I know how to create a std::function using a shared_ptr:</p>
  
  <pre><code>std::function&lt;void()&gt; MyClass::GetCallback()
  {
      return std::function&lt;void()&gt;(std::bind(&amp;MyClass::CallbackFunc, shared_from_this()));
  }
  </code></pre>
  
  <p>However the returned std::function keeps my object alive forever. So I'd like to bind it to a <strong>weak_ptr</strong>:</p>
  
  <pre><code>std::function&lt;void()&gt; MyClass::GetCallback()
  {
      std::weak_ptr&lt;MyClass&gt; thisWeakPtr(shared_from_this());
      return std::function&lt;void()&gt;(std::bind(&amp;MyClass::CallbackFunc, thisWeakPtr));
  }
  </code></pre>
  
  <p>But that doesn't compile. (std::bind will accept no weak_ptr!) Is there any way to bind to a weak_ptr?</p>
  
  <p>I've found discussions about this (see below), but there seems to be no standard implementation. What is the best solution for storing a "weak function", in particular if Boost is not available?</p>
  
  <hr>
  
  <p>Discussions / research (all of these use Boost and are not standardized):</p>
  
  <ul>
  <li><a href="http://lists.boost.org/Archives/boost/2012/01/189510.php">weak_function</a></li>
  <li><a href="https://svn.boost.org/trac/boost/ticket/810">weak_ptr binding</a></li>
  <li><a href="http://lists.boost.org/boost-users/2011/09/70647.php">"weak" binding</a> (and a <a href="http://lists.boost.org/Archives/boost/2012/01/189529.php">fix</a> for it)</li>
  <li><a href="http://permalink.gmane.org/gmane.comp.lib.boost.devel/227213">weak_fn</a></li>
  <li>Another <a href="http://lists.boost.org/Archives/boost/2009/03/149980.php">weak_fn</a></li>
  </ul>