title: Function wrapper that works for all kinds of functors without casting
body: <p>I'd like to create a function that takes a weak pointer and any kind of functor (lambda, <code>std::function</code>, whatever) and returns a new functor that only executes the original functor when the pointer was not removed in the meantime (so let's assume there is a <code>WeakPointer</code> type with such semantics). This should all work for any functor without having to specify explicitly the functor signature through template parameters or a cast.</p>
  
  <p><strong>EDIT:</strong>
  Some commenters have pointed out that <code>std::function</code> - which I used in my approach - might not be needed at all and neither might the lambda (though in my original question I also forgot to mention that I need to capture the weak pointer parameter), so any alternative solution that solves the general problem is of course is also highly appreciated, maybe I didn't think enough outside the box and was to focused on using a lambda + <code>std::function</code>. In any case, here goes what I tried so far:</p>
  
  <pre><code>template&lt;typename... ArgumentTypes&gt;
  inline std::function&lt;void(ArgumentTypes...)&gt; wrap(WeakPointer pWeakPointer, const std::function&lt;void(ArgumentTypes...)&gt;&amp;&amp; fun)
  {
      return [=] (ArgumentTypes... args)
      {
          if(pWeakPointer)
          {
              fun(args...);
          }
      };
  }
  </code></pre>
  
  <p>This works well without having to explicitly specify the argument types if I pass an <code>std::function</code>, but fails if I pass a lambda expression. I guess this because the <code>std::function</code> constructor ambiguity as asked in <a href="https://stackoverflow.com/questions/5931214/isnt-the-template-argument-the-signature-of-stdfunction-part-of-its-type">this question</a>. In any case, I tried the following helper to be able to capture any kind of function:</p>
  
  <pre><code>template&lt;typename F, typename... ArgumentTypes&gt;
  inline function&lt;void(ArgumentTypes...)&gt; wrap(WeakPointer pWeakPointer, const F&amp;&amp; fun)
  {
      return wrap(pWeakPointer, std::function&lt;void(ArgumentTypes...)&gt;(fun));
  }
  </code></pre>
  
  <p>This now works for lambdas that don't have parameters but fails for other ones, since it always instantiates <code>ArgumentTypes...</code> with an empty set.</p>
  
  <p>I can think of two solution to the problem, but didn't manage to implement either of them:</p>
  
  <ol>
  <li>Make sure that the correct <code>std::function</code> (or another Functor helper type) is created for a lambda, i.e. that a lambda with signature <code>R(T1)</code> results in a <code>std::function(R(T1))</code> so that the <code>ArgumentTypes...</code> will be correctly deduced</li>
  <li>Do not put the <code>ArgumentTypes...</code> as a template parameter instead have some other way (boost?) to get the argument pack from the lambda/functor, so I could do something like this:</li>
  </ol>
  
  <p>-</p>
  
  <pre><code>template&lt;typename F&gt;
  inline auto wrap(WeakPointer pWeakPointer, const F&amp;&amp; fun) -&gt; std::function&lt;void(arg_pack_from_functor(fun))&gt;
  {
      return wrap(pWeakPointer, std::function&lt;void(arg_pack_from_functor(fun))(fun));
  }
  </code></pre>