title: Obtaining function pointer to lambda?
body: <p>I want to be able to obtain a function pointer to a lambda in C++.</p>
  
  <p>I can do:</p>
  
  <pre><code>int (*c)(int) = [](int i) { return i; };
  </code></pre>
  
  <p>And, of course, the following works - even if it's not creating a function pointer.</p>
  
  <pre><code>auto a = [](int i) { return i; };
  </code></pre>
  
  <p>But the following:</p>
  
  <pre><code>auto *b = [](int i) { return i; };
  </code></pre>
  
  <p>Gives this error in GCC:</p>
  
  <pre><code>main.cpp: In function 'int main()':
  main.cpp:13:37: error: unable to deduce 'auto*' from '&lt;lambda closure object&gt;main()::&lt;lambda(int)&gt;{}'
       auto *b = [](int i) { return i; };
                                        ^
  main.cpp:13:37: note:   mismatched types 'auto*' and 'main()::&lt;lambda(int)&gt;'
  </code></pre>
  
  <p>It seems arbitrary that a lambda can be converted to a function pointer without issue, but the compiler cannot infer the function type and create a pointer to it using <code>auto *</code>. Especially when it can implicitly convert a <code>unique, lambda type</code> to a function pointer:</p>
  
  <pre><code>int (*g)(int) = a;
  </code></pre>
  
  <p>I've create a little test bed at <a href="http://coliru.stacked-crooked.com/a/2cbd62c8179dc61b">http://coliru.stacked-crooked.com/a/2cbd62c8179dc61b</a> that contains the above examples. This behavior is the same under C++11 and C++14.</p>