title: C++ lambda with captures as a function pointer
body: <p>I was playing with C++ lambdas and their implicit conversion to function pointers. My starting example was using them as callback for the ftw function. This works as expected.</p>
  
  <pre><code>#include &lt;ftw.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      auto callback = [](const char *fpath, const struct stat *sb,
          int typeflag) -&gt; int {
          cout &lt;&lt; fpath &lt;&lt; endl;
          return 0;
      };
  
      int ret = ftw("/etc", callback, 1);
  
      return ret;
  }
  </code></pre>
  
  <p>After modifying it to use captures:</p>
  
  <pre><code>int main()
  {
  
      vector&lt;string&gt; entries;
  
      auto callback = [&amp;](const char *fpath, const struct stat *sb,
          int typeflag) -&gt; int {
          entries.push_back(fpath);
          return 0;
      };
  
      int ret = ftw("/etc", callback, 1);
  
      for (auto entry : entries ) {
          cout &lt;&lt; entry &lt;&lt; endl;
      }
  
      return ret;
  }
  </code></pre>
  
  <p>I got the compiler error:</p>
  
  <pre><code>error: cannot convert ‘main()::&lt;lambda(const char*, const stat*, int)&gt;’ to ‘__ftw_func_t {aka int (*)(const char*, const stat*, int)}’ for argument ‘2’ to ‘int ftw(const char*, __ftw_func_t, int)’
  </code></pre>
  
  <p>After some reading. I learned that lambdas using captures <strong>can't be implicitly converted</strong> to function pointers.</p>
  
  <p>Is there a workaround for this? Does the fact that they can't be "implicitly" converted mean s that they can "explicitly" converted? (I tried casting, without success). What would be a clean way to modify the working example so that I could append the entries to some object using lambdas?.</p>