title: How to determine the result type of a C++ lambda (Closure Type)
body: <p>I'm trying to write a template function that works with either a <code>std::function</code> or a lambda. Let's assume an <code>apply</code> template something like this:</p>
  
  <pre><code>template&lt;typename F&gt;
  typename F::result_type apply(const F &amp;f) {
      return f();
  }
  </code></pre>
  
  <p>This is fine if I invoke it with a <code>std::function&lt;int()&gt;</code> for example, but not if a invoke it with <code>[]() -&gt; int { return 1; }</code> because the lambda's closure type doesn't have a <code>result_type</code> member. So how do I write <code>apply</code>'s return type so it works properly?</p>
  
  <p>I'm using clang 3.5 and C++14 at the moment, but whatever I get it'd be best if it was portable -- I'd be happy to switch to a newer compiler if that'd help.</p>
  
  <p>The actual example is a bit more complex. I'm trying to write something like <code>reduce</code> that would apply arguments to a callable if the first argument is callable, otherwise it will ignore the arguments and return the value passed as the first argument.</p>
  
  <pre><code>reduce(0, 1, 2); // returns 0
  reduce([](int a, int b) { return a+b; }, 2, 3); // return 5
  </code></pre>