title: std::bind and std::weak_ptr
body: <p>I have a class that creates a wrapper functor to deal with a weak_ptr to an object.  When the functor executes, it will test the weak_ptr before invoking the object functor.</p>
  
  <p>Additionally, the wrapper functor can return a default value if the object no longer exists.</p>
  
  <p>This is an excerpt of a class that compiles under GCC 4.4.7...</p>
  
  <pre><code>class Bind
  {
  public:
  
      // non-void weak-functor
      template &lt;typename R&gt;
      struct Wrapper&lt;R, typename std::enable_if&lt;!std::is_void&lt;R&gt;::value&gt;::type&gt;
      {
          using result_type = R;
  
          template &lt;typename O&gt;
          R operator()(const std::weak_ptr&lt;O&gt; &amp;&amp; obj,
                       std::function&lt;R()&gt; &amp;&amp; func, R &amp;&amp; ret)
          {
              if (auto ptr = obj.lock())
              {
                  return func();
              }
  
              return std::forward&lt;R&gt;(ret);
          }
      };
  
      // weak from shared - non-void return
      template &lt;typename R, typename F, typename O, typename... A&gt;
      static std::function&lt;R()&gt; Weak(R &amp;&amp; ret,
                                     F func,
                                     const std::shared_ptr&lt;O&gt; &amp;&amp; obj,
                                     A&amp;&amp;... args)
      {
          return std::bind(Wrapper&lt;R&gt;(),
                           std::weak_ptr&lt;O&gt;(obj),
                           std::function&lt;R()&gt;(std::bind(func, obj.get(),
                                              std::forward&lt;A&gt;(args)...)),
                           ret);
      }
  
      // ...
  };
  </code></pre>
  
  <p>The class can be used like this...</p>
  
  <pre><code>auto x = std::make_shared&lt;X&gt;();
  auto f = Bind::Weak(false, &amp;X::foo, x, 1, 2, 3);
  
  f();   // returns x::foo(1, 2, 3) 
  
  x.reset()
  
  f();   // returns false
  </code></pre>
  
  <p>Unfortunately, after upgrading the compiler to GCC 4.8.3 it no longer compiles.  The error starts like this...</p>
  
  <pre><code>error: could not convert 'std::bind(_Func&amp;&amp;, _BoundArgs&amp;&amp; ...)
  </code></pre>
  
  <p>...with the usual mountain of template crud.  The jist is that it can no longer convert the result of std::bind() inside Bind::Weak() into the return type of Bind::Weak().</p>
  
  <p>I've fiddled with this for a few hours now and haven't been able to resolve the issue.</p>